Pillow/Scripts/pilprint.py

97 lines
2.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python
2010-07-31 06:52:47 +04:00
#
# The Python Imaging Library.
# $Id$
#
# print image files to postscript printer
#
# History:
# 0.1 1996-04-20 fl Created
# 0.2 1996-10-04 fl Use draft mode when converting.
# 0.3 2003-05-06 fl Fixed a typo or two.
#
from __future__ import print_function
2015-04-24 02:41:33 +03:00
import getopt
import os
import sys
2010-07-31 06:52:47 +04:00
VERSION = "pilprint 0.3/2003-05-05"
from PIL import Image
from PIL import PSDraw
2015-04-24 02:41:33 +03:00
letter = (1.0*72, 1.0*72, 7.5*72, 10.0*72)
2010-07-31 06:52:47 +04:00
2015-04-02 11:45:24 +03:00
def description(filepath, image):
title = os.path.splitext(os.path.split(filepath)[1])[0]
2010-07-31 06:52:47 +04:00
format = " (%dx%d "
if image.format:
format = " (" + image.format + " %dx%d "
return title + format % image.size + image.mode + ")"
if len(sys.argv) == 1:
print("PIL Print 0.2a1/96-10-04 -- print image files")
print("Usage: pilprint files...")
print("Options:")
print(" -c colour printer (default is monochrome)")
print(" -p print via lpr (default is stdout)")
print(" -P <printer> same as -p but use given printer")
2010-07-31 06:52:47 +04:00
sys.exit(1)
try:
opt, argv = getopt.getopt(sys.argv[1:], "cdpP:")
except getopt.error as v:
print(v)
2010-07-31 06:52:47 +04:00
sys.exit(1)
2015-04-24 02:41:33 +03:00
printer = None # print to stdout
monochrome = 1 # reduce file size for most common case
2010-07-31 06:52:47 +04:00
for o, a in opt:
if o == "-d":
# debug: show available drivers
Image.init()
print(Image.ID)
2010-07-31 06:52:47 +04:00
sys.exit(1)
elif o == "-c":
# colour printer
monochrome = 0
elif o == "-p":
# default printer channel
printer = "lpr"
elif o == "-P":
# printer channel
printer = "lpr -P%s" % a
2015-04-02 11:45:24 +03:00
for filepath in argv:
2010-07-31 06:52:47 +04:00
try:
2015-04-02 11:45:24 +03:00
im = Image.open(filepath)
2010-07-31 06:52:47 +04:00
2015-04-02 11:45:24 +03:00
title = description(filepath, im)
2010-07-31 06:52:47 +04:00
if monochrome and im.mode not in ["1", "L"]:
im.draft("L", im.size)
im = im.convert("L")
if printer:
fp = os.popen(printer, "w")
else:
fp = sys.stdout
ps = PSDraw.PSDraw(fp)
ps.begin_document()
ps.setfont("Helvetica-Narrow-Bold", 18)
ps.text((letter[0], letter[3]+24), title)
ps.setfont("Helvetica-Narrow-Bold", 8)
ps.text((letter[0], letter[1]-30), VERSION)
ps.image(letter, im)
ps.end_document()
except:
print("cannot print image", end=' ')
print("(%s:%s)" % (sys.exc_info()[0], sys.exc_info()[1]))