I have two images, content
and generated
. I want to create a new image with the Y channel of generated
and the U and V channels of content
. With PIL, I think that I should be able to use .convert('YCbCr')
to convert my RGB input images to YUV. Then after I have created the new image, I can convert that to RGB with .convert('RGB')
.
The images enter the function below in an RGB format:
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
What is the best/most efficient way to combine the channels into a new image?
Here's the solution I went with:
# 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')