gpt4 book ai didi

python - 图像库从 JPEG 转换为

转载 作者:太空狗 更新时间:2023-10-29 22:04:17 25 4
gpt4 key购买 nike

我正在尝试将 200 dpi 的 JPEG 文件转换为 PDF 文件,但是,当我将文件另存为 PDF 时,我认为它会将 dpi 更改为 72,从而使图像变大。最初尝试将我的 JPEG 图像缩放到较小尺寸时,我遇到了类似的问题,并且能够通过在保存图像时指定 dpi 来解决这个问题。

im = Image.open("Image.jpg")

dpi=im.info['dpi']

if im.size == (2592, 1728):
out = im.resize((1188,792), Image.ANTIALIAS)
elif im.size == (1728,2592):
out = im.resize((792,1188), Image.ANTIALIAS)

out.save(project, dpi=dpi)

现在,当我尝试将此 JPEG 保存为 PDF 时,指定 dpi 似乎没有任何区别,而且我得到的图像比原始图像大,看起来 dpi 较低。使用 PIL 从 JPEG 转换为 PDF 时,有没有办法保持一致的分辨率?还是有更好的方法让我去做这件事?

这是我将文件目录从 JPEG 转换为 PDF 的方法:

for infile in listing:

outfile = destpath + os.path.splitext(infile)[0] + ".pdf"
current = path + infile

if infile != outfile:
im = Image.open(current)
dpi=im.info['dpi']

im.save(outfile, "PDF", Quality = 100)

最佳答案

PIL 1.1.7 来源的 CHANGES 文件中,您可以阅读:

  • Added resolution save option for PDF files.

    Andreas Kostyrka writes: I've included a patched PdfImagePlugin.py
    based on 1.1.6 as included in Ubuntu, that supports a "resolution"
    save option. Not great, but it makes the PDF saving more useful by
    allowing PDFs that are not exactly 72dpi.

所以你应该能够做到:

im.save(outfile, "PDF", resolution=100.0)

(似乎在我的 Ubuntu 机器上运行良好)。

关于python - 图像库从 JPEG 转换为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12626654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com