forked from lincolnloop/python-qrcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'lincolnloop:master' into master
- Loading branch information
Showing
11 changed files
with
100 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,52 @@ | ||
from itertools import chain | ||
|
||
import png | ||
|
||
import qrcode.image.base | ||
from pymaging import Image # type: ignore | ||
from pymaging.colors import RGB # type: ignore | ||
from pymaging.formats import registry # type: ignore | ||
from pymaging.shapes import Line # type: ignore | ||
from pymaging.webcolors import Black, White # type: ignore | ||
from pymaging_png.png import PNG # type: ignore | ||
|
||
|
||
class PymagingImage(qrcode.image.base.BaseImage): | ||
class PyPNGImage(qrcode.image.base.BaseImage): | ||
""" | ||
pymaging image builder, default format is PNG. | ||
pyPNG image builder. | ||
""" | ||
|
||
kind = "PNG" | ||
allowed_kinds = ("PNG",) | ||
|
||
def __init__(self, *args, **kwargs): | ||
""" | ||
Register PNG with pymaging. | ||
""" | ||
registry.formats = [] | ||
registry.names = {} | ||
registry._populate() | ||
registry.register(PNG) | ||
|
||
super().__init__(*args, **kwargs) | ||
needs_drawrect = False | ||
|
||
def new_image(self, **kwargs): | ||
return Image.new(RGB, self.pixel_size, self.pixel_size, White) | ||
return png.Writer(self.pixel_size, self.pixel_size, greyscale=True, bitdepth=1) | ||
|
||
def drawrect(self, row, col): | ||
(x, y), (x2, y2) = self.pixel_box(row, col) | ||
for r in range(self.box_size): | ||
line_y = y + r | ||
line = Line(x, line_y, x2, line_y) | ||
self._img.draw(line, Black) | ||
|
||
def save(self, stream, kind=None): | ||
self._img.save(stream, self.check_kind(kind)) | ||
|
||
def check_kind(self, kind, transform=None, **kwargs): | ||
""" | ||
pymaging (pymaging_png at least) uses lower case for the type. | ||
Not used. | ||
""" | ||
|
||
def lower_case(x): | ||
return x.lower() | ||
|
||
return super().check_kind(kind, transform=transform or lower_case, **kwargs) | ||
def save(self, stream, kind=None): | ||
self._img.write(stream, self.rows_iter()) | ||
|
||
def rows_iter(self): | ||
yield from self.border_rows_iter() | ||
border_col = [1] * (self.box_size * self.border) | ||
for module_row in self.modules: | ||
row = ( | ||
border_col | ||
+ list( | ||
chain.from_iterable( | ||
([not point] * self.box_size) for point in module_row | ||
) | ||
) | ||
+ border_col | ||
) | ||
for _ in range(self.box_size): | ||
yield row | ||
yield from self.border_rows_iter() | ||
|
||
def border_rows_iter(self): | ||
border_row = [1] * (self.box_size * (self.width + self.border * 2)) | ||
for _ in range(self.border * self.box_size): | ||
yield border_row | ||
|
||
|
||
# Keeping this for backwards compatibility. | ||
PymagingImage = PyPNGImage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters