Combining three RGB images into a single RGB image

拈花ヽ惹草 提交于 2020-12-08 05:13:48

问题


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


回答1:


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:




回答2:


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)



来源:https://stackoverflow.com/questions/58722340/combining-three-rgb-images-into-a-single-rgb-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!