简体   繁体   中英

Combining three RGB images into a single RGB image

I have three RGB images, but each one has only 1 non-zero channel (ie. one has a red channel with 0's in the blue and green channels) and I want to combine them into a single RGB image with the correct channel from each.

I apologise for my phrasing, I don't know much of the terminology (which really isn't helping my search queries)

Here are my images: Blue Green Red

You can also use OpenCV:

blue = cv2.imread("blue.jpg")
red = cv2.imread("red.jpg")
green = cv2.imread("green.jpg")

merge = blue + red + green
cv2.imwrite('merge.jpg', merge)

在此处输入图像描述

I think you can use Image.merge here and take the appropriate channels from each image. Note that I'm using requests.get(...) and BytesIO here to pull down from the linked images but you can just use Image.open(...) directly on the filename instead if you have them locally.

from io import BytesIO
from PIL import Image
import requests

red = Image.open(BytesIO(requests.get('https://i.stack.imgur.com/EKQW4.jpg').content)) 
green = Image.open(BytesIO(requests.get('https://i.stack.imgur.com/Xel7l.jpg').content))
blue = Image.open(BytesIO(requests.get('https://i.stack.imgur.com/vyrqR.jpg').content))
combined = Image.merge('RGB', (red.getchannel('R'), green.getchannel('G'), blue.getchannel('B'))
combined.save('output_image_name.jpg')

And that'll give you something like:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM