首页 > 解决方案 > Python条形码:无法打开资源

问题描述

有谁知道如何解决这个问题?我正在尝试使用模块python 条形码制作应用程序, 当我将它编译为原始时它运行良好,但是当我试图将它变成一个 exe(使用 autopytoexe)时,这给了我错误

Traceback (most recent call last):
  File "sad.py", line 14, in <module>
  File "site-packages\barcode\base.py", line 68, in save
  File "site-packages\barcode\ean.py", line 122, in render
  File "site-packages\barcode\base.py", line 111, in render
  File "site-packages\barcode\writer.py", line 226, in render
  File "site-packages\barcode\writer.py", line 356, in _paint_text
  File "site-packages\PIL\ImageFont.py", line 648, in truetype
  File "site-packages\PIL\ImageFont.py", line 645, in freetype
  File "site-packages\PIL\ImageFont.py", line 193, in __init__
OSError: cannot open resource

这是我目前正在运行的代码

import barcode
import os.path
import os
from barcode.writer import ImageWriter
from PIL import Image, ImageDraw, ImageFont

number = "0000000000000"
prodname = "Chicken Biryani"
directory = './Barcodes/'
a = barcode.get_barcode_class('ean13')
img_bar = a(number, writer = ImageWriter())
if not os.path.isdir(directory):
    os.mkdir(directory)
img_bar.save("Barcodes\\"+prodname)
img = Image.open("Barcodes\\"+prodname+'.png')
fnt = ImageFont.truetype("arial.ttf",50)
d = ImageDraw.Draw(img)
d.text((80,190), prodname, font=fnt, fill=(0,0,0))
img.save("Barcodes\\"+prodname+'.png')

标签: pythonbarcodepy2exe

解决方案


这个库正在使用DejaVuSansMono字体。您必须将该字体粘贴到包含该.exe文件的文件夹中。


推荐阅读