标签:
1 # -*- coding:utf-8 -*- 2 # Author: Pete Yim<xpHook@gmail.com> 3 # Date : 13-8-22 4 # Copyright (c) 2013 RCSH Systems Incorporated. All rights reserved. 5 import win32print 6 import win32ui 7 from PIL import Image, ImageWin 8 import _imaging 9 10 # 11 # Constants for GetDeviceCaps 12 # 13 # 14 # HORZRES / VERTRES = printable area 15 # 16 HORZRES = 8 17 VERTRES = 10 18 # 19 # LOGPIXELS = dots per inch 20 # 21 LOGPIXELSX = 88 22 LOGPIXELSY = 90 23 # 24 # PHYSICALWIDTH/HEIGHT = total area 25 # 26 PHYSICALWIDTH = 110 27 PHYSICALHEIGHT = 111 28 # 29 # PHYSICALOFFSETX/Y = left / top margin 30 # 31 PHYSICALOFFSETX = 112 32 PHYSICALOFFSETY = 113 33 34 printer_name = win32print.GetDefaultPrinter () 35 file_name = "E:\\abc.jpg" 36 37 # 38 # You can only write a Device-independent bitmap 39 # directly to a Windows device context; therefore 40 # we need (for ease) to use the Python Imaging 41 # Library to manipulate the image. 42 # 43 # Create a device context from a named printer 44 # and assess the printable size of the paper. 45 # 46 hDC = win32ui.CreateDC () 47 hDC.CreatePrinterDC (printer_name) 48 printable_area = hDC.GetDeviceCaps (HORZRES), hDC.GetDeviceCaps (VERTRES) 49 printer_size = hDC.GetDeviceCaps (PHYSICALWIDTH), hDC.GetDeviceCaps (PHYSICALHEIGHT) 50 printer_margins = hDC.GetDeviceCaps (PHYSICALOFFSETX), hDC.GetDeviceCaps (PHYSICALOFFSETY) 51 52 # 53 # Open the image, rotate it if it‘s wider than 54 # it is high, and work out how much to multiply 55 # each pixel by to get it as big as possible on 56 # the page without distorting. 57 # 58 bmp = Image.open (file_name) 59 if bmp.size[0] > bmp.size[1]: 60 bmp = bmp.rotate (90) 61 62 ratios = [1.0 * printable_area[0] / bmp.size[0], 1.0 * printable_area[1] / bmp.size[1]] 63 scale = min (ratios) 64 65 # 66 # Start the print job, and draw the bitmap to 67 # the printer device at the scaled size. 68 # 69 hDC.StartDoc (file_name) 70 hDC.StartPage () 71 72 dib = ImageWin.Dib (bmp) 73 scaled_width, scaled_height = [int (scale * i) for i in bmp.size] 74 x1 = int ((printer_size[0] - scaled_width) / 2) 75 y1 = int ((printer_size[1] - scaled_height) / 2) 76 x2 = x1 + scaled_width 77 y2 = y1 + scaled_height 78 dib.draw (hDC.GetHandleOutput (), (x1, y1, x2, y2)) 79 80 hDC.EndPage () 81 hDC.EndDoc () 82 hDC.DeleteDC ()
注:python调用win32接口打印照片
标签:
原文地址:http://www.cnblogs.com/phirothing/p/4506235.html