博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
四、python图像合并,打印图片
阅读量:4133 次
发布时间:2019-05-25

本文共 3439 字,大约阅读时间需要 11 分钟。

生活中肯定会遇到两张图片或多张合并成一张图片的情况,下面使用python+Pillow 实现这个目的。

代码:

import win32printimport win32uiimport win32confrom barcode.writer import ImageWriterfrom barcode.codex import Code39from PIL import Image, ImageDraw, ImageFont, ImageWin, ImageColorfrom StringIO import StringIOimport osdef merge(self, image1, image2):        '''        实现垂直合并两张图片        @param image1: 需要合并的第一张图片        @param image2: 需要合并的第二张图片        '''        w1, h1 = image1.size        w2, h2 = image2.size        target = Image.new('RGB', (w1 if w1 > w2 else w2 , h1 + h2), (255, 255, 255, 0))        temp = image1.resize((w1, h1), Image.ANTIALIAS)        target.paste(temp, (0, 0, w1, h1))        temp1 = image2.resize((w2, h2), Image.ANTIALIAS)        target.paste(temp1, (0, h1, w2, h1 + h2))        target.save('merge.png')        target.show()if __name__ == '__main__':    printer_name = win32print.GetDefaultPrinter()    print printer_name    t = Test()    t.merge(Image.open('t.png'), Image.open('image2.png'))

效果如下:

输入图片说明

打印图片,pywin32上的例子,为了方便,在这里在粘贴一遍

def imagePirnter(self):        #        # 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 = "merge.png"        #        # 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 ()if __name__ == '__main__':    t = Test()    t.imagePirnter()

效果如下:

输入图片说明

参考文章:

pywin32 win32print

转载地址:http://uiivi.baihongyu.com/

你可能感兴趣的文章
非常不错 Hadoop 的HDFS (Hadoop集群(第8期)_HDFS初探之旅)
查看>>
Tomcat启动错误,端口占用
查看>>
laravel 修改api返回默认的异常处理
查看>>
高德坐标转换百度坐标 javascript
查看>>
tp5封装通用的修改某列值
查看>>
laravel控制器与模型名称不统一
查看>>
vue登录拦截
查看>>
laravel事务
查看>>
laravel部署到宝塔步骤
查看>>
小程序获取access_token
查看>>
navicat远程连接mysql数据库
查看>>
tp5令牌数据无效 解决方法
查看>>
自己的网站与UCenter整合(大致流程)
查看>>
laravel 制作通用的curd 后台操作
查看>>
【小红书2017年笔试】求一个数组中平均数最大的子数组
查看>>
Linux基础系列-定时器与时间管理
查看>>
Linux基础系列-可执行程序的产生过程
查看>>
Linux基础系列-Kernel 初始化宏
查看>>
Linux子系统系列-I2C
查看>>
<iOS>关于自定义description的一点用法
查看>>