You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
4.0 KiB
Python
94 lines
4.0 KiB
Python
#! /usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
import os, tempfile, shlex, subprocess
|
|
from argparse import ArgumentParser
|
|
from random import shuffle
|
|
|
|
####
|
|
# Arguments
|
|
# TODO: add example to description
|
|
####
|
|
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("--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()
|
|
|
|
temp_dir = tempfile.mkdtemp()
|
|
|
|
# dimensions
|
|
args.size = (args.size).upper()
|
|
global size_sheet
|
|
if args.size == 'A4':
|
|
size_sheet = (3508, 2480) #landscape: widthxheight in px
|
|
elif args.size == 'A3':
|
|
size_sheet = (4960, 3508) #landscape: widthxheight in px
|
|
size_pages = (size_sheet[0]/4,size_sheet[1]/2 )# size of each of the pages which makes up the printing sheet
|
|
|
|
# image selection
|
|
listdir = os.listdir( os.path.abspath(args.dir))
|
|
listdir = [f for f in listdir if any (x in f for x in ['.jpeg','.jpg','.png','.JPEG','.JPG','.PNG', '.Jpg', '.Jpeg','.Png']) ] #only bitmaps allowed
|
|
|
|
# create list of 8 ordered images
|
|
global listdir_order
|
|
if args.order == 'alphabet':
|
|
listdir.sort()
|
|
listdir_order = listdir
|
|
elif args.order == 'reverse':
|
|
listdir.sort()
|
|
listdir.reverse()
|
|
listdir_order = listdir
|
|
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 'selected files according to pagenumbers:', listdir_order
|
|
|
|
# image conversion: -extent
|
|
tmpfiles = [ (temp_dir +'/'+ str(n)+'.jpg') for n, img in enumerate(listdir_order) ]
|
|
for n, img in enumerate(listdir_order):
|
|
template_convert = 'convert -background white "{targetimg}" -gravity Center -resize {w}x{h} -extent {w}x{h} {tmpfile}'
|
|
cmd_convert = template_convert.format(targetimg=(os.path.abspath(args.dir))+'/'+img,
|
|
w=size_pages[0],
|
|
h=size_pages[1],
|
|
tmpfile=tmpfiles[n])
|
|
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)
|
|
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,
|
|
output = tmp_output)
|
|
cmd_montage_l = shlex.split(cmd_montage)
|
|
subprocess.call(cmd_montage_l)
|
|
|
|
# resize output
|
|
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,
|
|
w=size_sheet[0],
|
|
h=size_sheet[1],
|
|
output=output_f)
|
|
cmd_convert_l = shlex.split(cmd_convert)
|
|
subprocess.call(cmd_convert_l)
|
|
|
|
print '** imposition save in', output_f, '**'
|
|
|
|
# garbage collection
|
|
cmd_rm = shlex.split( ('rm -rf '+ temp_dir) )
|
|
subprocess.call(cmd_rm ) # rm temp dir
|