Reputation: 2229
I'm working on the image processing script(Python
with PIL
library) and i need to convert color space of any image to RGB
. I've tried this trick, but it works only with png
images in RGBa
color space:
image = Image.open(imageFile)
image.load()
# replace alpha channel with white color
self.im = Image.new('RGB', image.size, (255, 255, 255))
self.im.paste(image, mask=image.split()[3])
How to make this code universally to all images in any colorspace?
Thanks.
Upvotes: 3
Views: 15316
Reputation: 2229
Found solution:
image = Image.open(imageFile)
image.load()
# replace alpha channel with white color
self.im = Image.new('RGB', image.size, (255, 255, 255))
self.im.paste(image, None)
in the self.im
variable will be stored an image in RGB colorspace with white(255, 255, 255) alpha channel.
Upvotes: 2
Reputation: 389
Do you only want to use PIL? I would suggest the openCV version 2 python bindings cv2
http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html#cv2.cvtColor
It has many possible conversions between the most common colourspaces.
If you dont want openCV
you can use skimage
http://scikit-image.org/docs/dev/api/skimage.color.html#convert-colorspace
Upvotes: 0