imposition with 1 or 2 pages

master
Castro0o 7 years ago
parent 9ca1c96787
commit f44f3d776c

@ -11,10 +11,11 @@ from random import shuffle
p = ArgumentParser(description='Script converts 8 images (jpg or png) from a given directory into a single page 4x2 imposition. Example: python imposition.py --dir imgs/ --size a3 --order random ')
p.add_argument("--dir", metavar='', default='', required=True, help="Image directory, which stores the source images for the imposition")
p.add_argument("--size", metavar='', default="A4", choices=['A4','a4','A3','a3'], help="Size of the printing sheet: A4 or A3. Default:A4")
p.add_argument("--pages", metavar='', type=int, choices=[1,2], default=1, help="Number of imposition pages. Default:1. Currently only 1 or 2 pages impositions are available.")
p.add_argument("--order", metavar='', default="alphabet", choices=["alphabet", "reverse", "random"], help="Image distribution Order in the imposition. Possible Values: alphabet (alphabetically) , reverse (reverse alphabetically), random. Default: alphabet")
p.add_argument("--output", metavar='' ,default="output.pdf", help="Output file. Can either be a pdf, jpg or png. Default: output.pdf")
args = p.parse_args()
print args
temp_dir = tempfile.mkdtemp()
# dimensions
@ -43,10 +44,15 @@ elif args.order == 'random':
shuffle(listdir)
listdir_order = listdir
print listdir_order
listdir_order = listdir_order[0:8]
pagenumbers = [2,5,4,3,1,6,7,0] # from from top left to bottom right ( order in imagemagick creates mosaic)
listdir_order = [listdir_order[n] for n in pagenumbers] # reorder according to pagenumbers
# print listdir_order
if args.pages == 1:
listdir_order = listdir_order[0:8]
pagenumbers = [2,5,4,3,1,6,7,0] # from from top left to bottom right ( order in imagemagick creates mosaic)
elif args.pages == 2:
listdir_order = listdir_order[0:16]
pagenumbers = [4,11,8,7,3,12,15,0,6,9,10,5,1,14,13,2]
listdir_order = [listdir_order[n] for n in pagenumbers] # reorder files according to pagenumbers
print 'selected files according to pagenumbers:', listdir_order
# image conversion: -extent
@ -60,30 +66,39 @@ for n, img in enumerate(listdir_order):
cmd_convert_l = shlex.split(cmd_convert)
subprocess.call(cmd_convert_l)
# montage
## order & rotation
tmpfiles_top = tmpfiles[0:4] # top row will be rotated in the mosaic
tmpfiles_top = ' '.join(tmpfiles_top)
tmpfiles_bottom = tmpfiles[4:8]
tmpfiles_bottom = ' '.join(tmpfiles_bottom)
# # montage
tmp_output = (temp_dir +'/'+'output.jpg')
# command
template_montage = "montage -geometry +1+1 -tile 4x2 \( -rotate 180 {topfiles} \) {bottomfiles} {output}"
cmd_montage = template_montage.format(topfiles=tmpfiles_top,
bottomfiles=tmpfiles_bottom,
if args.pages == 1:
template_montage = "montage -geometry +1+1 -tile 4x2 \( -rotate 180 {topfiles_p1} \) {bottomfiles_p1} {output}"
cmd_montage = template_montage.format(
topfiles_p1=' '.join(tmpfiles[:4]),
bottomfiles_p1=' '.join(tmpfiles[4:8]),
output = tmp_output)
elif args.pages == 2:
template_montage = "montage -geometry +1+1 -tile 4x2 \( -rotate 180 {topfiles_p1} \) {bottomfiles_p1} \( -rotate 180 {topfiles_p2} \) {bottomfiles_p2} {output}"
cmd_montage = template_montage.format(
topfiles_p1=' '.join(tmpfiles[:4]),
bottomfiles_p1=' '.join(tmpfiles[4:8]),
topfiles_p2=' '.join(tmpfiles[8:12]),
bottomfiles_p2=' '.join(tmpfiles[12:]), output = tmp_output)
print 'Create montage cmd {}: '.format( cmd_montage )
cmd_montage_l = shlex.split(cmd_montage)
subprocess.call(cmd_montage_l)
# resize output
# # resize output
tmp_output_files =[ (temp_dir+'/'+f) for f in (os.listdir(temp_dir)) if 'output' in f] #if more than 1 files results from montage
tmp_output_files = " ".join(tmp_output_files) # make into string
output_f = os.path.abspath('./'+args.output)
template_convert = "convert {input_f} -resize {w}x{h} -units PixelsPerInch -density 300x300 {output}"
cmd_convert = template_convert.format(
input_f= tmp_output,
input_f= tmp_output_files,
w=size_sheet[0],
h=size_sheet[1],
output=output_f)
cmd_convert_l = shlex.split(cmd_convert)
print 'Convert montage to {} cmd: {}'.format(args.output, cmd_convert)
subprocess.call(cmd_convert_l)
print '** imposition save in', output_f, '**'

Loading…
Cancel
Save