2011-07-06 25 views
4

pyBarcodeを使用して、PNGを生成しています。バーコードの下の数字が右に切り取られています。それを数ピクセル左に微調整するにはどうすればよいですか?pyBarcodeを使用して番号をバーコードの下に配置します。

barcode.writer.BaseWriter(paint_text=my_callback) 

そして、このようにしてコールバック関数を定義します:

my_callback(xpos, ypos) 

と:

use self.text as text 

the number is getting cut off

the documentationによると、私はこのような何かをする必要があり3210

これを私の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) 
+0

はこの打撃を与えます。 –

+0

@Craig私はしました:https://bitbucket.org/whitie/pybarcode/issue/6/how-to-position-the-text-below-the-barcode – ram1

答えて

1

編集:答えた後、私はあなたが1.0quiet_zone設定の工場を持って気づきました。それを6.5に戻してください。うまく見えると思います。

Edit2:私が経験した正確な問題を誤解しました。

pyBarcodeの著者は何らかの理由で、バーコードの中央にテキストを配置しています。レンダーメソッドが_paint_text()を呼び出すと、xpos/2が渡され、バーコードの中央に設定されます。私はこれが彼が使用しているデフォルトのフォントで大丈夫だと思いますが、あなたがフォントを増やしても、それはもはや適合しません。

代わりに、_paint_text()メソッドをオーバーライドして左側に配置することができました。下の最後の行では、変数posは、バーコードにテキストを描画する場所をPILに指示する(x、y)座標を含む単なるタプルです。だから私はxがバーコードと並んでいることを確認した。それを右揃えにする必要がある場合は、xpos変数を使って遊んで、必要な場所に移動することができます。あなたはpyBarcodeの作者にバグとして報告でき

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) 
+0

[そのスクリーンショット](http:// dl .dropbox.com/u/26816257/barcode_quiet.png) 'quiet_zone'が増えると、バーコードの下の数字が見えるようになりますが、それは中心から外れています。普通のバーコードのように見えるようにする必要があります。 – ram1

+0

私は参照してください。上記を確認し、私の回答を編集しました。うまくいけば、あなたの問題を解決します。 –

+0

_paint_text関数を追加しましたが、バーコードがまったく表示されません。私はまた、 'import os'と' PIL import Image、ImageDraw、ImageFont'から 'PATH = os.path.dirname(os.path.abspath(__ file __))'と 'FONT = os 'を追加しようとしました。 [this](https://bitbucket.org/whitie/pybarcode/src/1400018306e5/barcode/writer.py)には、パスコードがありません(PATH、 'DejaVuSansMono.ttf')。 。思考? – ram1

関連する問題