使用pyBarcode将数字定位在条形码下方

ram*_*am1 5 python django

我正在使用pyBarcode生成PNG,条形码下方的数字在右侧被切断.如何轻推几个像素?

这个数字正在被切断

根据文档,我需要做这样的事情:

barcode.writer.BaseWriter(paint_text=my_callback)
Run Code Online (Sandbox Code Playgroud)

并定义一个这样的回调:

my_callback(xpos, ypos)
Run Code Online (Sandbox Code Playgroud)

和:

use self.text as text
Run Code Online (Sandbox Code Playgroud)

我究竟如何将所有这些应用到我的Django视图(下面)?

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)
Run Code Online (Sandbox Code Playgroud)

Jos*_*osh 1

编辑:回答后我注意到你有一个工厂正在quiet_zone1.0. 把它改回来6.5,我想它看起来会很好。

Edit2:我误解了您遇到的确切问题。

无论出于何种原因,作者都pyBarcode将文本放在条形码的中间居中。当 render 方法调用时,_paint_text()它会传入xpos/2,将其设置在条形码的中间。我想这对于他使用的默认字体来说是可以的,但是当你增加字体时,它就不再适合了。

相反,我可以通过重写该方法将其放置在左侧_paint_text()。在下面的最后一行中,变量pos只是一个包含 (x,y) 坐标的元组,该坐标告诉 PIL 在条形码上的何处绘制文本。所以我确保 x 与条形码对齐。如果您需要将其正确对齐,您可以使用该xpos变量将其放置在您需要的位置。

试一试:

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)
Run Code Online (Sandbox Code Playgroud)