python - python3使用指定的打印机打印横向图像/文件
问题描述
我想使用指定的打印机打印我在脚本上创建的 pdf 文件(或图像),但该文件是横向的。我已经尝试过Tim Golden 的 python 打印,但打印错误并且大部分图像没有打印,或者我收到一条错误消息,指出找不到指定的文件。这是错误:“pywintypes.error: (2, 'ShellExecute', '系统找不到指定的文件。')” 命令如下:win32api.ShellExecute (0, "print", filename, '/d :"%s"' % 打印机名称, ".", 0)。当然文件名和打印机是字符串,打印机名称取自 win32print.EnumPrinters(2,None,1)
这是我的打印功能:
def programA_printer():
global name
global printer_name
global event2
# time.sleep(3)
i=0
while True:
if not event2.is_set():
try:
img = Image.open("Program_A_graph.png", 'r')
if (time.time()-(os.path.getmtime("Program_A_graph.png")) < 1.75):
break
except OSError as identifier:
i = i+1
print(identifier)
time.sleep(1)
if i>5:
print("Υπήρξε πρόβλημα, δεν εκτυπώνω και συνεχίζω στο επόμενο σετ!")
return
serial_number_message = int(time.time())
# img.show(title="Final Result")
img.convert('RGB').save('./εκτυπώσεις/'+str(serial_number_message)+'.pdf', format="PDF", resolution=100.0)
#win32api.ShellExecute (0, "print", './εκτυπώσεις/'+str(serial_number_message)+'.pdf', '/d:"%s"' % printer_name, ".",0)
#win32api.ShellExecute (0, "print", './εκτυπώσεις/'+str(serial_number_message)+'.pdf', '/d:"%s"' % printer_name, "./εκτυπώσεις",0)
HORZRES = 10
VERTRES = 10
PHYSICALWIDTH = 110
PHYSICALHEIGHT = 111
PHYSICALOFFSETX = 112
PHYSICALOFFSETY = 113
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)
bmp = img
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)
hDC.StartDoc("Result")
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()
我不知道还能尝试什么。有没有办法做到这一点?
解决方案
bmp = bmp.rotate(90)
这将裁剪图像。用于img.rotate(90, expand=True)
正确翻转图像。
您可以使用SetViewportExt
/SetWindowExt
而不是手动计算位图与打印机分辨率的比率。您还需要考虑打印机的边距。请参见下面的示例。
未找到文件错误的系统错误是单独的。使用调试器查找它发生的位置。
import win32ui, win32con
from PIL import Image, ImageWin
def print_test(printer_name):
try:
filename = "Program_A_graph.png"
img = Image.open(filename, 'r')
except:
print("error")
return
hdc = win32ui.CreateDC()
hdc.CreatePrinterDC(printer_name)
horzres = hdc.GetDeviceCaps(win32con.HORZRES)
vertres = hdc.GetDeviceCaps(win32con.VERTRES)
landscape = horzres > vertres
if landscape:
if img.size[1] > img.size[0]:
print('Landscape mode, tall image, rotate bitmap.')
img = img.rotate(90, expand=True)
else:
if img.size[1] < img.size[0]:
print('Portrait mode, wide image, rotate bitmap.')
img = img.rotate(90, expand=True)
img_width = img.size[0]
img_height = img.size[1]
if landscape:
#we want image width to match page width
ratio = vertres / horzres
max_width = img_width
max_height = (int)(img_width * ratio)
else:
#we want image height to match page height
ratio = horzres / vertres
max_height = img_height
max_width = (int)(max_height * ratio)
#map image size to page size
hdc.SetMapMode(win32con.MM_ISOTROPIC)
hdc.SetViewportExt((horzres, vertres));
hdc.SetWindowExt((max_width, max_height))
#offset image so it is centered horizontally
offset_x = (int)((max_width - img_width)/2)
offset_y = (int)((max_height - img_height)/2)
hdc.SetWindowOrg((-offset_x, -offset_y))
hdc.StartDoc('Result')
hdc.StartPage()
dib = ImageWin.Dib(img)
dib.draw(hdc.GetHandleOutput(), (0, 0, img_width, img_height))
hdc.EndPage()
hdc.EndDoc()
hdc.DeleteDC()
print( 'Debug info:' )
print( 'Landscape: %d' % landscape )
print( 'horzres: %d' % horzres )
print( 'vertres: %d' % vertres )
print( 'img_width: %d' % img_width )
print( 'img_height: %d' % img_height )
print( 'max_width: %d' % max_width )
print( 'max_height: %d' % max_height )
print( 'offset_x: %d' % offset_x )
print( 'offset_y: %d' % offset_y )
推荐阅读
- flex-lexer - Flex-lexer:将状态定义写入不同的文件
- python - 如果 NaN 存在于多列中的任何位置,则删除组
- frida - 如何使用frida挂钩intent.getData()?
- python - 在单链表中找到最小的项目并移动到头部?
- java - Java 中的 Pusher Chatkit Connect 回调
- ruby-on-rails - raise ConnectionNotEstablished, "没有找到带有 '#{spec_name}' 的连接池。" 除非池 LOCALHOST3000 不工作
- scala - 在 scala 项目上 - 运行 sbt test 命令时出现错误 GC 开销限制
- javascript - 在实际删除数据(POST)之前,我应该如何实现删除确认引导模式?
- java - BlueJ Java 谜题中移动方法的条件语句
- python - 如何在 python 中修复 TypeError