Python: Make transparent areas in PIL image to white (or any other solid opaque colour)

I had some issues with transparent PNG images causing some strange effects when resized and saved with Python's PIL Image library.

image image

image image

As you can see, those jarring effects are hard to ignore.

The issue was I was ignoring the alpha channel during conversion and PIL didn't quite know what to do with it.

It is a bit more work, but the results speak for themselves. Even the GIF image quality was improved after using this method!

image image
You'll have top view them at 100% to see the difference.

Update 6/12/14:

I found a better way of colouring the images which had NO loss in quality (updated code above)

345_shirt_438cef451b28326350794f77d38fe83d_B 345_shirt_438cef451b28326350794f77d38fe83d

(Shirt design "Majin Power!" by ddjvigo)

You can easily see which one is rendered with the new code as the difference is quite noticeable.

There was a bug which replaced the whole pixel with white, which is wrong because it could be any other colour with transparency < 255.

(Here's the old code if anyone was wondering)

Sources

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog