1
0
Fork 0
mirror of https://github.com/SinTan1729/random.git synced 2024-12-25 12:58:35 -06:00

Bug fixes and improvements

This commit is contained in:
Sayantan Santra 2023-02-20 21:57:43 -06:00
parent 37d8af2252
commit c7344c4971

View file

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