我的代码中有一个非常奇怪的错误。我正在编写一个 python 程序,它生成条形码并将它们组合成图像。当我执行 .exe 文件时,出现错误。然而,这很奇怪,因为当我从 VS Code 运行它而没有任何问题时,代码可以正常工作。但是当我将它转换为 .exe 文件时,它给了我一个错误。
这是代码:
def generate_and_save_image(self, filename, dpi=600):
# Create new image
image = Image.new("RGB", (1024, 768), "white")
draw = ImageDraw.Draw(image)
# Font
font_path = "C:/Users/RaulV/Desktop/sbar/ARIAL.ttf"
large_font = ImageFont.load_default()
print("Font Path:", font_path)
print("Large Font Loaded:", large_font)
# Generate and add the origin barcode
barcode_origin = Code128(self.current_origin, writer=ImageWriter())
barcode_origin.save("barcode_origin")
barcode_origin_img = Image.open("barcode_origin.png")
barcode_origin_img = barcode_origin_img.resize((barcode_origin_img.width, barcode_origin_img.height))
image.paste(barcode_origin_img, (10, 40))
draw.text((20, 10), "ORIGEN", fill="black", font=large_font)
# Generate and add the destination barcode
barcode_destination = Code128(self.current_destination, writer=ImageWriter())
barcode_destination.save("barcode_destination")
barcode_destination_img = Image.open("barcode_destination.png")
barcode_destination_img = barcode_destination_img.resize((barcode_destination_img.width, barcode_destination_img.height))
image.paste(barcode_destination_img, (image.width - barcode_destination_img.width - 10, 40))
draw.text((image.width - barcode_destination_img.width - 10, 10), "DESTINO", fill="black", font=large_font)
# Generate and add the scanned barcode
barcode_scanned = Code128(self.codigo_barras, writer=ImageWriter())
barcode_scanned.save(f"barcode")
barcode_scanned_img = Image.open(f"barcode.png")
# Calculate the coordinates to place the barcode in the center of the image
x = (image.width - barcode_scanned_img.width) // 2
y = (image.height - barcode_scanned_img.height) // 2 + 50
image.paste(barcode_scanned_img, (x, y))
# Generate and add the quantity barcode in the bottom right corner
barcode_quantity = Code128(str(self.cantidad), writer=ImageWriter())
barcode_quantity.save("barcode_quantity")
barcode_quantity_img = Image.open("barcode_quantity.png")
barcode_quantity_img = barcode_quantity_img.resize((barcode_quantity_img.width, barcode_quantity_img.height))
image.paste(barcode_quantity_img, (image.width - barcode_quantity_img.width - 10, image.height - barcode_quantity_img.height - 10))
# Text with the quantity
draw.text((image.width - barcode_quantity_img.width - 10, image.height - barcode_quantity_img.height - 30), "CANTIDAD", fill="black", font=large_font)
query = ""
self.cursor.execute(query, (self.codigo_barras))
result = self.cursor.fetchone()
if result:
article_code, description = result
draw.text((10, image.height-90), f"REF: {article_code}", fill="black", font=large_font)
draw.text((10, image.height-30), f"{description}", fill="black", font=large_font)
query = ""
self.cursor.execute(query, (self.current_origin))
result = self.cursor.fetchone()
if result:
cantidadAnterior = result[0]
draw.text((10, image.height-60), f"Unidades en la ubicacion de origen: {cantidadAnterior}", fill="black", font=large_font)
# Save image
image.save(filename, dpi=(dpi, dpi))
这是错误:
Font Path: C:/Users/RaulV/Desktop/sbar/ARIAL.ttf
Large Font Loaded: <PIL.ImageFont.FreeTypeFont object at 0x0000022DA8EB6ED0>
Exception in Tkinter callback
Traceback (most recent call last):
File "tkinter\__init__.py", line 1967, in __call__
File "combinedApp.py", line 759, in close_selected_boxes
File "combinedApp.py", line 842, in generate_and_save_image
File "barcode\base.py", line 64, in save
File "barcode\codex.py", line 255, in render
File "barcode\base.py", line 100, in render
File "barcode\writer.py", line 269, in render
File "barcode\writer.py", line 440, in _paint_text
File "PIL\ImageFont.py", line 807, in truetype
File "PIL\ImageFont.py", line 804, in freetype
File "PIL\ImageFont.py", line 244, in __init__
OSError: cannot open resource
759行:
self.generate_and_save_image(filename)
842行:
barcode_origin.save("barcode_origin")
我也尝试过使用默认字体:
large_font = ImageFont.load_default()
但我得到了同样的错误。
当我将 .py 转换为 .exe 时,我使用 pyinstaller,尝试在 exe 中添加字体,但出现了相同的错误。 这是转换命令:
pyinstaller --noconfirm --onefile --console --add-data "C:/Users/RaulV/Desktop/Barcodes/arial.ttf;." "C:/Users/RaulV/Desktop/Barcodes/combinedApp.py"
现在我尝试使用简单的代码,但出现了相同的错误:
def generate_barcode(code, filename):
code128 = barcode.get_barcode_class('code128')
barcode_instance = code128(code, writer=ImageWriter())
barcode_instance.save(filename)
generate_barcode('123456789', 'barcode.png')
我找到了一个解决方案,我必须在 Code128 初始化之前创建一个 ImageWriter 对象,如下所示:
font_path = 'your_path'
writer = ImageWriter()
writer.font_path = font_path
writer.format = 'png'
barcode_origin = Code128(self.current_origin, writer=writer)