2021-07-25 13:57:07 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2023-02-20 21:57:43 -06:00
|
|
|
# This is a script to paste small scanned ID cards in an A4 page for printing
|
|
|
|
# I use it to quickly organize the pages for printing
|
2021-07-25 13:57:07 -05:00
|
|
|
# Syntax : script.py filename width_in_cm -o [optional, to overwrite output file]
|
|
|
|
|
2023-02-20 21:57:43 -06:00
|
|
|
import sys
|
|
|
|
import os
|
2021-07-25 13:57:07 -05:00
|
|
|
from pdf2image import convert_from_path
|
|
|
|
from PIL import Image
|
|
|
|
|
2023-02-20 21:57:43 -06:00
|
|
|
if len(sys.argv) < 3:
|
|
|
|
raise Exception(
|
|
|
|
'Not enough arguments.\nUse the syntax "script.py filename width_in_cm -o [optional, to overwrite output file]"')
|
2021-07-25 13:57:07 -05:00
|
|
|
filename = sys.argv[1]
|
|
|
|
if not os.path.isfile(filename):
|
2023-02-20 21:57:43 -06:00
|
|
|
raise Exception('File {} not present', format(filename))
|
|
|
|
if filename.split('.')[-1] != 'pdf':
|
2021-07-25 13:57:07 -05:00
|
|
|
raise Exception('Given file is not a PDF')
|
2023-02-20 21:57:43 -06:00
|
|
|
if filename.split('/')[-1].split(']')[0] == '[printable':
|
|
|
|
raise Exception('File has already been processed')
|
2021-07-25 13:57:07 -05:00
|
|
|
print('Processing '+filename.split('/')[-1])
|
2023-02-20 21:57:43 -06:00
|
|
|
# converting to inch from cm at 300 dpi
|
|
|
|
new_width = int(int(sys.argv[2])*0.3937*300)
|
2021-07-25 13:57:07 -05:00
|
|
|
overwrite_status = '-n'
|
2023-02-20 21:57:43 -06:00
|
|
|
if len(sys.argv) > 3:
|
2021-07-25 13:57:07 -05:00
|
|
|
overwrite_status = sys.argv[3]
|
|
|
|
images = convert_from_path(filename)
|
|
|
|
a4size = int(8.27*300), int(11.7*300)
|
2023-02-20 21:57:43 -06:00
|
|
|
a4page = Image.new('RGB', a4size, 'white') # A4 at 300 dpi
|
2021-07-25 13:57:07 -05:00
|
|
|
pagenum = len(images)
|
|
|
|
widths = [images[i].size[0] for i in range(pagenum)]
|
|
|
|
heights = [images[i].size[1] for i in range(pagenum)]
|
|
|
|
new_heights = [int(heights[i]*new_width/widths[i]) for i in range(pagenum)]
|
|
|
|
gap = int(max(new_heights)/pagenum)
|
|
|
|
total_height = sum(new_heights)+gap*(pagenum-1)
|
|
|
|
if new_width > a4size[0] or total_height > a4size[1]:
|
2023-02-20 21:57:43 -06:00
|
|
|
raise Exception('Given width is too big for A4 page')
|
2021-07-25 13:57:07 -05:00
|
|
|
for i in range(pagenum):
|
2023-02-20 21:57:43 -06:00
|
|
|
a4page.paste(images[i].resize((new_width, new_heights[i]), Image.LANCZOS), (int(
|
|
|
|
(a4size[0]-new_width)/2), int((a4size[1]-total_height)/2+i*(new_heights[i-1]+gap))))
|
2021-07-25 13:57:07 -05:00
|
|
|
new_filename = '[printable] ' + filename.split('/')[-1].split('.')[0] + '.pdf'
|
|
|
|
if os.path.isfile(new_filename) and overwrite_status != '-o':
|
2023-02-20 21:57:43 -06:00
|
|
|
raise Exception('File '+new_filename +
|
|
|
|
' is already present. Add -o at the end to overwrite')
|
2021-07-25 13:57:07 -05:00
|
|
|
a4page.save(new_filename, quality=95)
|
|
|
|
print('File '+new_filename+' created.')
|