首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >python3用指定打印机打印景观图像/文件

python3用指定打印机打印景观图像/文件
EN

Stack Overflow用户
提问于 2019-02-04 18:22:46
回答 2查看 2.4K关注 0票数 4

我想打印一个pdf文件(或图像),我在我的脚本上创建的指定打印机,但该文件是横向的。我尝试过蒂姆·金的蟒蛇印刷品,但是它打印错了,大多数图像没有打印,或者我收到一个错误消息,指定的文件是找不到的。这是错误:"pywintypes.error:(2,'ShellExecute',‘系统找不到指定的文件’)“,命令是:win32api.ShellExecute (0,"print",filename,'/d:"%s"‘’% printer_name,”,0)。当然,文件名和打印机是字符串,打印机名称取自win32print.EnumPrinters(2,None,1)。

这是我的打印功能:

代码语言:javascript
运行
复制
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()

我不知道还能尝试什么。有办法做到这一点吗?

EN

回答 2

Stack Overflow用户

发布于 2019-02-05 08:02:02

bmp = bmp.rotate(90)

这将裁剪图像。使用img.rotate(90, expand=True)正确翻转图像。

您可以使用SetViewportExt/SetWindowExt代替手动计算位图与打印机分辨率的比率。您还需要考虑打印机的保证金。见下面的例子。

文件未找到错误的系统错误是单独的。使用调试器查找其发生的位置。

代码语言:javascript
运行
复制
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 )
票数 5
EN

Stack Overflow用户

发布于 2020-01-08 14:54:49

@Barmak Shemirani上面的大部分代码应该得到充分的赞扬。我只是添加了一个部分,允许您选择页面大小和/或强制方向。您现在也可以轻松地使用win32con中的devmode来根据需要推送图像。

代码语言:javascript
运行
复制
def print_test(filename, page_size="11x17", page_orientation="landscape", printer_name = win32print.GetDefaultPrinter()):

    try:
        img = Image.open(filename, 'r')
    except:
        print("error")
        return

    # open the printer.
    hprinter = win32print.OpenPrinter(printer_name)

    # retrieve default settings.  this code does not work on
    devmode = win32print.GetPrinter(hprinter, 2)["pDevMode"]

    # change paper size and orientation
    # all numbers can be found here -
    # https://github.com/SublimeText/Pywin32/blob/master/lib/x64/win32/lib/win32con.py
    if page_size == "letter":
        devmode.PaperSize = 1 # 11x17 = 17, letter = 1
    elif page_size == "11x17":
        devmode.PaperSize = 17 # 11x17 = 17, letter = 1

    # 1 = portrait, 2 = landscape
    if page_orientation == "portait":
        devmode.Orientation = 2
    elif page_orientation == "landscape":
        devmode.Orientation = 2

    # create dc using new settings
    # first get the integer hDC value - note that we need the name
    hdc = win32gui.CreateDC("WINSPOOL", printer_name, devmode)
    # next create a PyCDC from the hDC.
    hdc = win32ui.CreateDCFromHandle(hdc)


    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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54522120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档