图像库从JPEG转换为 [英] Image Library convert from JPEG to
问题描述
我正在尝试将带有200 dpi的JPEG文件转换为PDF文件,但是,当我将文件另存为PDF时,我认为它将dpi更改为72,从而使图像更大。我最初尝试将JPEG图像缩放到较小的尺寸时遇到了类似的问题,并且能够通过在保存图像时指定dpi来解决这个问题。
I'm attempting to convert a JPEG file with, 200 dpi, to a PDF file, however, when I save the file as a PDF I think it's changing the dpi to 72, and thus making the image larger. I had a similar problem when initially trying to scale my JPEG image to a smaller size, and was able to solve that by specifying the dpi when I save the image.
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时,有没有办法保持一致的分辨率?或者有更好的方法让我这样做吗?
Now when I try to save this JPEG as a PDF, specifying the dpi doesn't seem to make any difference, and I get an image that is larger than my original that looks like it has a lower dpi. Is there a way to mantain a consistent resolution when converting from JPEG to PDF using PIL? Or is there a better way for me to go about doing this?
这就是我将文件目录从JPEG转换为PDF的原因:
This is what I have for converting a directory of files from JPEG to 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文件中,可以读取:
In the CHANGES file of PIL 1.1.7
sources one can read:
为PDF文件添加了分辨率保存选项。
Added resolution save option for PDF files.
Andreas Kostyrka写道:我' ve包括一个修补的PdfImagePlugin.py
,基于1.1.6,包含在Ubuntu中,支持分辨率
保存选项。不是很好,但它使PDF保存更有用,因为
允许不完全是72dpi的PDF。
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.
所以你应该可以这样做:
So you should be able to do:
im.save(outfile, "PDF", resolution=100.0)
(似乎在我的Ubuntu盒子上工作正常)。
(seems to work fine on my Ubuntu box).
这篇关于图像库从JPEG转换为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!