split-picture-printable.py

Aus Wiki
Wechseln zu: Navigation, Suche
  • Alternative: pdfposter
#!/usr/bin/python2.6
# -*- coding: iso-8859-15 -*-
 
import sys, os
from PIL import Image
from threading import Thread
from subprocess import Popen
from shlex import split
 
filename = sys.argv[1]
pages = 4
format = 'A4'
resize = 'scale'	# expand canvas / scale
 
im = Image.open(filename)
 
# rotate, such that x is the longer edge
 
if im.size[1] > im.size[0]:
	print 'y is the longer edge. Rotating ...'
	im = im.rotate(90)
 
 
# resize image to fit specified page format
 
if format == 'A4':
	aspect = 297/210.
image_aspect = im.size[0] / float(im.size[1])
 
print 'Image aspect ratio: '+str(image_aspect)
print 'Desired aspect ratio: '+str(aspect)+' ('+format+')'
 
newwidth = im.size[0]
newheight = im.size[1]
if image_aspect > aspect:	# x > desired x => increase height
	newheight = int(newheight * image_aspect / aspect)
elif image_aspect < aspect:	# x < desired x => increase width
	newwidth = int(newwidth * aspect / image_aspect)
 
# make transparent areas white
 
if im.mode == "RGBA" and False:		# disabled, PIL seems to have a problem with transparency ...
	print 'Making transparent areas white ...'
	pixel_data = im.load()
 
	def transparency2white(y0, y1):
		for y in xrange(y0, y1):
			for x in xrange(im.size[0]):
				if pixel_data[x, y][3] < 255:
					pixel_data[x, y] = (255, 255, 255, 255)	
 
	stop1 = im.size[1]/4
	stop2 = 2*stop1
	stop3 = 3*stop1
 
	thread1 = Thread( target=transparency2white, args=(0,stop1), kwargs={} )
	thread2 = Thread( target=transparency2white, args=(stop1,stop2), kwargs={} )
	thread3 = Thread( target=transparency2white, args=(stop2,stop3), kwargs={} )
	thread4 = Thread( target=transparency2white, args=(stop3,im.size[1]), kwargs={} )
 
	thread1.start()
	thread2.start()
	thread3.start()
	thread4.start()
 
	thread1.join()
	thread2.join()
	thread3.join()
	thread4.join()
 
# resize
 
print 'Resizing ('+str(im.size[0])+','+str(im.size[1])+') -> ('+str(newwidth)+','+str(newheight)+') ...'
if resize == 'scale':
	im = im.convert('RGB').resize( (newwidth,newheight), Image.ANTIALIAS )	# scales the image
else:
	new = Image.new('RGBA', (newwidth,newheight), (255, 255, 255))
	pos = ( (newwidth-im.size[0])/2, (newheight-im.size[1])/2 )
	new.paste( im, pos )
	im = new.convert('RGB').convert('P', palette=Image.ADAPTIVE)
 
# split picture
 
if pages == 4:
	mx = newwidth/2
	my = newheight/2
	print 'Cropping tiles ...'
 
	def crop( box, filename ):
		tile = im.crop(box)
		tile.info['transparency'] = 1
		tile.info['dpi'] = (300,300)
		tile.save( filename, **tile.info )
 
	# using multiple CPU cores
 
	if os.path.exists('splitted.pdf'):
		os.remove('splitted.pdf')
 
	thread1 = Thread( target=crop, args=( (0,0,mx,my),			'tile1.pdf' ), kwargs={} )
	thread2 = Thread( target=crop, args=( (mx+1,0,newwidth,my),		'tile2.pdf' ), kwargs={} )
	thread3 = Thread( target=crop, args=( (0,my+1,mx,newheight),		'tile3.pdf' ), kwargs={} )
	thread4 = Thread( target=crop, args=( (mx+1,my+1,newwidth,newheight),	'tile4.pdf' ), kwargs={} )
 
	thread1.start()
	thread2.start()
	thread3.start()
	thread4.start()
 
	thread1.join()
	thread2.join()
	thread3.join()
	thread4.join()
 
	print 'Concatenating PDF tiles to print.pdf ...'
	Popen(split('pdftk tile1.pdf tile2.pdf tile3.pdf tile4.pdf cat output splitted.pdf')).wait()
	if os.path.exists('splitted.pdf'):
		os.remove('tile1.pdf')
		os.remove('tile2.pdf')
		os.remove('tile3.pdf')
		os.remove('tile4.pdf')
 
		#if gnome
		#if kde
		print 'Open in PDF viewer ...'
		Popen(split('evince splitted.pdf'))
Meine Werkzeuge
Namensräume
Varianten
Aktionen
Navigation
Werkzeuge