Skip to content

Commit

Permalink
修复bug:系统启用UTF-8支持时,无法读取非英文路径
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroi-sora committed Sep 6, 2022
1 parent 1f68533 commit c4b5e71
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
12 changes: 10 additions & 2 deletions callingOCR.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os
import subprocess # 进程,管道
from sys import platform as sysPlatform # popen静默模式
from json import loads as jsonLoads
from json import loads as jsonLoads, dumps as jsonDumps


class CallingOCR:
Expand All @@ -27,6 +27,8 @@ def __init__(self, exePath, configPath="", argsStr=""):
exePath += f' --config_path="{configPath}"'
if 'use_system_pause' not in exePath: # 强制禁用暂停
exePath += ' --use_system_pause=0'
if 'ensure_ascii' not in exePath: # 启用输出值ascii转义,规避编码问题
exePath += ' --ensure_ascii=1'
# 设置子进程启用静默模式,不显示控制台窗口
startupinfo = None
if 'win32' in str(sysPlatform).lower():
Expand Down Expand Up @@ -54,7 +56,13 @@ def run(self, imgPath):
:return: {'code': 识别码, 'data': 内容列表或错误信息字符串}\n"""
if not self.ret.poll() == None:
return {'code': 400, 'data': f'子进程已结束。'}
wirteStr = imgPath if imgPath[-1] == '\n' else imgPath + '\n'
# wirteStr = imgPath if imgPath[-1] == '\n' else imgPath + '\n'
writeDict = {'image_dir': imgPath}
try: # 输入地址转为ascii转义的json字符串,规避编码问题
wirteStr = jsonDumps(
writeDict, ensure_ascii=True, indent=None)+"\n"
except Exception as e:
return {'code': 403, 'data': f'输入字典转json失败。字典:{writeDict} || 报错:[{e}]'}
# 输入路径
try:
self.ret.stdin.write(wirteStr.encode('gbk'))
Expand Down
16 changes: 16 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,27 @@


class ConfigModule:
sysEncoding = 'gbk' # 系统编码。初始化时获取

def initValue(self, optVar):
"""初始化配置。传入并设置tk变量字典"""
self.optVar = optVar

def setSysEncoding(): # 获取系统编码
"""初始化编码"""
from locale import getdefaultlocale
loc = getdefaultlocale()[1]
if loc == 'cp936':
self.sysEncoding = 'gbk'
elif loc == 'cp65001':
self.sysEncoding = 'utf-8'
print('已启动文件系统utf-8模式(Beta)')
else:
tk.messagebox.showwarning(
"警告",
f"当前系统区域为 {loc} ,不属于cp936(中文简体)或cp65001(启用全球语言支持),可能导致Umi-OCR读取图片路径错误。")
setSysEncoding() # 初始化编码

def load():
"""从本地json文件读取配置"""
try:
Expand Down
5 changes: 3 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from pyperclip import copy as pyperclipCopy # 剪贴板
from webbrowser import open as webOpen # “关于”面板打开项目网址

ProjectVer = "1.2.6" # 版本号
ProjectVer = "1.2.7 Alpha 1" # 版本号
ProjectName = f"Umi-OCR 批量图片转文字 v{ProjectVer}" # 名称
ProjectWeb = "https://github.com/hiroi-sora/Umi-OCR"
TempFilePath = "Umi-OCR_temp"
Expand Down Expand Up @@ -494,7 +494,8 @@ def draggedImages(self, paths): # 拖入图片
self.notebook.select(self.tabFrameTable) # 切换到表格选项卡
pathList = []
for p in paths: # byte转字符串
pathList.append(p.decode("gbk"))
pathList.append(p.decode(Config.sysEncoding, # 根据系统编码来解码
errors='ignore'))
self.addImagesList(pathList)

def openFileWin(self): # 打开选择文件窗
Expand Down
3 changes: 2 additions & 1 deletion selectAreaWin.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ def getData(): # 将数据传给接口,然后关闭窗口
self.win.destroy() # 销毁窗口

def draggedFiles(self, paths): # 拖入文件
self.loadImage(paths[0].decode("gbk"))
self.loadImage(paths[0].decode( # 根据系统编码来解码
Config.sysEncoding, errors='ignore'))

def loadImage(self, path): # 载入新图片
"""载入图片"""
Expand Down

0 comments on commit c4b5e71

Please sign in to comment.