尝试渲染条形码图像,但对象无法调用?

时间:2019-01-01 10:31:36

标签: python python-3.x barcode

我正在尝试建立一个代码,该代码通过从txt文件生成条形码图像并将其粘贴到票证图像上来创建票证。

import barcode
import time
from barcode.writer import ImageWriter
mylist = []
with open('/Users/Droid/Desktop/my_project/Tickets/Tnumfile.TXT', 'r') as f:
for line in f.readlines():    #Walks through each line
    code = barcode.get('code', line, writer=ImageWriter()) 
    filename = code.save(line.strip())        #Saves Line 'tnumfile' as filename
    mylist.insert(0,line.strip() + '.png')



time.sleep(2)
from PIL import Image

for item in mylist:

im1 = Image.open('/Users/Droid/Desktop/my_project/Ticket.png') 
im2 = Image.open('/Users/Droid/Desktop/my_project/Tickets' + item) 
area = (30, 1380, 553, 1660)                                       

im1.paste(im2, area)
im1.save('line' + item)

应该通过.txt文件运行,将所有条形码转换为图像,并将其复制到票证图像模板上,而一直告诉我:

  File "C:\Users\Droid\Desktop\my_project\Script.py", line 7, in <module>
    code = barcode.get('code', line, writer=ImageWriter())  
TypeError: 'NoneType' object is not callable

1 个答案:

答案 0 :(得分:4)

如果要将条形码呈现到图像,则python-barcode project要求安装枕头,否则将ImageWriter设置为None。有关详细信息,请参见barcode.writer source code(PIL是Pillow提供的软件包的名称)。

运行pip install Pillow来解决这个问题。

您还可以通过安装python-barcode images附加项来告诉pip引入依赖项:

pip install python-barcode[images]