Python Wand converts from PDF to JPG background is incorrect

梦想的初衷 提交于 2019-11-28 11:38:21

An easy solution is to change the order of commands: Change the format to jpeg first and then to resize

        img.format = 'jpeg'
        img.resize(dest_width, int(ratio * img.height))

It is also very easy to open the PDF in the exact size by the resolution tuple, because the resolution can be a float number.

For others who still have this problem I fixed it after googling and trying a couple of hours thanks to this question https://stackoverflow.com/a/40494320/2686243 by using this two lines:

img.background_color = Color("white")
img.alpha_channel = 'remove'

Tried with Wand version 0.4.4

I got the answer by myself. It's because of the alpha_channel case. This pdf includes some transparent background(after i transfomred to png format), and for resize, ImageMagick choose the best resize filter, so black background displayed.

So, after a lot of experiments, I found that just add "img.alpha_channel=False" in "with" statement(before img.save()), that would work properly.

Thanks for VadimR's advise, it is helpful.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!