安裝:
pip install Pillow
pip install python-barcode
生成條形碼基本方法: https://pypi.org/project/python-barcode/0.8.1/
- 使用barcode生成圖像PIL對象用於顯示,或者 保存圖像
import matplotlib.pyplot as plt
import barcode
from barcode.writer import ImageWriter
bar = barcode.get(u'code39', u"test123456",writer=ImageWriter())
output = bar.render(writer_options={"format": "PNG"}) #渲染生成圖像對象
plt.imshow(output)
plt.axis('off') # 不顯示坐標軸
plt.show()
bar.save("11",options={"format": "JPEG"})#保存圖形里有渲染然后保存到文件
- 官網里有直接保存圖像的方法:
name = barcode.generate(u'code128', u'test123456',writer=ImageWriter(), output='barcode_png')
import matplotlib.pyplot as plt # plt 用於顯示圖片
import matplotlib.image
im = image.imread(name) # 讀取圖片文件
plt.imshow(im) # 顯示圖片
plt.axis('off') # 不顯示坐標軸
plt.show()
os.remove(name)
- 上述方法:
- 參數write 為 NONE,保存圖像文件格式則是 svg,
- 參數為ImageWrite()則默認圖片格式是 PNG,需要改成JPEG等,則需要在options={"format": "JPEG"},進行設置