# pip install pyzbar
from pyzbar.pyzbar import decode
# pip install Pillow
from PIL import Image
image = 'test.png'
img = Image.open(image)
barcodes = decode(img)
for barcode in barcodes:
url = barcode.data.decode("utf-8")
print(url)
识别学校家具二维码
注意学校二维码不是常规编码,解析的时候需要一些操作,具体看里面的注释。
其实最好是能调用微信的扫一扫,识别率很高。
import warnings
warnings.filterwarnings("ignore")
# pip install pyzbar
from pyzbar.pyzbar import decode
# pip install Pillow
from PIL import Image
import os
# paser the qr code of facilities
# input
# str: img_path, the qr image path
# return the parser result (string)
def facility_QR_parser(img_path):
QRimg = Image.open(img_path)
barcodes = decode(QRimg)
for barcode in barcodes:
url = barcode.data.decode('utf-8')
# print(url)
# 转成utf-8会乱码,经玄学操作,我们决定。
# 玄学秘籍:http://mytju.com/classcode/tools/messyCodeRecover.asp
# 注意这里“现在编码”和“原来编码”需要反复细品
# 也就是说,encode和decode方向来回都试一下
# 比如对于:¿ªÃÅÎļþ¹ñ
# 原来编码写的是'gbk',现在编码写的是'iso-8859-1'
# 我们先转了'gbk',之后转'iso-8859-1',但是还是失败
# 这里先转'iso-8859-1',再转'gbk'
url = bytes(url, encoding = 'iso-8859-1')
url = url.decode('gbk')
return url
# 把所有文件按照顺序命名
def rename_all_files():
allFiles = [f for f in os.listdir() if '.JPG' in f]
for i in range(len(allFiles)):
os.rename(file, "{}.JPG".format(i+1))
# print(facility_QR_parser("65ae5bc67c52a356a471bb8b799e633.jpg"))
if __name__ == '__main__':
rename_all_files()
allFiles = [f for f in os.listdir() if '.JPG' in f]
result = []
# print(allFiles)
for file in allFiles:
if "-" in file:
pass
tmp = [file, facility_QR_parser(file)]
os.rename(tmp[0], "{}-{}.JPG".format(tmp[0][:-4], tmp[1]))
result.append(tmp)
for i in result:
print(i)