1 #-*-coding:utf-8-*-2 #author:pete yim<[email protected]>3 #date:13-8-224 #Copyright (c) rcsh Systems Incorporated. All rights reserved.5 ImportWin32print6 ImportWin32ui7 fromPILImportImage, Imagewin8 Import_imaging9 Ten # One #Constants for GetDeviceCaps A # - # - #horzres/vertres = printable area the # -Horzres = 8 -Vertres = 10 - # + #logpixels = dots per inch - # +LOGPIXELSX = 88 ALogpixelsy = 90 at # - #physicalwidth/height = total area - # -Physicalwidth = 110 -Physicalheight = 111 - # in #physicaloffsetx/y = left/top margin - # toPhysicaloffsetx = 112 +Physicaloffsety = 113 - thePrinter_name =Win32print. GetDefaultPrinter () *file_name ="e:\\abc.jpg" $ Panax Notoginseng # - #can only write a device-independent bitmap the #directly to a Windows device context; therefore + #we need (for ease) to use the Python Imaging A #Library to manipulate the image. the # + #Create A device context from a named printer - #and assess the printable size of the paper. $ # $HDC =Win32ui. CreateDC () - HDC.CREATEPRINTERDC (printer_name) -Printable_area =Hdc.getdevicecaps (horzres), Hdc.getdevicecaps (vertres) thePrinter_size =Hdc.getdevicecaps (physicalwidth), Hdc.getdevicecaps (physicalheight) -Printer_margins =Hdc.getdevicecaps (PHYSICALOFFSETX), Hdc.getdevicecaps (physicaloffsety)Wuyi the # - #Open the image, rotate it if it ' s wider than Wu #It is-high, and work-out how-much to multiply - #Each pixel-to- get it as big as possible on About #The page without distorting. $ # -BMP =Image.open (file_name) - ifBmp.size[0] > Bmp.size[1]: -BMP = Bmp.rotate (90) A +ratios = [1.0 * printable_area[0]/bmp.size[0], 1.0 * printable_area[1]/bmp.size[1]] theScale =min (ratios) - $ # the #Start The print job, and draw the bitmap to the #The printer device at the scaled size. the # the Hdc.startdoc (file_name) - hdc.startpage () in theDIB =Imagewin.dib (BMP) theScaled_width, scaled_height = [Int (scale * i) forIinchBmp.size] Aboutx1 = Int ((printer_size[0]-scaled_width)/2) they1 = Int ((printer_size[1]-scaled_height)/2) thex2 = x1 +Scaled_width they2 = y1 +Scaled_height + Dib.draw (Hdc.gethandleoutput (), (x1, y1, x2, y2)) - the hdc.endpage ()Bayi Hdc.enddoc () theHdc.deletedc ()
Note: Python calls the Win32 interface to print photos
Python Win32 Print Sample