缩放图像以适合A3纸张尺寸的蟒蛇

时间:2014-03-13 05:13:01

标签: python-2.7 python-imaging-library

我有一系列图片。我想把它们中的5个放在1页(A3大小)中以放入报告中。 我的图像尺寸为(10,2.7),dpi = 300。

我的脚本如下

import numpy
from PIL import Image
well_name_list = []
images_list = []
inputfilename = 'wells.csv'

with open (inputfilename,'r') as f:
    for line in f:
        line=line.strip('\n')
        well = line
        if not well in well_name_list:
            well_name_list.append(well)

for j in range (0,len(well_name_list)):
    images = well_name_list[j]+'.png'
    images_list.append(images)

width, height = int(11.69*300),int(16.53*300)

groups = [images_list[i:i+5] for i in range(0,len(images_list),5)]

for i, group in enumerate(groups):
    page = Image.new('RGB',(width,height),'white')
    page.paste(Image.open(group[0]),box=(0,0))
    page.paste(Image.open(group[1]), box=(0,int(height/5.+.1)))
    page.paste(Image.open(group[2]),box=(0,int(2*height/5.+.1)))
    page.paste(Image.open(group[3]), box=(0,int(3*height/5.+.1)))
    page.paste(Image.open(group[4]), box=(0,int(4*height/5.+.1)))
    page.save('page{}.pdf'.format(i))

我获得的是比A3尺寸大得多的pdf页面。有人可以帮我解决这个问题吗?

非常感谢,

1 个答案:

答案 0 :(得分:0)

您需要在resolution调用中添加一个未记录的save参数。由于它没有记录,我不知道默认是什么,但我怀疑它是72 DPI。如果你把它设置为300,它应该是正确的大小。

page.save('page{}.pdf'.format(i), resolution=300)
相关问题