aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--test_upscale_qt.py8
-rwxr-xr-xupscale-qt17
2 files changed, 25 insertions, 0 deletions
diff --git a/test_upscale_qt.py b/test_upscale_qt.py
index 7d7307c..70608b6 100644
--- a/test_upscale_qt.py
+++ b/test_upscale_qt.py
@@ -67,6 +67,14 @@ def test_progress():
assert uq.progress(b'[I] load model') is None
+def test_fit_scale():
+ from PyQt6.QtCore import QSize
+ big = QImage(40, 20, QImage.Format.Format_RGB32)
+ assert uq.fit_scale(big, QSize(10, 5), 2).size() == QSize(20, 10)
+ assert uq.fit_scale(big, QSize(10, 5), 4).size() == QSize(40, 20)
+ assert uq.fit_scale(big, QSize(20, 10), 4).size() == QSize(40, 20) # 2x model
+
+
def test_save_over():
path = os.path.join(TMP, 'photo.JPG') # uppercase suffix on purpose
small = QImage(4, 4, QImage.Format.Format_RGB32)
diff --git a/upscale-qt b/upscale-qt
index a343439..db178e0 100755
--- a/upscale-qt
+++ b/upscale-qt
@@ -79,6 +79,15 @@ def save_over(img, path):
return True
+def fit_scale(img, src_size, factor):
+ """Shrink img to factor x src_size; never enlarge past what the model gave."""
+ w, h = src_size.width() * factor, src_size.height() * factor
+ if img.width() <= w:
+ return img
+ return img.scaled(w, h, Qt.AspectRatioMode.IgnoreAspectRatio,
+ Qt.TransformationMode.SmoothTransformation)
+
+
def tail(data, lines=15):
text = bytes(data).decode(errors='replace').replace('\r', '\n')
return '\n'.join([l for l in text.splitlines() if l.strip()][-lines:])
@@ -234,6 +243,11 @@ class Main(QMainWindow):
self.models = QComboBox()
self.models.setSizeAdjustPolicy(QComboBox.SizeAdjustPolicy.AdjustToContents)
self.models.setToolTip('Upscaler model')
+ self.scale = QComboBox()
+ self.scale.setToolTip('Output size; the model result is shrunk to fit')
+ for f in (2, 3, 4):
+ self.scale.addItem(f'{f}x', f)
+ self.scale.setCurrentIndex(2)
tb = self.addToolBar('main')
tb.setMovable(False)
@@ -253,6 +267,7 @@ class Main(QMainWindow):
self.a_open = act('Open', 'document-open', self.open_dialog,
QKeySequence.StandardKey.Open)
tb.addWidget(self.models)
+ tb.addWidget(self.scale)
self.a_up = act('Upscale', 'zoom-in', self.upscale, 'Ctrl+U')
self.a_save = act('Save As', 'document-save-as', self.save_as,
QKeySequence.StandardKey.SaveAs)
@@ -306,6 +321,7 @@ class Main(QMainWindow):
done = job is not None and job.result is not None
self.a_open.setEnabled(not busy)
self.models.setEnabled(not busy)
+ self.scale.setEnabled(not busy)
self.a_up.setEnabled(job is not None and not busy and self.models.count() > 0)
self.a_up.setToolTip('Upscale (Ctrl+U)' if self.models.count() else
f'No models in {self.get("upscalers_dir")}, see Settings')
@@ -381,6 +397,7 @@ class Main(QMainWindow):
self.statusBar().showMessage('Upscale failed')
QMessageBox.warning(self, 'Upscale failed', tail(self.err) or f'exit code {code}')
else:
+ img = fit_scale(img, job.image.size(), self.scale.currentData())
job.result = img
self.view.set_images(job.image, img)
self.statusBar().showMessage(