Skip to content

Commit

Permalink
支持根据长短边设定缩放大小
Browse files Browse the repository at this point in the history
Close #81
  • Loading branch information
TransparentLC committed May 21, 2024
1 parent 5f8708d commit 26ce0e1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 14 deletions.
30 changes: 19 additions & 11 deletions i18n.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ Output = 输出
OpenFileDialog = 浏览
UsedModel = 模型
ResizeMode = 放大尺寸计算方式
ResizeModeRatio = 固定倍率
ResizeModeWidth = 等比放大到宽度
ResizeModeHeight = 等比放大到高度
ResizeModeRatio = 倍率
ResizeModeWidth = 宽度
ResizeModeHeight = 高度
ResizeModeLongestSide = 较长边
ResizeModeShortestSide = 较短边
StartProcessing = 开始
PauseProcessing = 暂停
ContinueProcessing = 继续
Expand Down Expand Up @@ -49,9 +51,11 @@ Output = 輸出
OpenFileDialog = 瀏覽
UsedModel = 模型
ResizeMode = 放大尺寸計算方式
ResizeModeRatio = 固定倍率
ResizeModeWidth = 等比放大到寬度
ResizeModeHeight = 等比放大到高度
ResizeModeRatio = 倍率
ResizeModeWidth = 寬度
ResizeModeHeight = 高度
ResizeModeLongestSide = 較長邊
ResizeModeShortestSide = 較短邊
StartProcessing = 開始
PauseProcessing = 暫停
ContinueProcessing = 繼續
Expand Down Expand Up @@ -94,9 +98,11 @@ Output = 輸出
OpenFileDialog = 瀏覽
UsedModel = 模型
ResizeMode = 放大尺寸計算方式
ResizeModeRatio = 固定倍率
ResizeModeWidth = 等比放大到寬度
ResizeModeHeight = 等比放大到高度
ResizeModeRatio = 倍率
ResizeModeWidth = 寬度
ResizeModeHeight = 高度
ResizeModeLongestSide = 較長邊
ResizeModeShortestSide = 較短邊
StartProcessing = 開始
PauseProcessing = 暫停
ContinueProcessing = 繼續
Expand Down Expand Up @@ -140,8 +146,10 @@ OpenFileDialog = Browse
UsedModel = Model
ResizeMode = Resize mode
ResizeModeRatio = Ratio
ResizeModeWidth = Scale to width
ResizeModeHeight = Scale to height
ResizeModeWidth = Width
ResizeModeHeight = Height
ResizeModeLongestSide = Longest side
ResizeModeShortestSide = Shortest side
StartProcessing = Start
PauseProcessing = Pause
ContinueProcessing = Continue
Expand Down
30 changes: 29 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ def outputPathTraceCallback(var: tk.IntVar | tk.StringVar, index: str, mode: str
self.varintResizeWidth.trace_add('write', outputPathTraceCallback)
self.varintResizeHeight = tk.IntVar(value=self.config['Config'].getint('ResizeHeight'))
self.varintResizeHeight.trace_add('write', outputPathTraceCallback)
self.varintResizeLongestSide = tk.IntVar(value=self.config['Config'].getint('ResizeLongestSide'))
self.varintResizeLongestSide.trace_add('write', outputPathTraceCallback)
self.varintResizeShortestSide = tk.IntVar(value=self.config['Config'].getint('ResizeShortestSide'))
self.varintResizeShortestSide.trace_add('write', outputPathTraceCallback)
self.varstrModel = tk.StringVar(value=self.config['Config'].get('Model'))
self.varstrModel.trace_add('write', outputPathTraceCallback)
self.varintDownsampleIndex = tk.IntVar(value=self.config['Config'].getint('DownsampleIndex'))
Expand All @@ -172,6 +176,8 @@ def outputPathTraceCallback(var: tk.IntVar | tk.StringVar, index: str, mode: str
self.varstrLabelResizeModeRatio = tk.StringVar(value=i18n.getTranslatedString('ResizeModeRatio'))
self.varstrLabelResizeModeWidth = tk.StringVar(value=i18n.getTranslatedString('ResizeModeWidth'))
self.varstrLabelResizeModeHeight = tk.StringVar(value=i18n.getTranslatedString('ResizeModeHeight'))
self.varstrLabelResizeModeLongestSide = tk.StringVar(value=i18n.getTranslatedString('ResizeModeLongestSide'))
self.varstrLabelResizeModeShortestSide = tk.StringVar(value=i18n.getTranslatedString('ResizeModeShortestSide'))
self.varstrLabelStartProcessing = tk.StringVar(value=i18n.getTranslatedString(('ContinueProcessing' if self.varboolProcessingPaused.get() else 'PauseProcessing') if self.varboolProcessing.get() else 'StartProcessing'))
self.varstrLabelDownsampleMode = tk.StringVar(value=i18n.getTranslatedString('DownsampleMode'))
self.varstrLabelTileSize = tk.StringVar(value=i18n.getTranslatedString('TileSize'))
Expand Down Expand Up @@ -248,6 +254,14 @@ def setupWidgets(self):
self.radioResizeHeight.grid(row=3, column=0, padx=5, pady=5, sticky=tk.EW)
self.spinResizeHeight = ttk.Spinbox(self.frameResize, from_=1, to=16383, increment=1, width=12, textvariable=self.varintResizeHeight)
self.spinResizeHeight.grid(row=3, column=1, padx=5, pady=5, sticky=tk.EW)
self.radioResizeLongestSide = ttk.Radiobutton(self.frameResize, textvariable=self.varstrLabelResizeModeLongestSide, value=int(param.ResizeMode.LONGEST_SIDE), variable=self.varintResizeMode)
self.radioResizeLongestSide.grid(row=4, column=0, padx=5, pady=5, sticky=tk.EW)
self.spinResizeLongestSide = ttk.Spinbox(self.frameResize, from_=1, to=16383, increment=1, width=12, textvariable=self.varintResizeLongestSide)
self.spinResizeLongestSide.grid(row=4, column=1, padx=5, pady=5, sticky=tk.EW)
self.radioResizeShortestSide = ttk.Radiobutton(self.frameResize, textvariable=self.varstrLabelResizeModeShortestSide, value=int(param.ResizeMode.SHORTEST_SIDE), variable=self.varintResizeMode)
self.radioResizeShortestSide.grid(row=5, column=0, padx=5, pady=5, sticky=tk.EW)
self.spinResizeShortestSide = ttk.Spinbox(self.frameResize, from_=1, to=16383, increment=1, width=12, textvariable=self.varintResizeShortestSide)
self.spinResizeShortestSide.grid(row=5, column=1, padx=5, pady=5, sticky=tk.EW)
self.buttonProcess = ttk.Button(self.frameBasicConfigBottom, textvariable=self.varstrLabelStartProcessing, style='Accent.TButton', width=6, command=self.buttonProcess_click)
self.buttonProcess.grid(row=0, column=1, padx=5, pady=5, sticky=tk.SE)

Expand Down Expand Up @@ -350,6 +364,8 @@ def change_app_lang(self, event: tk.Event):
self.varstrLabelResizeModeRatio.set(i18n.getTranslatedString('ResizeModeRatio'))
self.varstrLabelResizeModeWidth.set(i18n.getTranslatedString('ResizeModeWidth'))
self.varstrLabelResizeModeHeight.set(i18n.getTranslatedString('ResizeModeHeight'))
self.varstrLabelResizeModeLongestSide.set(i18n.getTranslatedString('ResizeModeLongestSide'))
self.varstrLabelResizeModeShortestSide.set(i18n.getTranslatedString('ResizeModeShortestSide'))
self.varstrLabelStartProcessing.set(i18n.getTranslatedString(('ContinueProcessing' if self.varboolProcessingPaused.get() else 'PauseProcessing') if self.varboolProcessing.get() else 'StartProcessing'))
self.varstrLabelDownsampleMode.set(i18n.getTranslatedString('DownsampleMode'))

Expand Down Expand Up @@ -382,6 +398,8 @@ def close(self):
'ResizeRatio': self.varintResizeRatio.get(),
'ResizeWidth': self.varintResizeWidth.get(),
'ResizeHeight': self.varintResizeHeight.get(),
'ResizeLongestSide': self.varintResizeLongestSide.get(),
'ResizeShortestSide': self.varintResizeShortestSide.get(),
'Model': self.varstrModel.get(),
'DownsampleIndex': self.varintDownsampleIndex.get(),
'GPUID': self.varintGPUID.get(),
Expand Down Expand Up @@ -619,6 +637,10 @@ def getConfigParams(self) -> param.REConfigParams:
resizeModeValue = self.varintResizeWidth.get()
case param.ResizeMode.HEIGHT:
resizeModeValue = self.varintResizeHeight.get()
case param.ResizeMode.LONGEST_SIDE:
resizeModeValue = self.varintResizeLongestSide.get()
case param.ResizeMode.SHORTEST_SIDE:
resizeModeValue = self.varintResizeShortestSide.get()
return param.REConfigParams(
self.varstrModel.get(),
self.modelFactors[self.varstrModel.get()],
Expand Down Expand Up @@ -650,6 +672,10 @@ def getOutputPath(self, p: str) -> str:
suffix = f'w{self.varintResizeWidth.get()}'
case param.ResizeMode.HEIGHT:
suffix = f'h{self.varintResizeHeight.get()}'
case param.ResizeMode.LONGEST_SIDE:
suffix = f'l{self.varintResizeLongestSide.get()}'
case param.ResizeMode.SHORTEST_SIDE:
suffix = f's{self.varintResizeShortestSide.get()}'
return f'{base} ({self.models[self.comboModel.current()]} {suffix}){ext}'

# Config and model paths are initialized before main frame
Expand All @@ -664,6 +690,8 @@ def init_config_and_model_paths() -> tuple[configparser.ConfigParser, list[str]]
'ResizeRatio': 4,
'ResizeWidth': 1024,
'ResizeHeight': 1024,
'ResizeLongestSide': 1024,
'ResizeShortestSide': 1024,
'Model': '',
'DownsampleIndex': 0,
'GPUID': -1,
Expand Down Expand Up @@ -778,7 +806,7 @@ def changeTheme(theme: typing.Literal['Dark', 'Light']):
root.destroy(),
))

initialSize = (720, 540)
initialSize = (720, 640)
root.minsize(*initialSize)
root.geometry('{}x{}+{}+{}'.format(
*initialSize,
Expand Down
2 changes: 2 additions & 0 deletions param.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class ResizeMode(enum.IntEnum):
RATIO = enum.auto()
WIDTH = enum.auto()
HEIGHT = enum.auto()
LONGEST_SIDE = enum.auto()
SHORTEST_SIDE = enum.auto()

class REConfigParams(typing.NamedTuple):
model: str
Expand Down
15 changes: 13 additions & 2 deletions task.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,18 @@ def run(self) -> None:
self.inputPath = tempfile.mktemp('.png')
img.convert('RGBA').save(self.inputPath)
self.removeInput = True
match self.config.resizeMode:
resizeMode = self.config.resizeMode
if (
(resizeMode == param.ResizeMode.LONGEST_SIDE and srcWidth >= srcHeight)
or (resizeMode == param.ResizeMode.SHORTEST_SIDE and srcWidth <= srcHeight)
):
resizeMode = param.ResizeMode.WIDTH
elif (
(resizeMode == param.ResizeMode.LONGEST_SIDE and srcHeight >= srcWidth)
or (resizeMode == param.ResizeMode.SHORTEST_SIDE and srcHeight <= srcWidth)
):
resizeMode = param.ResizeMode.HEIGHT
match resizeMode:
case param.ResizeMode.RATIO:
dstWidth = srcWidth * self.config.resizeModeValue
dstHeight = srcHeight * self.config.resizeModeValue
Expand All @@ -64,7 +75,7 @@ def run(self) -> None:
dstWidth = round(dstHeight * srcRatio)
inputPathPreupscaled: str = None
if self.config.preupscale:
match self.config.resizeMode:
match resizeMode:
case param.ResizeMode.RATIO:
scaleRatio = self.config.resizeModeValue
case param.ResizeMode.WIDTH:
Expand Down

0 comments on commit 26ce0e1

Please sign in to comment.