python-barcode icon indicating copy to clipboard operation
python-barcode copied to clipboard

Create barcode without plain text

Open AxelRHD opened this issue 6 years ago • 6 comments

Is there an option to generate the barcode without the plain text?

AxelRHD avatar Feb 19 '19 09:02 AxelRHD

You can subclass writers and override the text render method. It probably makes sense to add a flag for this though, since it's a rather common request.

class ImageWitoutTextWriter(ImageWriter):
    def _paint_text(self, xpos, ypos):
        pass

WhyNotHugo avatar Feb 20 '19 18:02 WhyNotHugo

I'm added in class ImageWriter variable print_text. Default value is True, if need create barcode without text, writer=ImageWriter(False)

`class ImageWriter(BaseWriter):

    def __init__(self, print_text=True):
        BaseWriter.__init__(self, self._init, self._paint_module,
                            self._paint_text, self._finish)
        self.format = 'PNG'
        self.dpi = 300
        self._image = None
        self._draw = None
        self.print_text = print_text         

    def _init(self, code):
        size = self.calculate_size(len(code[0]), len(code), self.dpi)
        if not self.print_text:
            size = (size[0], int(size[1] * 0.7))
        self._image = Image.new('RGB', size, self.background)
        self._draw = ImageDraw.Draw(self._image)

    def _paint_module(self, xpos, ypos, width, color):
        size = [(mm2px(xpos, self.dpi), mm2px(ypos, self.dpi)),
                (mm2px(xpos + width, self.dpi),
                 mm2px(ypos + self.module_height, self.dpi))]
        self._draw.rectangle(size, outline=color, fill=color)

    def _paint_text(self, xpos, ypos):
        if self.print_text:
            font = ImageFont.truetype(FONT, self.font_size * 2)
            width, height = font.getsize(self.text)
            pos = (mm2px(xpos, self.dpi) - width // 2,
                   mm2px(ypos, self.dpi) - height // 4)
            self._draw.text(pos, self.text, font=font, fill=self.foreground)`

formgi avatar Apr 08 '19 08:04 formgi

Would you like to make that into a PR?

WhyNotHugo avatar Aug 10 '20 18:08 WhyNotHugo

For anyone reading this today, _paint_text has been renamed to _create_text.

georgek avatar Feb 05 '21 14:02 georgek

Was this ever integrated into main? I don't see it in https://github.com/WhyNotHugo/python-barcode/blob/main/barcode/writer.py

nabelekt avatar Mar 18 '21 05:03 nabelekt

For anyone reading this today, _paint_text has been renamed to _create_text.

Just to clarify, the rename is only on SVGWriter, in ImageWriter still uses the _paint_text

zipus avatar Apr 24 '24 11:04 zipus