# -*- coding:utf-8 -*-
# Author: Pete Yim<xpHook@gmail.com>
# Date : 13-8-22
# Copyright (c) 2013 RCSH Systems Incorporated. All rights reserved.
import win32print
import win32ui
from PIL import Image, ImageWin
import _imaging #
# Constants for GetDeviceCaps
#
#
# HORZRES / VERTRES = printable area
#
HORZRES = 8
VERTRES = 10
#
# LOGPIXELS = dots per inch
#
LOGPIXELSX = 88
LOGPIXELSY = 90
#
# PHYSICALWIDTH/HEIGHT = total area
#
PHYSICALWIDTH = 110
PHYSICALHEIGHT = 111
#
# PHYSICALOFFSETX/Y = left / top margin
#
PHYSICALOFFSETX = 112
PHYSICALOFFSETY = 113 printer_name = win32print.GetDefaultPrinter ()
file_name = "E:\\abc.jpg" #
# You can only write a Device-independent bitmap
# directly to a Windows device context; therefore
# we need (for ease) to use the Python Imaging
# Library to manipulate the image.
#
# 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)
printer_size = hDC.GetDeviceCaps (PHYSICALWIDTH), hDC.GetDeviceCaps (PHYSICALHEIGHT)
printer_margins = hDC.GetDeviceCaps (PHYSICALOFFSETX), hDC.GetDeviceCaps (PHYSICALOFFSETY) #
# Open the image, rotate it if it's wider than
# it is high, and work out how much to multiply
# each pixel by to get it as big as possible on
# the page without distorting.
#
bmp = Image.open (file_name)
if bmp.size[0] > bmp.size[1]:
bmp = bmp.rotate (90) ratios = [1.0 * printable_area[0] / bmp.size[0], 1.0 * printable_area[1] / bmp.size[1]]
scale = min (ratios) #
# Start the print job, and draw the bitmap to
# the printer device at the scaled size.
#
hDC.StartDoc (file_name)
hDC.StartPage () dib = ImageWin.Dib (bmp)
scaled_width, scaled_height = [int (scale * i) for i in bmp.size]
x1 = int ((printer_size[0] - scaled_width) / 2)
y1 = int ((printer_size[1] - scaled_height) / 2)
x2 = x1 + scaled_width
y2 = y1 + scaled_height
dib.draw (hDC.GetHandleOutput (), (x1, y1, x2, y2)) hDC.EndPage ()
hDC.EndDoc ()
hDC.DeleteDC ()

注:python调用win32接口打印照片

最新文章

  1. UILabel实现自适应高宽
  2. ggplot2.multiplot:将多个图形使用GGPLOT2在同一页上
  3. DirectWrite文字排版——字符串去尾
  4. winform之excel导入和导出
  5. RSA的密钥把JAVA格式转换成C#的格式
  6. 明修栈道,暗渡陈仓----之私募一哥徐翔新玩法 z
  7. Installutil.exe 注册exe
  8. Android用户界面 UI组件--AdapterView及其子类(二) AdapterViewAnimator及其子类
  9. IOS开发,如何用最新的Itunes给手机装ipa文件
  10. velocity序列动画
  11. USB OTG to PC USB API简介
  12. Sqoop mysql 数据导入到hdfs
  13. Swift语言Auto Layout入门教程:上篇
  14. acm课程练习2--1001
  15. 最大流&amp;流分布&amp;不确定图网络可靠性
  16. form表单参数传递和url参数传递的区别
  17. ClientAbortException: java.net.SocketException: Software caused connection abort: socket write erro
  18. Screen Space Depth Varying Glow based on Heat Diffusion
  19. 关于xampp中无法启动mysql,Attempting to start MySQL service...的解决办法!!
  20. 西门子PLC SCL语言开发学习笔记(二)

热门文章

  1. php 简单的验证码
  2. 【转】Ext.ajax.request 中的success和failure
  3. js 不可变的原始值和可变的对象引用
  4. php版网易视频云api
  5. HTML 5 video 视频标签全属性详解
  6. SQL SERVER 2008 使用TDE加密和解密
  7. “typedef int (init_fnc_t) (void);“的含义
  8. iOS开发之国际化
  9. The state of Web Components
  10. [wikioi]关押罪犯