一、python 生成一维码
来源:互联网 发布:父母洗脑 知乎 编辑:程序博客网 时间:2024/05/07 05:25
生活中到处可见一维码,几乎所有的正规产品上都可以看到,这几天好奇使用python可不可以实现生成一维码,并打印的功能,还真让我找到方法了,下面记录一下过程。
所需软件:
pyBarcode 0.7 https://pypi.python.org/pypi/pyBarcode/0.7
python2.7.12
Pillow-4.2.1 https://pypi.python.org/pypi/Pillow/4.2.1
pywin32 https://pypi.python.org/pypi/pywin32/
第一步 生成一维码:
from barcode.writer import ImageWriterfrom barcode.codex import Code39from PIL import Image, ImageDraw, ImageFont, ImageWinfrom StringIO import StringIOdef generagteBarCode(self): imagewriter = ImageWriter() #保存到图片中 # add_checksum : Boolean Add the checksum to code or not (default: True) ean = Code39("1234567890", writer=imagewriter, add_checksum=False) # 不需要写后缀,ImageWriter初始化方法中默认self.format = 'PNG' print '保存到image2.png' ean.save('image2') img = Image.open('image2.png') print '展示image2.png' img.show() # 写入stringio流中 i = StringIO() ean = Code39("0987654321", writer=imagewriter, add_checksum=False) ean.write(i) i = StringIO(i.getvalue()) img1 = Image.open(i) print '保存到stringIO中并以图片方式打开' img1.show()
效果如下:
参考文章:
pyBarcode document http://pythonhosted.org/pyBarcode/writers/index.html
StringIO https://docs.python.org/2/library/stringio.html
pywin32 win32print http://timgolden.me.uk/python/win32_how_do_i/print.html
pillow document http://pillow.readthedocs.io/en/4.2.x/reference/ImageDraw.html#example-draw-a-gray-cross-over-an-image
pillow 图片处理示例 http://www.yeayee.com/article-6739499-1.html
阅读全文
0 0
- 一、python 生成一维码
- python reportlab 生成pdf (一)
- Python生成黑客字典程序(一)
- 【Python】【Caffe】一、生成prototxt文件《python调用caffe模块》
- Python爬虫用Selenium抓取js生成的文件(一)
- python 生成随机一维或多维正态分布
- [python每日一练]--0001:生成激活码
- 用SIP来生成C/C++/Qt库的Python绑定(一)
- [开源] android apk 一键生成混淆文件 脚本分享 (python)
- [python每日一练]--0001:生成激活码并存储到mysql数据库
- python(一)
- python一
- Python(一)
- Python一
- Python(一)
- python (一)
- Python(一)
- python笔记, python 生成注释
- jmeter 录制 移动端app (https)
- 使用java的List<map> 实现树状目录(后端部分)
- ElasticSerach在window上配置
- 配置本地windbg
- CentOS下ZooKeeper单机模式、集群模式安装
- 一、python 生成一维码
- Unix 分时系统的演化[资料]
- UITableViewController+定制单元格
- 获取网页源代码并解决字符乱码的问题
- C++设计模式-Flyweight享元模式
- 有趣的Java-J02
- 数据预处理demo
- DOM的几个基本属性
- RAM、SRAM、DRAM、SDRAM、DDRSDRAM等基本概念