Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: Printing images through Python

Reply
Thread Tools

RE: Printing images through Python

 
 
Tim Golden
Guest
Posts: n/a
 
      10-03-2003
> From: Kevin [mailto]
> Does ANYONE have Python code to print PIL images?


It just so happens that I've been going through the same bit of grief
recently. Depending on just how much control you want, I advocate two
approaches, with several others possible if I had more time to explore the,
frankly arcane, possibilities offered by the Win32 SDK:

1) Use ReportLab's PDF generator to embed the image. Get the ReportLab docs
(http://www.reportlab.com/docs/userguide.pdf) and search for InlineImage. If
you need a code example I can give you one, but it's pretty straightforward.
This has the advantage that you can then call whatever Acrobat viewer you
want in print mode (eg acrord32.exe /p) to give the user the chance to
select the printer, paper size etc. You can even get Acrobat to print using
defaults without asking the user anything (can't remember the switch).

2) If you want quick-and-dirty, make use of the fact that Device-Independent
Bitmaps are *much* easier to work with in Windows than the convention
HBITMAP variety. So (with minimal comments):

<code>
import win32ui
import win32gui
import win32con
from PIL import Image, ImageWin

#
# Constants for GetDeviceCaps
#
HORZRES = 8
VERTRES = 10

#
# Find the printer resolution to scale to
#
hDC = win32ui.CreateDC ()
hDC.CreatePrinterDC () # can optionally put a printer name in here
printer_resolution = hDC.GetDeviceCaps (HORZRES), hDC.GetDeviceCaps
(VERTRES)
print "printer resolution =", printer_resolution

#
# Open the bitmap and rotate because we know it's
# really landscape
#
bmp = Image.open ("screen.bmp")
print "original bitmap size =", bmp.size
bmp = bmp.rotate (90)
print "rotated bitmap size =", bmp.size

#
# Resize the image to fit the page but not to overflow
#
ratios = [1.0 * printer_resolution[0] / bmp.size[0], 1.0 *
printer_resolution[1] / bmp.size[1]]
print "ratios =", ratios
scale = min (ratios)
print "scale =", scale

#
# Create the printer document and send the page
#
hDC.StartDoc ("Test")
hDC.StartPage ()

dib = ImageWin.Dib (bmp)
scaled_size = [scale * i for i in bmp.size]
print "scaled bitmap size =", scaled_size
dib.draw (hDC.GetHandleOutput (), [0, 0] + scaled_size)

hDC.EndPage ()
hDC.EndDoc ()
</code>

HTH.
TJG

__________________________________________________ ______________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
__________________________________________________ ______________________

 
Reply With Quote
 
 
 
 
Kevin
Guest
Posts: n/a
 
      10-04-2003
Thanks for the help Tim, seems to work well! At least now that you've given
me a "working" example to start with I can build from there.

One problem I had though, just FYI:

ratios = [1.0 * printer_resolution[0] / bmp.size[0], 1.0 *
printer_resolution[1] / bmp.size[1]]

turned out that printer_resolution[1] was a function... have to do more
looking to figure out why. I just used [0] for both args and it was fine
(for now).

Thanks!

Kevin.

"Tim Golden" <> wrote in message
news:mailman.1065171306.2074.python-...
> > From: Kevin [mailto]
> > Does ANYONE have Python code to print PIL images?

>
> It just so happens that I've been going through the same bit of grief
> recently. Depending on just how much control you want, I advocate two
> approaches, with several others possible if I had more time to explore

the,
> frankly arcane, possibilities offered by the Win32 SDK:
>
> 1) Use ReportLab's PDF generator to embed the image. Get the ReportLab

docs
> (http://www.reportlab.com/docs/userguide.pdf) and search for InlineImage.

If
> you need a code example I can give you one, but it's pretty

straightforward.
> This has the advantage that you can then call whatever Acrobat viewer you
> want in print mode (eg acrord32.exe /p) to give the user the chance to
> select the printer, paper size etc. You can even get Acrobat to print

using
> defaults without asking the user anything (can't remember the switch).
>
> 2) If you want quick-and-dirty, make use of the fact that

Device-Independent
> Bitmaps are *much* easier to work with in Windows than the convention
> HBITMAP variety. So (with minimal comments):
>
> <code>
> import win32ui
> import win32gui
> import win32con
> from PIL import Image, ImageWin
>
> #
> # Constants for GetDeviceCaps
> #
> HORZRES = 8
> VERTRES = 10
>
> #
> # Find the printer resolution to scale to
> #
> hDC = win32ui.CreateDC ()
> hDC.CreatePrinterDC () # can optionally put a printer name in here
> printer_resolution = hDC.GetDeviceCaps (HORZRES), hDC.GetDeviceCaps
> (VERTRES)
> print "printer resolution =", printer_resolution
>
> #
> # Open the bitmap and rotate because we know it's
> # really landscape
> #
> bmp = Image.open ("screen.bmp")
> print "original bitmap size =", bmp.size
> bmp = bmp.rotate (90)
> print "rotated bitmap size =", bmp.size
>
> #
> # Resize the image to fit the page but not to overflow
> #
> ratios = [1.0 * printer_resolution[0] / bmp.size[0], 1.0 *
> printer_resolution[1] / bmp.size[1]]
> print "ratios =", ratios
> scale = min (ratios)
> print "scale =", scale
>
> #
> # Create the printer document and send the page
> #
> hDC.StartDoc ("Test")
> hDC.StartPage ()
>
> dib = ImageWin.Dib (bmp)
> scaled_size = [scale * i for i in bmp.size]
> print "scaled bitmap size =", scaled_size
> dib.draw (hDC.GetHandleOutput (), [0, 0] + scaled_size)
>
> hDC.EndPage ()
> hDC.EndDoc ()
> </code>
>
> HTH.
> TJG
>
> __________________________________________________ ______________________
> This e-mail has been scanned for all viruses by Star Internet. The
> service is powered by MessageLabs. For more information on a proactive
> anti-virus service working around the clock, around the globe, visit:
> http://www.star.net.uk
> __________________________________________________ ______________________
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-18-2007 10:11 AM
Printing Images through python post Jody Burgess Python 0 07-16-2004 04:13 PM
FW: Printing images through Python Tim Golden Python 1 10-06-2003 05:45 PM
RE: Printing images through Python Tim Golden Python 0 10-03-2003 08:53 AM
Printing images through Python Kevin Python 0 10-03-2003 05:23 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57