Pillow error with ImageFont 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 "barcodebase.py", line 64, in save
  File "barcodecodex.py", line 255, in render
  File "barcodebase.py", line 100, in render
  File "barcodewriter.py", line 269, in render
  File "barcodewriter.py", line 440, in _paint_text
  File "PILImageFont.py", line 807, in truetype
  File "PILImageFont.py", line 804, in freetype
  File "PILImageFont.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"

New contributor

raúl vaquerizo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật