plik
@@ -0,0 +1,40 @@ |
||
| 1 |
+# -*- coding: utf-8 -*- |
|
| 2 |
+ |
|
| 3 |
+from PIL import Image, ImageEnhance |
|
| 4 |
+from pyzbar import pyzbar |
|
| 5 |
+ |
|
| 6 |
+ |
|
| 7 |
+# The zbar DLLs are included with the Windows Python wheels. |
|
| 8 |
+# On other operating systems, you will need to install the zbar shared library. |
|
| 9 |
+# |
|
| 10 |
+# Mac OS X: |
|
| 11 |
+# brew install zbar |
|
| 12 |
+# |
|
| 13 |
+# Linux: |
|
| 14 |
+# sudo apt-get install libzbar0 |
|
| 15 |
+# |
|
| 16 |
+# Install this Python wrapper; use the second form to install dependencies of the command-line scripts: |
|
| 17 |
+# pip install pyzbar |
|
| 18 |
+ |
|
| 19 |
+ |
|
| 20 |
+def zbar(path): |
|
| 21 |
+ img = Image.open(path) |
|
| 22 |
+ |
|
| 23 |
+ # img = ImageEnhance.Brightness(img).enhance(2.0) # 增加亮度 |
|
| 24 |
+ # img = ImageEnhance.Sharpness(img).enhance(17.0) # 锐利化 |
|
| 25 |
+ # img = ImageEnhance.Contrast(img).enhance(4.0) # 增加对比度 |
|
| 26 |
+ # img = img.convert('L') # 灰度化
|
|
| 27 |
+ |
|
| 28 |
+ # img.show() |
|
| 29 |
+ |
|
| 30 |
+ barcodes = pyzbar.decode(img) |
|
| 31 |
+ |
|
| 32 |
+ # for barcode in barcodes: |
|
| 33 |
+ # barcodeData = barcode.data.decode("utf-8")
|
|
| 34 |
+ # print(barcodeData) |
|
| 35 |
+ |
|
| 36 |
+ return [bar.data.decode('utf-8') for bar in barcodes]
|
|
| 37 |
+ |
|
| 38 |
+ |
|
| 39 |
+if __name__ == '__main__': |
|
| 40 |
+ print zbar('zbar.jpg')
|