Read QR code from image file with Python (Mac)

Use ZBar and the Python module https://pypi.python.org/pypi/zbar/. It also supports barcodes other than QR codes.

In order to streamline the aggregation of experimental results using paper cards, I made a note that I prepared a QR code in the card in advance and automatically processed it with Python after scanning.

Environment, version

Installation

ZBar

One-shot installation with Homebrew.

$ brew install zbar

Python zbar module

I'd like to install this with PyPI in one shot, but if I put it in as it is, Python will die with a Segmentation fault at the time of import.

$ pip install zbar

$ python
Python 2.7.9 (default, Jan  7 2015, 11:50:42) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import zbar
Segmentation fault: 11

$ pip uninstall zbar

Therefore, apply Patch to the source code and install it. In this patch, we add sentinel to the array to prevent the occurrence of Segmantation fault.

$ wget https://pypi.python.org/packages/source/z/zbar/zbar-0.10.tar.bz2
$ wget https://github.com/npinchot/zbar/commit/d3c1611ad2411fbdc3e79eb96ca704a63d30ae69.diff
$ tar jxvf zbar-0.10.tar.bz2
$ cd zbar-0.10
$ patch -p1 < ../d3c1611ad2411fbdc3e79eb96ca704a63d30ae69.diff
patching file imagescanner.c
$ python setup.py install

$ python
Python 2.7.9 (default, Jan  7 2015, 11:50:42) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import zlib
>>>

This time it was okay.

Python Pillow module

Enter with one PyPI.

$ pip install Pillow

Extract the QR code from the image file

Python test code.

zbar_test.py

zbar_test.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

### cf. http://99blues.dyndns.org/blog/2010/12/zbar/

import sys
import zbar
import PIL.Image
 
if len(sys.argv) < 2: exit(1)
 
# create a reader
scanner = zbar.ImageScanner()
 
# configure the reader
scanner.parse_config('enable')
 
# obtain image data
pil = PIL.Image.open(sys.argv[1]).convert('L')
(width, height) = pil.size
raw = pil.tostring()
 
# wrap image data
image = zbar.Image(width, height, 'Y800', raw)
 
# scan the image for barcodes
scanner.scan(image)
 
# extract results
for symbol in image:
    # do something useful with results
    print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data
 
# clean up
del(image)
$ ./zbar_test.py /path/to/image.jpg
decoded QRCODE symbol "http://www.example.com/"

reference

Recommended Posts

Read QR code from image file with Python (Mac)
Read line by line from a file with Python
Read fbx from python with cinema4d
Install python with mac vs code
Read CSV file with python (Download & parse CSV file)
Let's read the RINEX file with Python ①
Image acquisition from camera with Python + OpenCV
Read a file in Python with a relative path from the program
[Automation] Read mail (msg file) with Python
[Implementation example] Read the file line by line with Cython (Python) from the last line
How to read a CSV file with Python 2/3
Image processing from scratch with python (5) Fourier transform
[Python] How to read excel file with pandas
Medical image analysis with Python 1 (Read MRI image with SimpleITK)
Read table data in PDF file with Python
Image processing from scratch with python (4) Contour extraction
Convert the character code of the file with Python3
Wav file generation from numeric text with python
I made a QR code image with CuteR
Image processing with Python
Read Python csv file
[Python] Read From Stdin
How to scrape image data from flickr with python
Read json file with Python, format it, and output json
Run iphone safari from mac with python + selenium + safari-webdriver
[Note] How to write QR code and description in the same image with python
Get country code with python
Image processing with Python (Part 2)
Python with VS Code (Windows 10)
Read csv with python pandas
Read image coordinates with Python-matplotlib
Image editing with python OpenCV
Python3 + Django ~ Mac ~ with Apache
Draw netCDF file with python
Sorting image files with Python (2)
Sorting image files with Python (3)
Debug Python with VS Code
Image processing with Python (Part 1)
Tweet with image in Python
Sorting image files with Python
Stop Omxplayer from Python code
Image processing with Python (Part 3)
Generate QR code in Python
With skype, notify with skype from python!
Download csv file with python
Using OpenCV with Python @Mac
Document Python code with Doxygen
[Python] Image processing with scikit-image
Read json data with python
Prepare Python development environment with Mac + Windows + VisualStudio Code (Windows version)
Prepare Python development environment with Mac + Windows + VisualStudio Code (Mac version)
Mass generation of QR code with character display by Python
Python> Read from a multi-line string instead of a file> io.StringIO ()
[Python] Read command line arguments from file name or stdin
How to read an Excel file (.xlsx) with Pandas [Python]
Python: Extract file information from shared drive with Google Drive API
Execute Python script from batch file
Decrypt the QR code with CNN
Cut out an image with python
Extract the xz file with python
Call C from Python with DragonFFI