Put the number under the barcode using pyBarcode

I use pyBarcode to create a PNG, and the number below the barcode gets cut off to the right. How do I push, few pixels left?

the number is getting cut off

According to the documentation I need to do something like this:

 barcode.writer.BaseWriter(paint_text=my_callback) 

And define the callback as follows:

 my_callback(xpos, ypos) 

and

 use self.text as text 

How can I apply all this to my Django view (below)?

 def barcode(request): import barcode from barcode.writer import ImageWriter from cStringIO import StringIO def mm2px(mm, dpi=300): return (mm * dpi) / 25.4 class MyImageWriter(ImageWriter): def calculate_size(self, modules_per_line, number_of_lines, dpi=300): width = 2 * self.quiet_zone + modules_per_line * self.module_width height = 1.0 + self.module_height * number_of_lines if self.text: height += (self.font_size + self.text_distance) / 3 return int(mm2px(width, dpi)), int(mm2px(height, dpi)) f = BarcodeForm(request.GET) if f.is_valid(): try: i = StringIO() bc_factory = barcode.get_barcode_class(f.PYBARCODE_TYPE[f.cleaned_data['barcode_type']]) bc_factory.default_writer_options['quiet_zone'] = 1.0 bc_factory.default_writer_options['text_distance'] = 1.0 bc_factory.default_writer_options['module_height'] = 15.0 bc_factory.default_writer_options['module_width'] = 0.3 bc_factory.default_writer_options['font_size'] = 46 bc = bc_factory(f.cleaned_data['text'], writer=MyImageWriter()) bc.write(i) return HttpResponse(i.getvalue(), mimetype='image/png') except Exception, e: return HttpResponseBadRequest(str(e)) else: return HttpResponseBadRequest('Missing text or unsupported barcode type: %s' % f.errors) 
+4
source share
1 answer

Edit: after the answer, I noticed that you have a factory that sets quiet_zone to 1.0 . Change it to 6.5 and I assume that it will look normal.

Edit2: I misunderstood the exact problem you are facing.

For some reason, the author of pyBarcode places the text in the center of the barcode. When the render method calls _paint_text() , it goes into xpos/2 , which sets it in the middle of the barcode. I assume this is normal with the default font used, but when you enlarge the font, it no longer fits.

Instead, I was able to put it on the left side by overriding the _paint_text() method. In the last line below, the pos variable is just a tuple containing the coordinate (x, y) that tells PIL where to draw the text on the barcode. So I made sure that x is built using a barcode. If you need it to be right-aligned, you can play with the xpos variable to get it where you need it.

Take a picture:

 class MyImageWriter(ImageWriter): def calculate_size(self, modules_per_line, number_of_lines, dpi=300): width = 2 * self.quiet_zone + modules_per_line * self.module_width height = 1.0 + self.module_height * number_of_lines if self.text: height += (self.font_size + self.text_distance) / 3 return int(mm2px(width, dpi)), int(mm2px(height, dpi)) def _paint_text(self, xpos, ypos): # this should align your font to the left side of the bar code: xpos = self.quiet_zone pos = (mm2px(xpos, self.dpi), mm2px(ypos, self.dpi)) font = ImageFont.truetype(FONT, self.font_size) self._draw.text(pos, self.text, font=font, fill=self.foreground) 
+1
source

All Articles