Python에서 QR 코드 이미지를 디코딩하는 방법은 무엇입니까?
TL; DR : (선호하는 순수) Python을 사용하여 이미지 파일에서 QR 코드를 디코딩하는 방법이 필요합니다.
Python을 사용하여 디코딩하려는 QR 코드가있는 jpg 파일이 있습니다. 나는 이것을 주장하는 몇 개의 라이브러리를 찾았습니다.
PyQRCode ( website here )는 다음과 같은 경로를 제공하여 이미지에서 qr 코드를 디코딩 할 수 있습니다.
import sys, qrcode
d = qrcode.Decoder()
if d.decode('out.png'):
print 'result: ' + d.result
else:
print 'error: ' + d.error
그래서 간단히 sudo pip install pyqrcode
. 그러나 위의 예제 코드에 대해 이상한 점은 가져 오기만한다는 것입니다 qrcode
( pyqrcode
그래도 아닙니다 ). qr 코드 이미지 만 생성 할 수있는 이 라이브러리 를 qrcode
참조 한다고 생각하기 때문에 다소 혼란 스러웠습니다. 그래서 위의 코드를 및 둘 다로 시도 했지만 둘 다 두 번째 줄에서 실패합니다 . 또한 웹 사이트 는 Ubuntu 8.10 (6 년 이상 전에 나왔음)을 참조하고 있으며 최신 커밋을 확인하기 위해 공개 (git 또는 기타) 저장소를 찾을 수 없습니다. 그래서 다음 도서관으로 이동했습니다.pyqrcode
qrcode
AttributeError: 'module' object has no attribute 'Decoder'
ZBar ( 여기 웹 사이트 )가 주장하고 있으므로 "an open source software suite for reading bar codes from various sources, such as image files."
Mac OSX 실행에 설치해 보았습니다 sudo pip install zbar
. 이것은 error: command 'cc' failed with exit status 1
. 나는 이 질문 에 대한 답변에서 제안을 시도했지만 해결할 수없는 것 같습니다. 그래서 다시 진행하기로 결정했습니다.
이 블로그 게시물 에 따르면 QRTools 는 다음 코드를 사용하여 이미지를 쉽게 디코딩 할 수 있습니다.
from qrtools import QR
myCode = QR(filename=u"/home/psutton/Documents/Python/qrcodes/qrcode.png")
if myCode.decode():
print myCode.data
print myCode.data_type
print myCode.data_to_string()
그래서 sudo pip install qrtools
아무것도 찾을 수없는을 사용하여 설치를 시도했습니다 . 나는 또한 그것을 시도 python-qrtools
, qr-tools
, python-qrtools
하지만 불행히도 아무 소용이, 그리고 몇 개 조합. ZBar를 기반으로한다고 말하는 이 repo 를 참조한다고 가정합니다 (위 참조). Heroku에서 내 코드를 실행하고 싶지만 (따라서 순수한 Python 솔루션을 선호 함) Linux 상자에 성공적으로 설치하고 sudo apt-get install python-qrtools
실행 해 보았습니다.
from qrtools import QR
c = QR(filename='/home/kramer65/qrcode.jpg')
c.data # prints u'NULL'
c.data_type # prints u'text'
c.data_to_string() # prints '\xef\xbb\xbfNULL' where I expect an int (being `1234567890`)
이것은 그것을 해독하는 것처럼 보이지만 올바르게 작동하지 않는 것 같습니다. 또한 ZBar가 필요하므로 순수한 Python이 아닙니다. 그래서 또 다른 도서관을 찾기로 결정했습니다.
PyXing ( website here )은 인기있는 Java ZXing 라이브러리 의 Python 포트로 추정 되지만 초기 및 유일한 커밋은 6 년이 지났으며 프로젝트에는 readme 나 문서가 전혀 없습니다.
나머지를 위해 몇 개의 qr -en coder ( de coder가 아님)와 디코딩 할 수있는 API 끝점을 찾았습니다 . 이 서비스가 다른 API 끝점에 종속되는 것을 원하지 않기 때문에 디코딩을 로컬로 유지하고 싶습니다.
결론적으로 (순수한) Python의 이미지에서 QR 코드를 디코딩하는 방법을 아는 사람이 있습니까? 모든 팁을 환영합니다!
다음 단계와 코드를 사용하여 시도 할 수 있습니다 qrtools
.
qrcode
아직 존재하지 않는 경우 파일 만들기- 나는 이것을 사용
pyqrcode
하여 설치할 수 있습니다.pip install pyqrcode
그런 다음 코드를 사용하십시오.
>>> import pyqrcode >>> qr = pyqrcode.create("HORN O.K. PLEASE.") >>> qr.png("horn.png", scale=6)
- 나는 이것을 사용
다음을
qrcode
사용하여 기존 파일 디코딩qrtools
- 다음을
qrtools
사용하여 설치sudo apt-get install python-qrtools
이제 파이썬 프롬프트에서 다음 코드를 사용하십시오.
>>> import qrtools >>> qr = qrtools.QR() >>> qr.decode("horn.png") >>> print qr.data u'HORN O.K. PLEASE.'
- 다음을
다음은 단일 실행의 전체 코드입니다.
In [2]: import pyqrcode
In [3]: qr = pyqrcode.create("HORN O.K. PLEASE.")
In [4]: qr.png("horn.png", scale=6)
In [5]: import qrtools
In [6]: qr = qrtools.QR()
In [7]: qr.decode("horn.png")
Out[7]: True
In [8]: print qr.data
HORN O.K. PLEASE.
주의 사항
PyPNG
사용을pip install pypng
위해 사용하여 설치해야 할 수도 있습니다.pyqrcode
PIL
설치 한 경우IOError: decoder zip not available
. 이 경우 다음을 사용하여 제거하고 다시 설치하십시오PIL
.pip uninstall PIL pip install PIL
그래도 작동하지 않으면
Pillow
대신 사용해보십시오.pip uninstall PIL pip install pillow
다음 코드는 저에게 잘 작동합니다.
brew install zbar
pip install pyqrcode
pip install pyzbar
QR 코드 이미지 생성 :
import pyqrcode
qr = pyqrcode.create("test1")
qr.png("test1.png", scale=6)
QR 코드 디코딩의 경우 :
from PIL import Image
from pyzbar.pyzbar import decode
data = decode(Image.open('test1.png'))
print(data)
결과를 인쇄합니다.
[Decoded(data=b'test1', type='QRCODE', rect=Rect(left=24, top=24, width=126, height=126), polygon=[Point(x=24, y=24), Point(x=24, y=150), Point(x=150, y=150), Point(x=150, y=24)])]
Windows + Python 2.7 64 비트에서 작동하도록하는 데 거의 30 분이 소요되었으므로 다음은 허용되는 답변에 대한 추가 참고 사항입니다.
Install it with
pip install zbar-0.10-cp27-none-win_amd64.whl
If Python reports an
ImportError: DLL load failed: The specified module could not be found.
when doingimport zbar
, then you will just need to install the Visual C++ Redistributable Packages for VS 2013 (I spent a lot of time here, trying to recompile unsuccessfully...)Required too: libzbar64-0.dll must be in a folder which is in the PATH. In my case I copied it to "C:\Python27\libzbar64-0.dll" (which is in the PATH)
Then do
pip install pyqrcode pip install qrtools pip install Pillow
and the code from the main answer should work:
import pyqrcode
qr = pyqrcode.create("HORN O.K. PLEASE.")
qr.png("horn.png", scale=6)
import qrtools
qr = qrtools.QR()
qr.decode("horn.png")
print qr.data
참고URL : https://stackoverflow.com/questions/27233351/how-to-decode-a-qr-code-image-in-preferably-pure-python
'program tip' 카테고리의 다른 글
fb : app_id 또는 fb : admins는 언제 필요합니까? (0) | 2020.11.02 |
---|---|
std :: array에 배열을 채울 값을 취하는 생성자가없는 이유는 무엇입니까? (0) | 2020.11.02 |
PHP DateTime 마이크로 초는 항상 0을 반환합니다. (0) | 2020.11.02 |
인터페이스의 명시 적 구현이 공개 될 수없는 이유는 무엇입니까? (0) | 2020.11.02 |
ASP.NET MVC : 숨겨진 필드 값은 HtmlHelper.Hidden을 사용하여 렌더링되지 않습니다. (0) | 2020.11.02 |