Python 如何从1个图像的Y通道和另一个图像的U、V通道创建新图像?
我有两张图片,Python 如何从1个图像的Y通道和另一个图像的U、V通道创建新图像?,python,python-imaging-library,rgb,pillow,yuv,Python,Python Imaging Library,Rgb,Pillow,Yuv,我有两张图片,内容和生成的。我想创建一个新图像,Y通道为生成,U和V通道为内容。有了PIL,我想我应该能够使用.convert('YCbCr')将我的RGB输入图像转换为YUV。然后在创建新图像后,我可以使用将其转换为RGB。convert('RGB') 图像以RGB格式进入以下功能: def original_colors(content, generated): generated_y = generated.convert('YCbCr') content_uv = con
内容
和生成的
。我想创建一个新图像,Y通道为生成
,U和V通道为内容
。有了PIL,我想我应该能够使用.convert('YCbCr')
将我的RGB输入图像转换为YUV。然后在创建新图像后,我可以使用将其转换为RGB。convert('RGB')
图像以RGB格式进入以下功能:
def original_colors(content, generated):
generated_y = generated.convert('YCbCr')
content_uv = content_uv.convert('YCbCr')
# Combine Y from generated_y with U and V from content_uv
# and convert the resulting output back to RGB.
return output
将频道组合成新图像的最佳/最有效方法是什么
以下是我采用的解决方案:
# Combine the Y channel of the generated image and the UV/CbCr channels of the
# content image to perform color-independent style transfer.
def original_colors(content, generated):
content_channels = list(content.convert('YCbCr').split())
generated_channels = list(generated.convert('YCbCr').split())
content_channels[0] = generated_channels[0]
return Image.merge('YCbCr', content_channels).convert('RGB')
如果
alpha\u composite()
函数不能完成所需的操作,则可以将图像加载到数据数组中,并使用切片替换数组的相关部分。使用两个图像显示以下内容:
from PIL import Image
import numpy
# open images and make the same size
image1 = Image.open("image1.jpg").resize((256, 256))
image2 = Image.open("image2.jpg").resize((256, 256))
# convert tp YCbCr
image1_y = image1.convert('YCbCr')
image2_y = image2.convert('YCbCr')
# load image data into arrays
image1_y_array = numpy.array(image1_y)
image2_y_array = numpy.array(image2_y)
# show shape and size of arrays
print (image1_y_array.shape)
print (image2_y_array.shape)
#print (image1_y_array[:,:,0]) # uncomment to see actual data
#print (image2_y_array[:,:,0]) # uncomment to see actual data
# replace image 1 Y channel with image 2 Y channel
# assume 1st [0] channel is the Y
image1_y_array[:,:,0] = image2_y_array[:,:,0]
# create new image from the updated array data
new_image1_y = Image.fromarray(image1_y_array)
# and show the result
new_image1_y.show()
# can now convert new_image1_y back to jpeg, etc.
将图像数据加载到阵列中时,可以看到阵列形状输出的3个数据通道:
(256, 256, 3)
(256, 256, 3)
我假设0索引通道是Y通道,如果不是,则根据需要将幻数0替换为1或2
注:显然,图像的大小应该相同。我希望这能有所帮助
编辑:
也可以在不使用numpy的情况下执行相同的操作:
from PIL import Image
# open images and make the same size
image1 = Image.open("image1.jpg").resize((256, 256))
image2 = Image.open("image2.jpg").resize((256, 256))
# convert tp YCbCr
image1_y = image1.convert('YCbCr')
image2_y = image2.convert('YCbCr')
# split image data
image1_y_data = image1_y.split()
image2_y_data = image2_y.split()
# replace image 1 Y channel with image 2 Y channel
# assume 1st [0] channel is the Y
image1_y_data_list = list(image1_y_data)
image2_y_data_list = list(image2_y_data)
image1_y_data_list[0] = image2_y_data_list[0]
# create new image from the updated data
new_image1_y = Image.merge('YCbCr', image1_y_data_list)
# and show the result
new_image1_y.show()
# can now convert new_image1_y back to jpeg, etc.
有可能只使用PIL而不使用Numpy吗?与
Image.merge
、Image.split
和Image.getdata
?@ProGamerDev类似,我使用这些函数添加了第二个版本。希望这能给你一些工作上的帮助。谢谢!我已经在你的解决方案的基础上,用一个只支持PIL的解决方案更新了主帖。