我們的生活已完全離不開一維碼和二維碼,本文會簡單的介紹如果通過python的方法來生成它們
本文環境:
Python 2.7.10
pyBarcode==0.7
Pillow==5.1.0
pip install pyBarcode
pip install Pillow
(env) python manage.py shell
Python 2.7.10 (default, May 23 2015, 09:40:32)[MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright","credits" or "license" for more information.
(InteractiveConsole)
>>> from barcode.writer importImageWriter
>>> from barcode.codex import Code39
>>> from PIL import Image, ImageDraw,ImageFont, ImageWin
>>> imagewriter = ImageWriter()
>>> ean =Code39("1234567890", writer=imagewriter, add_checksum=False)
>>> fullname = ean.save('image')
>>> img = Image.open(fullname)
>>> img.show()
首先我們導入Code39,pyBarcode支持多種方式,可用下面命令獲取
>>> import barcode
>>> barcode.PROVIDED_BARCODES
['code39', 'ean', 'ean13', 'ean8', 'gs1','gtin', 'isbn', 'isbn10', 'isbn13', 'issn', 'jan', 'pzn', 'upc', 'upca']
add_checksum 表示是否將checksum添加到code中
ean.save()參數為文件名,這個文件名可以是絕對路徑或者相對路徑。
最後,調用了PIL的Image來顯示這個生成的圖片,如下:
>>> from StringIO import StringIO
>>> i = StringIO()
>>> ean =Code39("0987654321", writer=imagewriter, add_checksum=False)
>>> ean.write(i)
>>> f = StringIO(i.getvalue())
>>> img1 = Image.open(f)
>>> print 'save to StringIO and showwith image format'
save to StringIO and show with image format
>>> img1.show()
圖片打開時是從StringIO中把內容讀出來。
從上面圖片效果看,文字離一維碼距離實在太遠了,需要調整一下。差了一下,基本的Writer有下面選項,還有繼承類的SVGWritr和ImageWriter等,對我的需要來說,基本的就夠了,可以通過調整text_distance和font_size來調整效果
module_width:
The width of one barcode module in mm as float. Defaults to 0.2.
module_height:
The height of the barcode modules in mm as float. Defaults to 15.0.
quiet_zone:
Distance on the left and on the right from the border to the first (last) barcode module in mm as float. Defaults to 6.5.
font_size:
Font size of the text under the barcode in pt as integer. Defaults to 10.
text_distance:
Distance between the barcode and the text under it in mm as float. Defaults to 5.0.
background:
The background color of the created barcode as string. Defaults to white.
foreground:
The foreground and text color of the created barcode as string. Defaults to black.
center_text:
If true (the default) the text is centered under the barcode else left aligned.
有兩種方法可以將這個選項參數傳遞給Writer
All writer take the following options(specified as keyword arguments to Barcode.save(filename, option=value) or setvia Writer.set_options(option=value)).
實際使用中,Writer.set_options並沒有生效,不確定是否我的方法用錯了。
options = {"text_distance":1, "font_size":12}
imagewriter = ImageWriter()
imagewriter.set_options(options=options)
改用Barcode.save方法
options = {"text_distance":1,"font_size":12}
ean = Code39(filename, writer=imagewriter,add_checksum=False)
ean.save(fullpath,options=options)
圖片效果如下
詳細內容可查看https://pythonhosted.org/pyBarcode/writers/index.html
pip install qrcode
定義二維碼生成函數,將網址放入二維碼是常用場景,我們就以這個為例
在qrcode.QRCode初始化函數裡,可以定義相應參數,官方資料非常吝嗇,下面是網上找到的一些用法
defgen_qrcode(link):
qr=qrcode.QRCode(
version =2,
error_correction =qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=10,)
qr.add_data(link)
qr.make(fit=True)
img = qr.make_image()
img.show()
photopath = os.path.join(settings.MEDIA_ROOT,"test")
ifnot os.path.exists(photopath):
os.makedirs(photopath)
path = os.path.join(photopath,'create.jpg')
img.save(path)
return path
生成的圖片如下
暫時不會,哈哈
關注下方公眾號獲取更多文章
參考文檔pyBarcode 0.7https://pypi.python.org/pypi/pyBarcode
Pillow-4.2.1https://pypi.python.org/pypi/Pillow
pywin32https://pypi.python.org/pypi/pywin32/
https://pycode.readthedocs.io/en/latest/
https://pythonhosted.org/pyBarcode/index.html