Skip to content

Commit 7bcb56c

Browse files
author
Hugo Osvaldo Barrera
committed
Add support for transparent backgrounds
Fixes #64
1 parent 60ec8b1 commit 7bcb56c

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

README.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,16 @@ Commandline::
135135
Changelog
136136
---------
137137

138+
v0.13.0
139+
~~~~~~~
140+
141+
* Added support for transparent backgrounds. This is done by setting the ``mode`` option
142+
for a writer to ``RGBA``.
143+
138144
v0.12.0
139145
~~~~~~~
140146

141-
* Removed `writer_options` from `barcode.get`. This parameter was not used.
147+
* Removed ``writer_options`` from ``barcode.get``. This parameter was not used.
142148
* Add a ``with_doctype`` flag to ``SVGWriter``. Set this to false to avoid including a
143149
``DOCTYPE`` in the resulting SVG.
144150
* Add support for ``Pillow>=8.0.0``.

barcode/writer.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,18 +332,30 @@ def write(self, content, fp: BinaryIO):
332332
else:
333333

334334
class ImageWriter(BaseWriter): # type: ignore
335-
def __init__(self):
335+
format: str
336+
mode: str
337+
dpi: int
338+
339+
def __init__(self, format="PNG", mode="RGB"):
340+
"""Initialise a new write instance.
341+
342+
:params format: The file format for the generated image. This parameter can
343+
take any value that Pillow accepts.
344+
:params mode: The colour-mode for the generated image. Set this to RGBA if
345+
you wish to use colours with transparency.
346+
"""
336347
BaseWriter.__init__(
337348
self, self._init, self._paint_module, self._paint_text, self._finish
338349
)
339-
self.format = "PNG"
350+
self.format = format
351+
self.mode = mode
340352
self.dpi = 300
341353
self._image = None
342354
self._draw = None
343355

344356
def _init(self, code):
345357
size = self.calculate_size(len(code[0]), len(code), self.dpi)
346-
self._image = Image.new("RGB", size, self.background)
358+
self._image = Image.new(self.mode, size, self.background)
347359
self._draw = ImageDraw.Draw(self._image)
348360

349361
def _paint_module(self, xpos, ypos, width, color):

tests/test_writers.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ def test_saving_image_to_byteio():
1818
with open(f"{TESTPATH}/somefile.jpeg", "wb") as f:
1919
EAN13("100000011111", writer=ImageWriter()).write(f)
2020

21+
def test_saving_rgba_image():
22+
rv = BytesIO()
23+
EAN13(str(100000902922), writer=ImageWriter()).write(rv)
24+
25+
with open(f"{TESTPATH}/ean13-with-transparent-bg.png", "wb") as f:
26+
writer = ImageWriter(mode="RGBA")
27+
28+
EAN13("100000011111", writer=writer).write(
29+
f, options={"background": "rgba(255,0,0,0)"}
30+
)
31+
2132

2233
def test_saving_svg_to_byteio():
2334
rv = BytesIO()

0 commit comments

Comments
 (0)