コード例 #1
0
def printer_pic(file_name):
    # 物理宽度、高度
    PHYSICALWIDTH = 110
    PHYSICALHEIGHT = 111
    # 物理偏移位置
    PHYSICALOFFSETX = 0
    PHYSICALOFFSETY = 0
    printer_name = win32print.GetDefaultPrinter()
    printer_name = 'Microsoft Print to PDF'
    hDC = win32ui.CreateDC()
    hDC.CreatePrinterDC(printer_name)
    printer_size = hDC.GetDeviceCaps(PHYSICALWIDTH), hDC.GetDeviceCaps(
        PHYSICALHEIGHT)
    bmp = Image.open(file_name)
    if (bmp.size[0] > bmp.size[1]):
        #print('rotate...')
        #print(bmp.size)
        bmp = bmp.transpose(Image.ROTATE_90)
        #bmp.show()
        print(bmp.size)

    hDC.StartDoc(file_name)
    hDC.StartPage()
    dib = ImageWin.Dib(bmp)

    dib.draw(hDC.GetHandleOutput(), (0, 0, 4961, 7016))

    hDC.EndPage()
    hDC.EndDoc()
    hDC.DeleteDC()
コード例 #2
0
    def handlePrint(self, data):
        with renderLock:
            self.document, copies = data
            self.update()

            if platform == "linux":
                bmp = QImage(self.size[0], self.size[1], QImage.Format_Mono)
                self.paintEvent(None, bmp)
                buffer = QBuffer()
                buffer.open(QBuffer.ReadWrite)
                bmp.save(buffer, "BMP")
                img = Image.open(io.BytesIO(buffer.data()))
                img.save("/tmp/image.png")
                for i in range(copies):
                    os.system("lpr /tmp/image.png")
            elif platform == "win32":
                pix = self.grab()
                bmp = QImage(pix)
                buffer = QBuffer()
                buffer.open(QBuffer.ReadWrite)
                bmp.save(buffer, "BMP")
                img = Image.open(io.BytesIO(buffer.data()))
                printerName = win32print.GetDefaultPrinter()
                deviceContext = win32ui.CreateDC()
                deviceContext.CreatePrinterDC(printerName)
                deviceContext.StartDoc("Inventory Label")
                for i in range(copies):
                    deviceContext.StartPage()
                    dib = ImageWin.Dib(img)
                    dib.draw(deviceContext.GetHandleOutput(),
                             (0, 0, self.size[0], self.size[1]))
                    deviceContext.EndPage()
                deviceContext.EndDoc()
                deviceContext.DeleteDC()
コード例 #3
0
ファイル: autoPrint.py プロジェクト: georgeliwei/autoPrint
def print_picture_file(fname):
    default_printer_name = wp.GetDefaultPrinter()
    if default_printer_name is None or default_printer_name == "":
        return

    hDC = wu.CreateDC()
    hDC.CreatePrinterDC(default_printer_name)
    printable_area = hDC.GetDeviceCaps(8), hDC.GetDeviceCaps(10)
    printer_size = hDC.GetDeviceCaps(110), hDC.GetDeviceCaps(111)
    printer_margins = hDC.GetDeviceCaps(112), hDC.GetDeviceCaps(113)

    img = Image.open(fname)
    if img.size[0] > img.size[1]:
        img = img.transpose(Image.ROTATE_90)
    ratios = [1.0 * printable_area[0] / img.size[0], 1.0 * printable_area[1] / img.size[1]]
    scale = min(ratios)
    hDC.StartDoc(fname)
    hDC.StartPage()
    dib = ImageWin.Dib(img)
    scaled_width, scaled_height = [int(scale * i) for i in img.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()
コード例 #4
0
    def imprimirEtiqueta(self,
                         impressora_name,
                         caminho,
                         etiqueta,
                         move=False,
                         destino=''):
        hDC = win32ui.CreateDC()
        hDC.CreatePrinterDC(impressora_name)
        printable_area = hDC.GetDeviceCaps(self.HORZRES), hDC.GetDeviceCaps(
            self.VERTRES)
        printer_size = hDC.GetDeviceCaps(
            self.PHYSICALWIDTH), hDC.GetDeviceCaps(self.PHYSICALHEIGHT)
        printer_margins = hDC.GetDeviceCaps(
            self.PHYSICALOFFSETX), hDC.GetDeviceCaps(self.PHYSICALOFFSETY)

        bmp = Image.open(caminho + "/" + etiqueta).convert("1")
        bmp = bmp.rotate(180)
        hDC.StartDoc(etiqueta)
        hDC.StartPage()
        dib = ImageWin.Dib(bmp)
        dib.draw(hDC.GetHandleOutput(),
                 ((0 + 70), (0), (639),
                  (300 - 50)))  #ajustar a area imprimivel do papel
        hDC.EndPage()
        hDC.EndDoc()
        hDC.DeleteDC()
        if (move):
            self.movEtiqueta(caminho, etiqueta, destino)
        pass
コード例 #5
0
def print_png_list(pngList, dirPath, printerName):
    hDC = win32ui.CreateDC()

    for filename in pngList:
        try:
            hDC.CreatePrinterDC(win32print.GetDefaultPrinter())
            printer_size = hDC.GetDeviceCaps(win32con.PHYSICALWIDTH), hDC.GetDeviceCaps(win32con.PHYSICALHEIGHT)
            logging.debug(printer_size)

            bmp = Image.open(dirPath + '/' + filename)
            # if bmp.size[0] < bmp.size[1]:
            #     bmp = bmp.rotate(90)

            hDC.StartDoc(dirPath + '/' + filename)
            hDC.StartPage()

            dib = ImageWin.Dib(bmp)
            labelSize = get_label_size(printerName)
            dib.draw(hDC.GetHandleOutput(), (0, 0, labelSize[0], labelSize[1]))
            hDC.EndPage()

            time.sleep(1)

        except BaseException as e:
            raise e

        finally:
            hDC.EndDoc()
コード例 #6
0
def sample_print_image():
    sampleFileName = './sample_waybill.png'

    hDC = win32ui.CreateDC()
    try:
        hDC.CreatePrinterDC(win32print.GetDefaultPrinter())
        printer_size = hDC.GetDeviceCaps(win32con.PHYSICALWIDTH), hDC.GetDeviceCaps(win32con.PHYSICALHEIGHT)
        logging.debug(printer_size)

        bmp = Image.open(sampleFileName)
        # if bmp.size[0] < bmp.size[1]:
        #     bmp = bmp.rotate(90)

        hDC.StartDoc(sampleFileName)
        hDC.StartPage()

        dib = ImageWin.Dib(bmp)
        # dib.draw(hDC.GetHandleOutput(), (0, 0, printer_size[0] - 100, printer_size[1]))
        dib.draw(hDC.GetHandleOutput(), (0, 0, LabelSize.SF_WIDTH, LabelSize.SF_HEIGHT))

    except BaseException as e:
        raise e

    finally:
        hDC.EndPage()
        hDC.EndDoc()
コード例 #7
0
        def test_pointer(self):
            im = hopper()
            (width, height) = im.size
            opath = self.tempfile("temp.png")
            imdib = ImageWin.Dib(im)

            hdr = BITMAPINFOHEADER()
            hdr.biSize = ctypes.sizeof(hdr)
            hdr.biWidth = width
            hdr.biHeight = height
            hdr.biPlanes = 1
            hdr.biBitCount = 32
            hdr.biCompression = BI_RGB
            hdr.biSizeImage = width * height * 4
            hdr.biClrUsed = 0
            hdr.biClrImportant = 0

            hdc = CreateCompatibleDC(None)
            pixels = ctypes.c_void_p()
            dib = CreateDIBSection(hdc, ctypes.byref(hdr), DIB_RGB_COLORS,
                                   ctypes.byref(pixels), None, 0)
            SelectObject(hdc, dib)

            imdib.expose(hdc)
            bitmap = serialize_dib(hdr, pixels)
            DeleteObject(dib)
            DeleteDC(hdc)

            Image.open(BytesIO(bitmap)).save(opath)
コード例 #8
0
def main():
    printer_name = win32print.GetDefaultPrinter(
    )  # How to reach printer on LPT1 port?
    hDC = win32ui.CreateDC()
    hDC.CreatePrinterDC(printer_name)

    qr = treepoem.generate_barcode(
        barcode_type='qrcode',
        data=nombre,
    )
    #qr2 = treepoem.generate_barcode(barcode_type='qrcode', data=nombre, )
    #barra = treepoem.generate_barcode(barcode_type='code39', data='12312371289379812',)
    #barra2 = treepoem.generate_barcode(barcode_type='code39', data='12312371289379812',)

    hDC.StartDoc("Codigos")
    hDC.StartPage()
    qr3 = ImageWin.Dib(qr)
    qr3.draw(hDC.GetHandleOutput(), (500, 2000, 1000, 2500))
    #qr4 = ImageWin.Dib(qr2)
    #qr4.draw(hDC.GetHandleOutput(),(500,2500,1000,3000))
    #barra5 = ImageWin.Dib(barra)
    #barra5.draw(hDC.GetHandleOutput(),(100, 1000,200,1100))
    #barra4 = ImageWin.Dib(barra2)
    #barra4.draw(hDC.GetHandleOutput(),(100,2000, 200, 2100))

    hDC.EndPage()
    hDC.EndDoc()
コード例 #9
0
def imprimir():
    PHYSICALWIDTH = 110
    PHYSICALHEIGHT = 110

    printer_name = win32print.GetDefaultPrinter()
    file_name = "image.jpg"

    hDC = win32ui.CreateDC()
    hDC.CreatePrinterDC(printer_name)
    printer_size = hDC.GetDeviceCaps(PHYSICALWIDTH), hDC.GetDeviceCaps(
        PHYSICALHEIGHT)

    bmp = Image.open(file_name)
    if bmp.size[0] < bmp.size[1]:
        bmp = bmp.rotate(90)

    hDC.StartDoc(file_name)
    hDC.StartPage()

    dib = ImageWin.Dib(bmp)
    dib.draw(hDC.GetHandleOutput(), (0, 0, printer_size[0], printer_size[1]))

    hDC.EndPage()
    hDC.EndDoc()
    hDC.DeleteDC()
コード例 #10
0
def do_print(file_path):
    printer_name = win32print.GetDefaultPrinter()
    hDC = win32ui.CreateDC()
    hDC.CreatePrinterDC(printer_name)
    printable_area = hDC.GetDeviceCaps(HORZRES), hDC.GetDeviceCaps(VERTRES)
    printer_size = hDC.GetDeviceCaps(PHYSICALWIDTH), hDC.GetDeviceCaps(
        PHYSICALHEIGHT)
    bmp = Image.open(file_path)
    ratios = [
        1.0 * printable_area[0] / bmp.size[0],
        1.0 * printable_area[1] / bmp.size[1]
    ]
    scale = min(ratios)
    hDC.StartDoc(file_path)
    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()
コード例 #11
0
def execPrint(file_name):
    #
    # 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

    #Ricoh SPC751m with 720x1115
    x1 = 100
    y1 = 0
    x2 = 5540
    y2 = 8599

    dib.draw(hDC.GetHandleOutput(), (x1, y1, x2, y2))

    hDC.EndPage()
    hDC.EndDoc()
    hDC.DeleteDC()
コード例 #12
0
ファイル: printer.py プロジェクト: csalg/chemotherapy_manager
 def __printImage(self, hdc, image):
     if config.debug:
         image.show()
         return
     hdc.StartPage()
     dib = ImageWin.Dib(image)
     dib.draw(hdc.GetHandleOutput(), (0, 0, self.page_height, self.page_width))
     hdc.EndPage()
コード例 #13
0
ファイル: test_imagewin.py プロジェクト: a3104/pillow-simd
    def test_removed_methods(self):
        # Arrange
        im = hopper()
        dib = ImageWin.Dib(im)

        # Act/Assert
        self.assertRaises(Exception, dib.tostring)
        self.assertRaises(Exception, dib.fromstring)
コード例 #14
0
    def test_dib_image(self):
        # Arrange
        im = hopper()

        # Act
        dib = ImageWin.Dib(im)

        # Assert
        assert dib.size == im.size
コード例 #15
0
    def test_dib_fromstring_tostring_deprecated(self):
        # Arrange
        im = hopper()
        dib = ImageWin.Dib(im)
        buffer = dib.tobytes()

        # Act/Assert
        self.assert_warning(DeprecationWarning, lambda: dib.tostring())
        self.assert_warning(DeprecationWarning, lambda: dib.fromstring(buffer))
コード例 #16
0
    def test_dib_image(self):
        # Arrange
        im = hopper()

        # Act
        dib = ImageWin.Dib(im)

        # Assert
        self.assertEqual(dib.size, im.size)
コード例 #17
0
    def test_hwnd(self):
        # Arrange
        wnd = 50

        # Act
        hwnd = ImageWin.HWND(wnd)
        wnd2 = int(hwnd)

        # Assert
        self.assertEqual(wnd2, 50)
コード例 #18
0
    def test_hdc(self):
        # Arrange
        dc = 50

        # Act
        hdc = ImageWin.HDC(dc)
        dc2 = int(hdc)

        # Assert
        assert dc2 == 50
コード例 #19
0
    def test_dib_mode_string(self):
        # Arrange
        mode = "RGBA"
        size = (128, 128)

        # Act
        dib = ImageWin.Dib(mode, size)

        # Assert
        self.assertEqual(dib.size, (128, 128))
コード例 #20
0
    def test_hwnd(self):
        # Arrange
        wnd = 50

        # Act
        hwnd = ImageWin.HWND(wnd)
        wnd2 = int(hwnd)

        # Assert
        assert wnd2 == 50
コード例 #21
0
    def test_hdc(self):
        # Arrange
        dc = 50

        # Act
        hdc = ImageWin.HDC(dc)
        dc2 = int(hdc)

        # Assert
        self.assertEqual(dc2, 50)
コード例 #22
0
 def image(self, position, image, size):
     "print PIL image at position with given size"
     if ImageWin is None:
         raise NotImplementedError, "PIL required for image method"
     if self.page == 0:
         self.begin_document()
     dib = ImageWin.Dib(image)
     endpos = (position[0] + size[0], position[1] + size[1])
     dest = (position[0] * scale_factor, -1 * position[1] * scale_factor,
             endpos[0] * scale_factor, -1 * endpos[1] * scale_factor)
     dib.draw(self.hdc, dest)
コード例 #23
0
def printPhotoFromFilename(file_name):
    HORZRES = 8
    VERTRES = 10

    file_name = path_to_watch + "\\" + file_name

    LOGPIXELSX = 88
    LOGPIXELSY = 90

    PHYSICALWIDTH = 110
    PHYSICALHEIGHT = 111

    PHYSICALOFFSETX = 0
    PHYSICALOFFSETY = 0

    printer_name = win32print.GetDefaultPrinter()

    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 = Image.open(file_name)
    if bmp.size[0] > bmp.size[1]:
        bmp = bmp.rotate(0)
    watermark = Image.open('ramka.png')
    bmp.paste(watermark, (0, 0, 1080, 1920), watermark)

    ratios = [
        1.2 * printable_area[0] / bmp.size[0],
        1.2 * printable_area[1] / bmp.size[1]
    ]

    print(ratios)
    scale = min(ratios)

    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()
コード例 #24
0
    def test_dib_frombytes_tobytes_roundtrip(self):
        # Arrange
        # Make two different DIB images
        im = hopper()
        dib1 = ImageWin.Dib(im)

        mode = "RGB"
        size = (128, 128)
        dib2 = ImageWin.Dib(mode, size)

        # Confirm they're different
        self.assertNotEqual(dib1.tobytes(), dib2.tobytes())

        # Act
        # Make one the same as the using tobytes()/frombytes()
        test_buffer = dib1.tobytes()
        dib2.frombytes(test_buffer)

        # Assert
        # Confirm they're the same
        self.assertEqual(dib1.tobytes(), dib2.tobytes())
コード例 #25
0
    def do_printer(self, filepath, print_x_mm, print_y_mm, print_w_mm,
                   print_h_mm):
        """
        if (self.mm_to_pixel(print_w_mm,0)>self.printable_area[0])|(self.mm_to_pixel(print_h_mm,1)>self.printable_area[1]):
            print(self.mm_to_pixel(print_w_mm,0),self.printable_area[0])
            print(self.mm_to_pixel(print_h_mm,1),self.printable_area[1])
            print("Error print size")        
            return False
        """

        bmp = Image.open(filepath)
        bmp_width = bmp.size[0]
        bmp_height = bmp.size[1]
        print("Image size(mm): ", self.pixel_to_mm(bmp_width, 0), '*',
              self.pixel_to_mm(bmp_height, 1))

        ratios = [
            1.0 * self.mm_to_pixel(print_w_mm, 0) / bmp_width,
            1.0 * self.mm_to_pixel(print_h_mm, 1) / bmp_height
        ]
        scale = min(ratios)
        #print(m_HORZRES-printer_margins[0] ,bmp.size[0],m_VERTRES ,bmp.size[1])
        print("ratios", ratios, "scale", scale)

        self.hDC.StartDoc(filepath)
        self.hDC.StartPage()
        dib = ImageWin.Dib(bmp)
        scaled_width = int(scale * bmp_width)
        scaled_height = int(scale * bmp_height)
        print("scaled_width(mm)", self.pixel_to_mm(scaled_width, 0),
              "scaled_height(mm)", self.pixel_to_mm(scaled_height, 1))

        offset_x = self.mm_to_pixel(print_x_mm, 0)
        offset_y = self.mm_to_pixel(print_y_mm, 1)

        x1 = int(
            (self.mm_to_pixel(print_w_mm, 0) - scaled_width) / 2) + offset_x
        y1 = int(
            (self.mm_to_pixel(print_h_mm, 0) - scaled_height) / 2) + offset_y
        x2 = x1 + scaled_width + offset_x
        y2 = y1 + scaled_height + offset_y
        print('x1(mm):', self.pixel_to_mm(x1, 0), 'y1(mm):',
              self.pixel_to_mm(y1, 1))
        print('x2(mm):', self.pixel_to_mm(x2, 0), 'y2(mm):',
              self.pixel_to_mm(y2, 1))

        #return 0
        dib.draw(self.hDC.GetHandleOutput(), (x1, y1, x2, y2))

        self.hDC.EndPage()
        self.hDC.EndDoc()
        print("Print Finish")
コード例 #26
0
    def test_dib_paste(self):
        # Arrange
        im = hopper()

        mode = "RGBA"
        size = (128, 128)
        dib = ImageWin.Dib(mode, size)

        # Act
        dib.paste(im)

        # Assert
        self.assertEqual(dib.size, (128, 128))
コード例 #27
0
    def test_dib_paste_bbox(self):
        # Arrange
        im = hopper()
        bbox = (0, 0, 10, 10)

        mode = "RGBA"
        size = (128, 128)
        dib = ImageWin.Dib(mode, size)

        # Act
        dib.paste(im, bbox)

        # Assert
        assert dib.size == (128, 128)
コード例 #28
0
def printStripFunction(id):
    flt=flight.objects.get(id=id)
    try:
        # https://stackoverflow.com/questions/53765699/python-win32print-job-status
        # printing
        # p=win32print.OpenPrinter('OneNote')
        # i set the printer to landscape
        p=win32print.GetDefaultPrinter()
        print("the printer is: "+p)
        image_name=str(flt.stripImage)
        print(image_name)

        hdc=win32ui.CreateDC()
        hdc.CreatePrinterDC(p)
        printer_size=hdc.GetDeviceCaps(110),hdc.GetDeviceCaps(111)
        print(printer_size)

        bmp=Image.open(image_name)
        if bmp.size[0]>bmp.size[1]:
            print("horizental strip width > hieght")
            # bmp=bmp.rotate(90)

        else:
            print("vertical strip width < hieght")
            bmp=bmp.rotate(90)


        print("size 0 is: "+str(bmp.size[0]))
        print("size 1 is: "+str(bmp.size[1]))

        hdc.StartDoc(image_name)
        hdc.StartPage()

        dib=ImageWin.Dib(bmp)
        print("printer size 0 :....>"+str(printer_size[0]+764))
        print("printer size 1 :....>"+str(printer_size[1]+95))

        dib.draw(hdc.GetHandleOutput(),(0,0,printer_size[0],printer_size[1]))
        print("get handeled output..."+str(hdc.GetHandleOutput()))
        print("ending page.....")
        hdc.EndPage()
        hdc.EndDoc()
        hdc.DeleteDC()
        # save printed and squack
        flt.printed=True
        flt.save()
        printingStatus=True
    except:
        printingStatus=False
    return printingStatus
コード例 #29
0
def print_image(img, printer_name):
    """процедура печати картинки"""
    requestMessage = {}
    try:
        if printer_name == "":
            printer_name = win32print.GetDefaultPrinter()
    except Exception:
        requestMessage[
            "Error"] = "GetDefaultPrinter:%s" % traceback.format_exc()
        return requestMessage
    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]:
            img = img.rotate(90, expand=True)
    else:
        if img.size[1] < img.size[0]:
            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()
コード例 #30
0
def printer_pic(file_name):
    # 物理宽度、高度
    PHYSICALWIDTH = 110
    PHYSICALHEIGHT = 111
    # 物理偏移位置
    PHYSICALOFFSETX = 0
    PHYSICALOFFSETY = 0
    printer_name = win32print.GetDefaultPrinter()
    hDC = win32ui.CreateDC()
    hDC.CreatePrinterDC(printer_name)
    printer_size = hDC.GetDeviceCaps(PHYSICALWIDTH), hDC.GetDeviceCaps(
        PHYSICALHEIGHT)
    # printer_margins = hDC.GetDeviceCaps (PHYSICALOFFSETX), hDC.GetDeviceCaps (PHYSICALOFFSETY)
    # 打开图片
    # #通过每个像素使它尽可能大
    # #页面不失真。
    bmp = Image.open(file_name)
    #bmp.show()
    if (bmp.size[0] > bmp.size[1]):
        #print('rotate...')
        #print(bmp.size)
        bmp = bmp.transpose(Image.ROTATE_90)
        #bmp.show()
        #print(bmp.size)
    ratios = [1.0 * 1754 / bmp.size[0], 1.0 * 1240 / bmp.size[1]]
    scale = min(ratios)
    # #开始打印作业,并将位图绘制到
    # #按比例缩放打印机设备。
    hDC.StartDoc(file_name)
    hDC.StartPage()
    dib = ImageWin.Dib(bmp)
    scaled_width, scaled_height = [int(scale * i) for i in bmp.size]
    #print(scaled_width, scaled_height)
    x1 = int((printer_size[0] - scaled_width) / 2)
    y1 = int((printer_size[1] - scaled_height) / 2)
    # 横向位置坐标
    x1 = 0
    # 竖向位置坐标
    y1 = 0
    # 4倍为自适应图片实际尺寸打印
    x2 = x1 + bmp.size[0] * 4
    y2 = y1 + bmp.size[1] * 4
    dib.draw(hDC.GetHandleOutput(), (x1, y1, x2, y2))

    hDC.EndPage()
    hDC.EndDoc()
    hDC.DeleteDC()