Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot save as png after packaging into exe file #69

Closed
Yinjiafeng opened this issue Apr 26, 2020 · 10 comments
Closed

Cannot save as png after packaging into exe file #69

Yinjiafeng opened this issue Apr 26, 2020 · 10 comments

Comments

@Yinjiafeng
Copy link

Yinjiafeng commented Apr 26, 2020

微信截图_20200426105205

Traceback (most recent call last):
  File "site-packages\PIL\ImageFont.py", line 186, in __init__
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 9: ordinal not in range(128)

This error occurs under python3.6, how to solve it

@WhyNotHugo
Copy link
Owner

WhyNotHugo commented Apr 29, 2020

What do you mean by "packing into exe file"?
This error gives me the impression that you haven't installed the library, what's the output of pip freeze?

@WhyNotHugo WhyNotHugo added the more-information-needed waiting for a reply from the reporter label May 3, 2020
@ghost
Copy link

ghost commented May 25, 2020

im on this problem too.
im making barcode generator with your package, Tkinter, and wrap it using auto-py-to-exe
work fine until i make it into .exe files

import tkinter as tk
from tkinter import *
import barcode
from barcode.writer import ImageWriter

utama=tk.Tk()
utama.title("Generate barcode 128")
utama.geometry("350x250")

def clickOk():
    x=kode.get()
    bar1=barcode.get_barcode_class('code128')
    bar2=bar1(x,writer=ImageWriter())
    bar3=bar2.save('barcode')
    
kode=tk.Entry(utama)
kode.grid(row=0,column=0)
tombol=tk.Button(utama,text="OK",command=clickOk)
tombol.grid(row=1,column=0)

utama.mainloop()

@WhyNotHugo
Copy link
Owner

If the code works for regular Python, then there's nothing regarding this library at fault.

It looks like the issue is with the tool you're using to generate exe files, so you need to report the issue with them (it might also be that you're misusing it, I honestly have no idea). I've never developed on windows, so I've no idea about the extra complexities around it.

I don't have access to any Windows machines, so I can't really try it (nor do I have the time to research these tools, TBH).

@WhyNotHugo WhyNotHugo removed the more-information-needed waiting for a reply from the reporter label May 25, 2020
@Phaugt
Copy link

Phaugt commented Dec 16, 2020

found this: https://stackoverflow.com/questions/17620179/io-errorcannot-open-image-while-generating-barcode-after-freezing-using-py2exe

so apparently when changing to another font than the provided it works on windows.

so when i changed to: self.font_path = os.path.join(PATH, "fonts", "calibri.ttf") it works, so maybe the error is because we dont have that font as stanard on windows.

so when packaging to .exe alter the writer.py file to a font that is usable by the OS.
[your python installation directory]\Lib\site-packages\barcode\writer.py

@WhyNotHugo
Copy link
Owner

Can you extract you exe container and check if the font is in there?
It's possible that the tool that creates the exe container isn't including the font inside of it. If that's the case, you should report that to them.

@Phaugt
Copy link

Phaugt commented Dec 16, 2020

I checked in the dist folder (which PyInstaller creates) but I couldn't find any traces of the barcode-code but I guess it is a problem within PyInstaller. But for those who have the issue either have to include the .ttf file in their code or just swap to something that is recognized by PyInstaller (like in my case when I swapped to calibri since I use that in the gui).

Hopefully it could help anyone that tries to make an .exe with your module.

@WhyNotHugo
Copy link
Owner

Keep in mind that patching writer.py with a different path is very brittle. Any update will undo those changes, and it'll be hard to track as time goes by.

I haven't used PyInstaller, so I can't provide much specific advice for it (I just use pip or the OS's package manager). I do suggest you report the issue to them though, it sounds like there's an issue packaging files when building these bundles.

The generated package / bundle should contain, at least, these files:

barcode/fonts/DejaVuSansMono.ttf
barcode/writer.py
barcode/codex.py
barcode/itf.py
barcode/charsets/itf.py
barcode/charsets/code128.py
barcode/charsets/upc.py
barcode/charsets/code39.py
barcode/charsets/ean.py
barcode/charsets/__init__.py
barcode/errors.py
barcode/upc.py
barcode/isxn.py
barcode/base.py
barcode/pybarcode.py
barcode/version.py
barcode/ean.py
barcode/__init__.py

@Phaugt
Copy link

Phaugt commented Dec 16, 2020

After reading some PyInstaller compiles the module (if I got that right) to a .pyc or .pyd file and somehow uses that in the .exe but that also means that the font file should be there somewhere, but then PyInstaller could have some trouble reading that I guess.

Here is a PyInstaller dist folder with only PyInstaller and barcode in the venv.

barcode

So I guess that the module is somewhere.

@WhyNotHugo
Copy link
Owner

I'd expect to see all the files form this module there too. Maybe inside Include?
I've no idea what pyd is though, I've never seen that before.

@zdposter
Copy link

I have the same error with pyinstaller and I was able to solve with adding following lines to spec file:

added_files = [
         ("C:\\Program Files\\Python38\\Lib\\site-packages\\barcode\\fonts", 'barcode/fonts'),
         ]
a = Analysis(['b_test.py'],
             pathex=['C:\\test'],
             binaries=[],
             datas=added_files,
...

Anyway I would suggest to:

  1. allow to use custom font instead of hardcode (it should be easier that multiplatform detection of available monospace font ;-))
  2. provide better error message. e.g. replace font = ImageFont.truetype(self.font_path, self.font_size * 2) with:
            try:
                font = ImageFont.truetype(self.font_path, self.font_size * 2)
            except IOError as error:
                print("Cannot open font:", self.font_path)
                raise error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants