blountdj
blountdj

Reputation: 639

PIL simple image paste - image changing color

I'm trying to paste an image onto another, using:

original = Img.open('original.gif')
tile_img = Img.open('tile_image.jpg')
area = 0, 0, 300, 300
original.paste(tile_img, area)
new_cropped.show()

This works except the pasted image changes color to grey.

Image before:

enter image description here

Image after:

enter image description here

Is there a simple way to retain the same pasted image color? I've tried reading the other questions and the documentation, but I can't find any explanation of how to do this.

Many thanks

Upvotes: 4

Views: 1594

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207853

I believe all GIF images are palettised - that is, rather than containing an RGB triplet at each location, they contain an index into a palette of RGB triplets. This saves space and improves download speed - at the expense of only allowing 256 unique colours per image.

If you want to treat a GIF (or palettised PNG file) as RGB, you need to ensure you convert it to RGB on opening, otherwise you will be working with palette indices rather than RGB triplets.

Try changing the first line to:

original = Img.open('original.gif').convert('RGB')

Upvotes: 4

Related Questions