Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving in PNG (using PIL library) after taking ImageChops.difference of two PNG files is producing transparent (or white) PNG image file

Here in this code I am taking difference of two same resolution PNG images ,then saving the difference,. Saving in JPEG works fine but in PNG, it produces a total tranparent PNG image file. look at the comments in the last two lines

import Image
import ImageChops
js_black_im = Image.open("/js_black.png")
js_white_im = Image.open("/fb_white.png")
diff_im = ImageChops.difference(js_black_im, js_white_im)
diff_im.save("/js_onlytext.jpeg", "JPEG") #this works as expected
diff_im.save("/js_onlytext.png", "PNG") #this produces a total tranparent PNG image file![js_black.png][1]![fb_black.png][2]
like image 803
Alok Nayak Avatar asked Oct 22 '25 05:10

Alok Nayak


1 Answers

Perhaps your original images have an alpha channel (RGBA), you should know that beforehand, and/or check the result the image type that Image.open produces (looking at Image.mode or Image.info). Anyway, you can force the RGB type (no alpha channel) by calling <image>.convert('RGB'), before or after doing the difference.

like image 73
leonbloy Avatar answered Oct 23 '25 18:10

leonbloy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!