简体   繁体   English

拍摄ImageChops后保存为PNG(使用PIL库)。两个PNG文件的差异将生成透明(或白色)的PNG图像文件

[英]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,. 在此代码中,我要获取两个相同分辨率的PNG图像的差异,然后保存差异。 Saving in JPEG works fine but in PNG, it produces a total tranparent PNG image file. 保存为JPEG效果很好,但在PNG中保存时,会生成一个总的透明PNG图像文件。 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]

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 ). 也许您的原始图像具有Alpha通道(RGBA),您应该事先知道它,并且/或者检查结果Image.open生成的图像类型(查看Image.modeImage.info )。 Anyway, you can force the RGB type (no alpha channel) by calling <image>.convert('RGB') , before or after doing the difference. 无论如何,您可以在进行差异之前或之后通过调用<image>.convert('RGB')来强制RGB类型(无Alpha通道)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM