Python中文
首页
教程
问答
标签
搜索
登录
注册
Python魔杖转换PDF到JPG,背景是在
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我在将pdf转换为jpeg时发现了一个非常复杂的问题,所以我想弄清楚这可能是一个小错误。 看到下面转换的jpg,你会发现,背景色都是黑色的。 图片如下:www.shdowin.com/public/02.jpg</p> <p>但是,在pdf的源文件中,可以看到背景颜色是正常的白色。 图片如下:www.shdowin.com/public/normal.jpg</p> <p>我认为这可能是我的pdf文件的错误,然而,当我尝试在.NET环境中使用Acrobat.pdf2image时,转换后的jpg显示正确。</p> <p>这是我的代码:</p> <pre><code>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) </code></pre> <p>对这个问题有什么建议吗?</p> <p>我已将pdf上载到url: <a href="http://www.shdowin.com/public/02.pdf" rel="nofollow">02.pdf</a></p> <p>你可以试试。。。</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>我自己得到了答案。这是因为阿尔法通道的情况。这个pdf包含一些透明的背景(在我转换成png格式之后),对于resize,ImageMagick选择了最佳的resize过滤器,所以显示黑色背景。</p> <p>所以,经过大量实验,我发现只要在“with”语句中添加“img.alpha_channel=False”(在img.save()之前),就可以正常工作了。</p> <p>谢谢瓦迪姆的建议,这很有帮助。</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
将Pandas数据帧转换为PyTorch张量?
2 回答
将Pandas数据帧转换为scipy稀疏矩阵
1 回答
将Pandas数据帧转换为Spark Datafram时出现问题
7 回答
将pandas数据帧转换为spark DataFram时出错
1 回答
将Pandas数据帧转换为spark datafram时收到错误
1 回答
将Pandas数据帧转换为Spark数据帧
1 回答
将Pandas数据帧转换为Tensorflow数据
5 回答
将Pandas数据帧转换为tkinter obj
6 回答
将pandas数据帧转换为XML
9 回答
将Pandas数据帧转换为值sql语句
3 回答
将pandas数据帧转换为元组
6 回答
将pandas数据帧转换为元组列表
7 回答
将pandas数据帧转换为元组列表并删除所有pandas数据类型
1 回答
将pandas数据帧转换为具有头和数据类型的numpy数组
1 回答
将pandas数据帧转换为内存中的拼花地板,并将其加载到Python中的Hadoop中
9 回答
将pandas数据帧转换为内存中类似文件的对象?
9 回答
将Pandas数据帧转换为内存功能(&F)
6 回答
将pandas数据帧转换为列表列表
2 回答
将pandas数据帧转换为列表列表以输入到RNN
10 回答
将Pandas数据帧转换为单行DataFram
10 回答