Domanda

I have a series of images. I want to put 5 of them in 1 page (A3 size) to put in the report. My image size is (10,2.7), dpi=300.

My script is as follow

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))

What I obtained is pdf pages which are much larger than A3 size. Can anyone please help me to fix this?

Many thanks,

È stato utile?

Soluzione

There is an undocumented resolution parameter that you need to add to the save call. Since it's undocumented I don't know what the default is, but I suspect it's 72 DPI. If you set it to 300 it should come out the right size.

page.save('page{}.pdf'.format(i), resolution=300)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top