【问题标题】:Adjusting size of barcode image output调整条码图像输出大小
【发布时间】:2019-06-30 16:32:45
【问题描述】:

我正在尝试调整条码输出的大小

import barcode
from barcode.writer import ImageWriter

bar_class = barcode.get_barcode_class('code128')
barcode = '1234567890'
writer=ImageWriter()
writer.set_options({module_width:2, module_height:2})
code128 = bar_class(barcode, writer)
code128.save('filename')

我得到的错误是:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'module_width' is not defined

我不太明白如何使用此处找到的文档:https://pythonhosted.org/pyBarcode/writers/index.html

【问题讨论】:

    标签: python image barcode


    【解决方案1】:

    生成任意大小的图像后,您可以使用PIL调整其大小

    import barcode
    from barcode.writer import ImageWriter
    import PIL
    from PIL import Image
    
    bar_class = barcode.get_barcode_class('code128')
    barcode = '1234567890'
    
    writer=ImageWriter()
    code128 = bar_class(barcode, writer)
    
    code128.save('filename') # save the originally generated image
    
    to_be_resized = Image.open('filename.png') # open in a PIL Image object
    
    newSize = (500, 300) # new size will be 500 by 300 pixels, for example
    resized = to_be_resized.resize(newSize, resample=PIL.Image.NEAREST) # you can choose other :resample: values to get different quality/speed results
    
    resized.save('filename_resized.png') # save the resized image
    

    更多关于PIL.Image.resize

    【讨论】:

    • 此解决方案有效,但是当我使用 pyFPDF 将图像添加到 pdf 时,服务器上的 pdf 预览看起来不错但是当我下载 PDF 时,由于某种原因质量下降并且不能由条形码阅读器读取
    • 可能你在服务器端发生了一些压缩,尽量不要使用pdf,尝试只上传一张图片到服务器并从服务器下载,检查质量是否下降
    • 调整条形码大小时必须小心。像您一样使用 NEAREST 可以更改不同条形的相对宽度,可能达到不可读的程度。任何其他选项都会在条形边缘引入一些模糊。
    【解决方案2】:

    我通过改变这一行解决了这个问题

    code128.save('filename', {"module_width":0.35, "module_height":10, "font_size": 18, "text_distance": 1, "quiet_zone": 3})
    

    不使用其他库。

    【讨论】:

      【解决方案3】:

      看到了! 我完全像这样解决了我的问题,保存时,我这样做了::

      重要提示:没有其他库!

      ean = barcode.get('ean13', setbarcodehere, writer=ImageWitoutTextWriter())
      filename = ean.save(setfilenamehere, {"module_width":0.35, "module_height":10, "font_size": 18, "text_distance": -3, "quiet_zone": 1})
      filename
      print('Done! :D, {}'.format(filename))
      

      示例:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-25
        相关资源
        最近更新 更多