pure.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from pymaging import Image
  2. from pymaging.colors import RGB
  3. from pymaging.formats import registry
  4. from pymaging.shapes import Line
  5. from pymaging.webcolors import Black, White
  6. from pymaging_png.png import PNG
  7. import qrcode.image.base
  8. class PymagingImage(qrcode.image.base.BaseImage):
  9. """
  10. pymaging image builder, default format is PNG.
  11. """
  12. kind = "PNG"
  13. allowed_kinds = ("PNG",)
  14. def __init__(self, *args, **kwargs):
  15. """
  16. Register PNG with pymaging.
  17. """
  18. registry.formats = []
  19. registry.names = {}
  20. registry._populate()
  21. registry.register(PNG)
  22. super(PymagingImage, self).__init__(*args, **kwargs)
  23. def new_image(self, **kwargs):
  24. return Image.new(RGB, self.pixel_size, self.pixel_size, White)
  25. def drawrect(self, row, col):
  26. (x, y), (x2, y2) = self.pixel_box(row, col)
  27. for r in range(self.box_size):
  28. line_y = y + r
  29. line = Line(x, line_y, x2, line_y)
  30. self._img.draw(line, Black)
  31. def save(self, stream, kind=None):
  32. self._img.save(stream, self.check_kind(kind))
  33. def check_kind(self, kind, transform=None, **kwargs):
  34. """
  35. pymaging (pymaging_png at least) uses lower case for the type.
  36. """
  37. if transform is None:
  38. transform = lambda x: x.lower()
  39. return super(PymagingImage, self).check_kind(
  40. kind, transform=transform, **kwargs)