日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python使用python-barcode生成任意长度字符串的条形码(一维码)详细教程

發布時間:2025/3/11 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python使用python-barcode生成任意长度字符串的条形码(一维码)详细教程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用python-barcode生成一維碼

1.安裝python包

pip install Pillow pip install python-barcode

2.實例程序

# -*- coding: utf-8 -*-import barcode import random from barcode.writer import ImageWriter import os from PIL import Image#生成一維碼的類型的種類 """ [u'code39', u'code128', u'ean', u'ean13', u'ean8', u'gs1', u'gtin',u'isbn', u'isbn10', u'isbn13', u'issn', u'jan', u'pzn', u'upc', u'upca'] """#生成一維碼,參數:碼類型、碼內容、文件名(文件名后綴自動加.png) def barcode_to_png(barcode_type,text_str,filename):EAN = barcode.get_barcode_class(barcode_type) #設置生成一維碼的類型ean = EAN(text_str, writer=ImageWriter())ean.save(filename)#8個一維碼進行2*4的方式合并存成一張圖,參數:碼類型、文件數量 def barcode_fun(barcode_type,file_num):for i in range(8*file_num):r_num = random.randint(100000000000000, 999999999999999)barcode_to_png(barcode_type,str(r_num),'./code/'+barcode_type+'/'+str(i))if( (i+1)%8 == 0 ):base_img0 = Image.open('./code/'+barcode_type+'/'+str(i-7)+'.png')base_img1 = Image.open('./code/'+barcode_type+'/'+str(i-6)+'.png')base_img2 = Image.open('./code/'+barcode_type+'/'+str(i-5)+'.png')base_img3 = Image.open('./code/'+barcode_type+'/'+str(i-4)+'.png')base_img4 = Image.open('./code/'+barcode_type+'/'+str(i-3)+'.png')base_img5 = Image.open('./code/'+barcode_type+'/'+str(i-2)+'.png')base_img6 = Image.open('./code/'+barcode_type+'/'+str(i-1)+'.png')base_img7 = Image.open('./code/'+barcode_type+'/'+str(i)+'.png')x = base_img0.size[0]y = base_img0.size[1]target = Image.new('RGBA', (2*x,4*y), color=0)target.paste(base_img0,(0,0,x,y))target.paste(base_img1,(x,0,2*x,y))target.paste(base_img2,(0,y,x,2*y))target.paste(base_img3,(x,y,2*x,2*y))target.paste(base_img4,(0,2*y,x,3*y))target.paste(base_img5,(x,2*y,2*x,3*y))target.paste(base_img6,(0,3*y,x,4*y))target.paste(base_img7,(x,3*y,2*x,4*y))target.save('./code/'+barcode_type+'/'+barcode_type+"_"+str((i+1)/8)+'.png')os.system("rm "+'./code/'+barcode_type+'/'+str(i-7)+'.png')os.system("rm "+'./code/'+barcode_type+'/'+str(i-6)+'.png')os.system("rm "+'./code/'+barcode_type+'/'+str(i-5)+'.png')os.system("rm "+'./code/'+barcode_type+'/'+str(i-4)+'.png')os.system("rm "+'./code/'+barcode_type+'/'+str(i-3)+'.png')os.system("rm "+'./code/'+barcode_type+'/'+str(i-2)+'.png')os.system("rm "+'./code/'+barcode_type+'/'+str(i-1)+'.png')os.system("rm "+'./code/'+barcode_type+'/'+str(i)+'.png')#主函數設置需要生成碼類型的目錄,生成相應碼。 if __name__=="__main__":os.system("mkdir -p ./code")os.system("mkdir -p ./code/code128")os.system("mkdir -p ./code/EAN13")os.system("mkdir -p ./code/code39")os.system("mkdir -p ./code/upca")code128 = 'code128'ean13 = 'EAN13'code39 = 'code39'upca = 'upca'barcode_fun(code128,10)barcode_fun(ean13,10)barcode_fun(code39,10)barcode_fun(upca,10)

3.結果
在當前目錄下的code目錄下生成了相應的目錄和文件

?

code128_1.png作為實例展示效果為:

?

總結

以上是生活随笔為你收集整理的python使用python-barcode生成任意长度字符串的条形码(一维码)详细教程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。