Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sunknudsen opened this issue Oct 13, 2020 · 2 comments
Open

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

sunknudsen opened this issue Oct 13, 2020 · 2 comments

Comments

@sunknudsen
Copy link

sunknudsen commented Oct 13, 2020

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
Copy link
Author

sunknudsen commented Oct 13, 2020

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
Copy link

thank you so much! It actually worked!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants