pythonexebarcode

Pillow error with ImageFont generating a Barcode with barcode Code128 OSError: cannot open resource


I have a very strange error in my code. I'm writing a python program that generates barcodes and combines them into an image. When I execute the .exe file, it gives me an error. However, it's strange because when I run it from VS Code without any issues, the code works correctly. But when I convert it to an .exe file, it gives me an error.

Here's the code:

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

Here is the error:

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

Line 759: self.generate_and_save_image(filename) Line 842: barcode_origin.save("barcode_origin")

I have tried too with default font: large_font = ImageFont.load_default() but I get the same error.

When I convert the .py to .exe I use pyinstaller, I tried adding the font in the exe and I get the same error. This is the command to convert:

pyinstaller --noconfirm --onefile --console --add-data "C:/Users/RaulV/Desktop/Barcodes/arial.ttf;."  "C:/Users/RaulV/Desktop/Barcodes/combinedApp.py"

Now I'm trying with a simple code and I get the same error:

    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')

Solution

  • I found a solution.

    I have to create an ImageWriter object before the initialization of Code128, like this:

    font_path = 'your_path'
    writer = ImageWriter()
    writer.font_path = font_path 
    writer.format = 'png'
    barcode_origin = Code128(self.current_origin, writer=writer)