如何修复由于在使用pyins转换到.exe期间从URL读取图像而发生的“JSONDecodeError”

2024-09-30 18:35:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python中使用Tkinter从URL读取图像,并使用Tesseract OCR提取文本,并将其显示在Tkinter画布上。项目几乎完成了,但是当我尝试将python文件转换为.exe文件时,会弹出JSONDecodeError。在我使用IDLE运行之前,代码运行得非常好。你知道吗

我尝试使用请求来获取URL并读取图像,但我无法做到这一点。所以我用urllib.request.urlopen(url)“”。但是在使用pyinstaller转换到.exe的过程中,会弹出“JSONDecodeError”并停止转换。Pyinstaller用于将.py文件转换为.exe文件,然后再放入模块(从URL读取图像的模块)。 我该怎么解决这个问题?你知道吗

下面是代码的一小部分:

import urllib.request

if url=="":
    tk.messagebox.showerror("ERROR","Enter a URL!!!")
img=urllib.request.urlopen(url)
self.tesseract(Image.open(img),root)

这就是我犯的错误

  File "c:\users\aayush.gour\appdata\local\programs\python\python36\lib\site-pac
kages\PyInstaller\hooks\hook-PyQt5.py", line 23, in <module>
    collect_system_data_files(pyqt5_library_info.location['PrefixPath'],
  File "c:\users\aayush.gour\appdata\local\programs\python\python36\lib\site-pac
kages\PyInstaller\utils\hooks\qt.py", line 67, in __getattr__
    qli = json.loads(json_str)
  File "c:\users\aayush.gour\appdata\local\programs\python\python36\lib\json\__i
nit__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "c:\users\aayush.gour\appdata\local\programs\python\python36\lib\json\dec
oder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "c:\users\aayush.gour\appdata\local\programs\python\python36\lib\json\dec
oder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Tags: 文件inpyjsonurlliblocalline
1条回答
网友
1楼 · 发布于 2024-09-30 18:35:24

原来我找到了自己问题的答案。 这个问题的答案分为两部分。你知道吗

第1部分

我没有使用urllib.request,而是使用了skimage库中的io。这就容易多了。下面是代码(我使用python3.6.8)

from skimage import io
url="https://SomeUrlForAnImage.com"
img=io.imread(url)
self.tesseract(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), root)

我使用cv2.cvtColor(img, cv2.COLOR_BGR2RGB)函数将图像从BGR转换为RGB颜色编码。你知道吗

第2部分-更新Pyinstaller

如果使用pip install pyinstaller安装pyinstaller,则使用pip uninstall pyinstaller卸载它,并从here安装最新版本。下载zip文件并使用WinRar或任何其他软件(如7z)将其解压缩。打开解压缩文件夹中的命令窗口并键入python setup.py install。这将在您的设备上安装最新版本的pyinstaller。大多数错误可以通过安装pyinstaller的最新版本来消除。你知道吗

相关问题 更多 >