IO Error:Cannot open image while generating barcode after freezing using py2exe

断了今生、忘了曾经 提交于 2021-02-04 20:47:25

问题


I have used pyBarcode library to generate Barcode in my software and its works perfectly fine while I load it from command line but once I freeze the entire software using py2exe I am getting IO error while generating the barcode.

File "panels.pyc", line 383, in generate
File "barcodeGenerator.pyc", line 9, in generate
File "barcode\base.pyc", line 68, in save
File "barcode\codex.pyc", line 251, in render
File "barcode\base.pyc", line 103, in render
File "barcode\writer.pyc", line 188, in render
File "barcode\writer.pyc", line 280, in _paint_text
File "PIL\ImageFont.pyc", line 248, in truetype
File "PIL\ImageFont.pyc", line 146, in __init__
IOError: cannot open resource

Here panels.py is my python script from where I call generate method of barcodeGenerator.py whose code is given below.

barcodeGenerator.py :-

import barcode
from barcode import generate
from barcode.writer import ImageWriter
from PIL import PngImagePlugin

def generate(details,path):
  EAN = barcode.get_barcode_class('code128')
  ean = EAN(details, writer=ImageWriter())
  barcodePic = ean.save(path + 'barcode')

And yes the setup.py file using which is freeze is:-

from distutils.core import setup
import py2exe

includes = ["HuffmanDictionary"]

setup(
options = {
            "py2exe": {"includes": includes}
          },
console=['MainFrame.py',"extraModules.py","encode.py","decode.py","panels.py","barcodeGenerator.py" ]
)

Please could some one point the error I made.I am very close to completing the entire software this is the last bug and I am using windows 7 64 bit.

EDIT: Already have been to this link and tried but still not working for me.


回答1:


I found this in [your python installation directory]\Lib\site-packages\barcode\writer.py

PATH = os.path.dirname(os.path.abspath(__file__))
FONT = os.path.join(PATH, 'DejaVuSansMono.ttf')

DejaVuSansMono.ttf is also in the same directory. But, after cx_freeze built, barcode writer give an invalid font path to ImageFont.

My workaround is to copy DejaVuSansMono.ttf to Windows\Fonts OS folder on the PC that will run your built executable.

Similar issue at How I can load a font file with PIL.ImageFont.truetype without specifying the absolute path?

Another workaround is to modify barcode\writer.py

#FONT = os.path.join(PATH, 'DejaVuSansMono.ttf')
FONT = 'arial.ttf'

I think arial.ttf should be in every Windows\Fonts (default font path) directory.




回答2:


Finally after digging deep into google and asking question on the mailing lists of py2exe I realized that there was some error in py2exe it just didn't include the some modules without any reason. So I added the entire module in the folder of .exe as include_package and that worked as a charm. Also cx_Freeze is a good alternative to using py2exe since it is easy to use and is supported on multiple platform.

Hope this helps other who are wasting time searching on internet.



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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!