lincolnloop / python-qrcode

Python QR Code image generator
https://pypi.python.org/pypi/qrcode
Other
4.36k stars 666 forks source link

NumPy backport #183

Closed KmolYuan closed 1 year ago

KmolYuan commented 4 years ago

Hello! This is a image factory for NumPy array. (for NumPy and OpenCV users)

from PyQt5.QtGui import QImage
import qrcode
from qrcode.image.numpy import NpImage
image = qrcode.make(self.compressed_script, image_factory=NpImage)
pixmap = QPixmap.fromImage(image.get_qimage(QImage))
maribedran commented 3 years ago

@KmolYuan I couldn't run the code example you provided. Can you provide a working example with only Numpy without PyQt5?

KmolYuan commented 3 years ago

I'm sorry for the horrible things I once wrote.

The returned image can be used as a normal numpy array with size w x w x 3.

>>> import qrcode
>>> from qrcode.image.numpy import NpImage
>>> image = qrcode.make("example", image_factory=NpImage)
>>> image.shape
(290, 290, 3)
>>> image[0, 0]
array([255, 255, 255], dtype=uint8)
>>> from numpy import array
>>> array(image)  # Convert to np.array
array([[[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]]], dtype=uint8)
maribedran commented 3 years ago

@KmolYuan thank you for the updates! I was able to test this locally.

I'm still not sure about this PR, though. The save method is not implemented and there's no way to actually see an image and scan it. If there's a use case for this factory, it can always be shipped as a separate package, but I think it needs a minimum compatibility with the default interface to be included in the package.

KmolYuan commented 3 years ago

If necessary, I can use a pure python function to achieve.

KmolYuan commented 3 years ago
import qrcode
from qrcode.image.numpy import NpImage
image = qrcode.make("example", image_factory=NpImage)
image.save("example.png")

example2

maribedran commented 3 years ago

@KmolYuan I fixed the save method to be able to save to a stream and added a test, but we'd need more test for this factory as it breaks the coverage tests (under 98%). Could you make sure we have more test coverage for this?

KmolYuan commented 3 years ago

Simplified to gray-scale, same as PIL image.