python - 如何从一张图像的 Y channel 和另一张图像的 U、V channel 创建新图像?

标签 python rgb python-imaging-library yuv

我有两个图像,内容生成。我想使用生成的Y channel 和内容的U和V channel 创建一个新图像。有了 PIL,我认为我应该能够使用 .convert('YCbCr') 将 RGB 输入图像转换为 YUV。创建新图像后,我可以使用 .convert('RGB') 将其转换为 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

将 channel 组合成新图像的最佳/最有效方法是什么?

<小时/>

这是我采用的解决方案:

# 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_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 个 channel 的数据:

(256, 256, 3)
(256, 256, 3)

我假设 0 索引 channel 是 Y channel ,如果不是,则根据需要将魔数(Magic Number) 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.

关于python - 如何从一张图像的 Y channel 和另一张图像的 U、V channel 创建新图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50438752/

相关文章:

java - java中JPG图像的像素的精确值

python - PIL 将 png 像素导入为单个值而不是 3 个值向量

python - 无法将文本与 PIL 文本大小正确对齐

python - matplotlib GridSpec 中的行标题

python - 带有切片的 Q 网络损失的 Tensorflow 实现

rgb - 无损 RGB 到 Y'CbCr 转换

python - 在 python 中屏蔽然后粘贴两个图像

python - 帮助屏幕中忽略了 argparse 的 "required"?

python - 使用 Beautiful Soup 获取所有 HTML 标签

android - Color.rgb 在 Android 中有什么作用?