当我把一个pdf格式转换成jpeg格式时,我发现了一个非常有线的东西,所以我想知道这可能是一个小bug。看下面转换后的jpg,你会发现,背景颜色都是黑色的。图片在这里: www.shdowin.com/public/02.jpg
但是,在pdf的源文件中,您可以看到背景色为正常白色。图片在这里:www.shdowin.com/public/Norm.jpg
我想这可能是我的pdf文件的错误,然而,当我尝试在.NET环境中使用Acrobat.pdf2image时,转换后的jpg显示正确。
下面是我的代码:
from wand.image import Image
from wand.color import Color
import os, os.path, sys
def pdf2jpg(source_file, target_file, dest_width, dest_height):
RESOLUTION = 300
ret = True
try:
with Image(filename=source_file, resolution=(RESOLUTION,RESOLUTION)) as img:
img.background_color = Color('white')
img_width = img.width
ratio = dest_width / img_width
img.resize(dest_width, int(ratio * img.height))
img.format = 'jpeg'
img.save(filename = target_file)
except Exception as e:
ret = False
return ret
if __name__ == "__main__":
source_file = "./02.pdf"
target_file = "./02.jpg"
ret = pdf2jpg(source_file, target_file, 1895, 1080)对这个问题有什么建议吗?
我已将pdf文件上载至网址:02.pdf
你可以试试..。
发布于 2017-10-07 02:43:36
对于其他仍然有这个问题的人,我在谷歌上搜索并尝试了几个小时后,通过使用这两行代码解决了这个问题,感谢这个问题https://stackoverflow.com/a/40494320/2686243:
img.background_color = Color("white")
img.alpha_channel = 'remove'已尝试使用Wand 0.4.4版
发布于 2013-12-19 08:44:17
我自己找到了答案。是因为alpha_channel的案子。这个pdf包含一些透明的背景(在我转换为png格式后),并且为了调整大小,ImageMagick选择了最好的调整大小过滤器,所以黑色背景显示。
因此,经过多次实验,我发现只需在"with“语句中添加"img.alpha_channel=False”(在img.save()之前),就可以正常工作。
感谢VadimR的建议,它是有帮助的。
发布于 2014-12-08 02:44:32
一个简单的解决方案是更改命令的顺序:先将格式更改为jpeg,然后再更改大小
img.format = 'jpeg'
img.resize(dest_width, int(ratio * img.height))通过分辨率元组以精确大小打开PDF也是非常容易的,因为分辨率可以是浮点数。
https://stackoverflow.com/questions/20439234
复制相似问题