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
> -----Original Message-----
> From: Tim Golden
> Sent: 03 October 2003 09:49
> To: 'Kevin'
> Cc: http://www.velocityreviews.com/forums/(E-Mail Removed)
> Subject: RE: Printing images through Python
>
>
> > From: Kevin [mailto(E-Mail Removed)]
> > 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
>


Also meant to suggest looking on Roger Burnham's site for a fuller example:

http://starship.python.net/crew/roger/

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 1 10-04-2003 03:17 AM
Printing images through Python Kevin Python 0 10-03-2003 05:23 AM



Advertisments