mnooner256 / pyqrcode

Python 3 module to generate QR Codes
BSD 3-Clause "New" or "Revised" License
408 stars 74 forks source link

ValueError: buffer size must be a multiple of element size #78

Open sunknudsen opened 3 years ago

sunknudsen commented 3 years ago

Trying to feed a PNG buffer to np.frombuffer and getting a buffer size error.

New to Python... would someone happen to know what is going on and how to fix this?

from cv2 import cv2
import pyqrcode
import io
import numpy as np
import sys

buffer = io.BytesIO()
qr = pyqrcode.create("foo")
qr.png(buffer, scale=2)

array = np.frombuffer(buffer.getvalue())
image = cv2.imdecode(array, cv2.IMREAD_COLOR)

cv2.imshow('QR code', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

sys.exit()

Thanks!

sunknudsen commented 3 years ago

Found a fix... does the following code look good to you?

from cv2 import cv2
import pyqrcode
import io
import numpy as np
import sys

buffer = io.BytesIO()
qr = pyqrcode.create("foo")
qr.png(buffer, scale=2)

buffer.seek(0)

array = np.asarray(bytearray(buffer.read()), dtype=np.uint8)
image = cv2.imdecode(array, cv2.IMREAD_COLOR)

cv2.imshow('QR code', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

sys.exit()
Ashutosh-Vermaa commented 2 years ago

thank you so much! It actually worked!