简体   繁体   English

无法使用PIL将图像从PNG转换为JPG(Python图像库)

[英]Unable to convert an image from PNG TO JPG using PIL (Python Image Library)

I use weasyprint library in python to convert an HTML template to png. 我在python中使用weasyprint库将HTML模板转换为png。 Then I am trying to convert the png image to jpg using a python library named PIL(PILLOW). 然后我试图使用一个名为PIL(PILLOW)的python库将png图像转换为jpg。

The PNG IMAGE is given below. PNG图片如下。 在此处输入图片说明

But the image obtained after conversion using PIL is not what I was expecting. 但是使用PIL转换后获得的图像不是我所期望的。 The colors are lost and only some components of the image are visible. 颜色丢失,只有图像的某些部分可见。

        from PIL import Image
        img = Image.open(file_path)
        rgb_im = img.convert('RGB')

        jpg_img_path = os.path.splitext(file_path)[0]
        jpg_img_path += '.jpg'
        rgb_im.save(jpg_img_path)

When I tried to use an online editor, they have provided me the perfect image from the png. 当我尝试使用在线编辑器时,他们为我提供了png的完美图像。 Below is the jpg image obtained from the online converter . 下面是从在线转换器获得的jpg图像 在此处输入图片说明

And the Image when I have used PIL . 还有使用PIL时的图像。 在此处输入图片说明

This could be completely wrong, but I suspect that what happens is that your HTML renderer gives you an alpha-channel PNG where the background is transparent, which then gets flattened to black by .convert(). 这可能是完全错误的,但是我怀疑发生的事情是您的HTML渲染器为您提供了一个Alpha通道的PNG,背景为透明,然后通过.convert()变平为黑色。 If I am right, the problem should go away if you create an all-white image (or whatever background you prefer) of the same size as your PNG, and composite the PNG over that before convert() and save(). 如果我是对的,那么如果您创建与PNG大小相同的全白图像(或您喜欢的背景),并在convert()和save()之前将PNG合成,则问题应该消失。 (There might be some smarter way of doing this that does not require you to actually create a complete background image, but this should do as a test). (执行此操作可能有一些更聪明的方法,该方法不需要您实际创建完整的背景图像,但这应该作为测试)。

The answer of @Ture Palsson is correct, it is an alpha problem. @Ture Palsson的答案是正确的,这是一个alpha问题。 You can try to get rid of it using some PIL workaround, as shown here , or you use some very simple skimage code, that I would prefer: 您可以尝试使用一些PIL解决办法摆脱它,如图所示这里 ,或者你使用一些非常简单的skimage代码,那我宁愿:

from skimage.io import imread, imsave
from skimage.color import rgba2rgb
png = imread('UCLgy.png')
imsave('UCLgy.jpg', rgba2rgb(png, (1,1,1))) # 1, 1, 1 is white

The issue is that the original PNG image has an alpha layer, ie transparency, which JPEG doesn't support. 问题在于原始的PNG图像具有Alpha层,即JPEG不支持的透明度。 The easiest thing to do is to make a new image the same size as the original image but filled with white, then paste the transparent image on top: 最简单的方法是使一个新图像的大小与原始图像相同,但用白色填充,然后将透明图像粘贴到顶部:

#!/usr/bin/env python3

from PIL import Image

# Open image
im = Image.open('image.png')

# Make a background, same size filled with solid white
result = Image.new('RGB', (im.width,im.height), color=(255,255,255))

# Paste original image over white background and save
result.paste(im,im)
result.save('result.jpg')

Thus just uses the same modules as you already have without introducing any new dependencies. 因此,仅使用与您已有的模块相同的模块,而不会引入任何新的依赖项。

您可以使用imgkit python库将HTML直接转换为png

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

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