diff --git a/i18n.ini b/i18n.ini index dd6de2f..0fde272 100644 --- a/i18n.ini +++ b/i18n.ini @@ -4,9 +4,11 @@ Output = 输出 OpenFileDialog = 浏览 UsedModel = 模型 ResizeMode = 放大尺寸计算方式 -ResizeModeRatio = 固定倍率 -ResizeModeWidth = 等比放大到宽度 -ResizeModeHeight = 等比放大到高度 +ResizeModeRatio = 倍率 +ResizeModeWidth = 宽度 +ResizeModeHeight = 高度 +ResizeModeLongestSide = 较长边 +ResizeModeShortestSide = 较短边 StartProcessing = 开始 PauseProcessing = 暂停 ContinueProcessing = 继续 @@ -49,9 +51,11 @@ Output = 輸出 OpenFileDialog = 瀏覽 UsedModel = 模型 ResizeMode = 放大尺寸計算方式 -ResizeModeRatio = 固定倍率 -ResizeModeWidth = 等比放大到寬度 -ResizeModeHeight = 等比放大到高度 +ResizeModeRatio = 倍率 +ResizeModeWidth = 寬度 +ResizeModeHeight = 高度 +ResizeModeLongestSide = 較長邊 +ResizeModeShortestSide = 較短邊 StartProcessing = 開始 PauseProcessing = 暫停 ContinueProcessing = 繼續 @@ -94,9 +98,11 @@ Output = 輸出 OpenFileDialog = 瀏覽 UsedModel = 模型 ResizeMode = 放大尺寸計算方式 -ResizeModeRatio = 固定倍率 -ResizeModeWidth = 等比放大到寬度 -ResizeModeHeight = 等比放大到高度 +ResizeModeRatio = 倍率 +ResizeModeWidth = 寬度 +ResizeModeHeight = 高度 +ResizeModeLongestSide = 較長邊 +ResizeModeShortestSide = 較短邊 StartProcessing = 開始 PauseProcessing = 暫停 ContinueProcessing = 繼續 @@ -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 diff --git a/main.py b/main.py index b583805..287380d 100644 --- a/main.py +++ b/main.py @@ -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')) @@ -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')) @@ -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) @@ -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')) @@ -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(), @@ -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()], @@ -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 @@ -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, @@ -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, diff --git a/param.py b/param.py index 774eaac..1a22e80 100644 --- a/param.py +++ b/param.py @@ -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 diff --git a/task.py b/task.py index 4e5ee4d..fd3bab4 100644 --- a/task.py +++ b/task.py @@ -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 @@ -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: