From 08a3f4f075a7e4550b303bd422246e380fb49b5a Mon Sep 17 00:00:00 2001 From: Alex Xu Date: Sat, 24 Apr 2021 21:53:48 -0700 Subject: [PATCH] Add new options --- .gitignore | 155 +++++++++++++++++++++++++++++++++++++++++-- mangle/image.py | 39 +++++++++-- mangle/options.py | 12 +++- mangle/ui/options.ui | 18 ++++- 4 files changed, 208 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 64de61f..50970d1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,153 @@ -build -dist -*.pyc -*.pyo +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# generated manga *.mngl +[0-9][0-9][0-9][0-9][0-9].png +*.manga +*.manga_save *.cbz +*.pdf + +# vscode +.vscode/ + *.bat .idea/ @@ -33,6 +177,3 @@ Icon Network Trash Folder Temporary Items .apdisk - -venv -mangle.spec diff --git a/mangle/image.py b/mangle/image.py index 2fe18b4..fac3f16 100644 --- a/mangle/image.py +++ b/mangle/image.py @@ -24,12 +24,14 @@ class ImageFlags: Resize = 1 << 1 Frame = 1 << 2 Quantize = 1 << 3 - ScaleCrop = 1 << 4 + Fit = 1 << 4 SplitRightLeft = 1 << 5 # split right then left SplitRight = 1 << 6 # split only the right page SplitLeft = 1 << 7 # split only the left page SplitLeftRight = 1 << 8 # split left then right page AutoCrop = 1 << 9 + Fill = 1 << 10 + Stretch = 1 << 11 class KindleData: @@ -134,10 +136,35 @@ def quantizeImage(image, palette): @protect_bad_image -def scaleCropImage(image, size): +def fitImage(image, size, method=Image.ANTIALIAS): + # copied from ImageOps.contain() from the Python3 version of Pillow + # with division related modifications for Python2 + + im_ratio = 1.0 * image.width / image.height + dest_ratio = 1.0 * size[0] / size[1] + + if im_ratio != dest_ratio: + if im_ratio > dest_ratio: + new_height = int(1.0 * image.height / image.width * size[0]) + if new_height != size[1]: + size = (size[0], new_height) + else: + new_width = int(1.0 * image.width / image.height * size[1]) + if new_width != size[0]: + size = (new_width, size[1]) + return image.resize(size, resample=method) + + +@protect_bad_image +def fillImage(image, size): return ImageOps.fit(image, size, Image.ANTIALIAS) +@protect_bad_image +def stretchImage(image, size): + return image.resize(size, Image.ANTIALIAS) + + @protect_bad_image def resizeImage(image, size): widthDev, heightDev = size @@ -275,8 +302,12 @@ def convertImage(source, target, device, flags): image = orientImage(image, size) if flags & ImageFlags.Resize: image = resizeImage(image, size) - if flags & ImageFlags.ScaleCrop: - image = scaleCropImage(image, size) + if flags & ImageFlags.Fit: + image = fitImage(image, size) + if flags & ImageFlags.Fill: + image = fillImage(image, size) + if flags & ImageFlags.Stretch: + image = stretchImage(image, size) if flags & ImageFlags.Frame: image = frameImage(image, tuple(palette[:3]), tuple(palette[-3:]), size) if flags & ImageFlags.Quantize: diff --git a/mangle/options.py b/mangle/options.py index 8829463..e798735 100644 --- a/mangle/options.py +++ b/mangle/options.py @@ -43,7 +43,9 @@ class DialogOptions(QtGui.QDialog): self.checkboxOverwrite.setChecked(self.book.overwrite) self.checkboxOrient.setChecked(self.book.imageFlags & ImageFlags.Orient) self.checkboxResize.setChecked(self.book.imageFlags & ImageFlags.Resize) - self.checkboxScaleCrop.setChecked(self.book.imageFlags & ImageFlags.ScaleCrop) + self.checkboxFit.setChecked(self.book.imageFlags & ImageFlags.Fit) + self.checkboxFill.setChecked(self.book.imageFlags & ImageFlags.Fill) + self.checkboxStretch.setChecked(self.book.imageFlags & ImageFlags.Stretch) self.checkboxQuantize.setChecked(self.book.imageFlags & ImageFlags.Quantize) self.checkboxFrame.setChecked(self.book.imageFlags & ImageFlags.Frame) @@ -62,8 +64,12 @@ class DialogOptions(QtGui.QDialog): imageFlags |= ImageFlags.Orient if self.checkboxResize.isChecked(): imageFlags |= ImageFlags.Resize - if self.checkboxScaleCrop.isChecked(): - imageFlags |= ImageFlags.ScaleCrop + if self.checkboxFit.isChecked(): + imageFlags |= ImageFlags.Fit + if self.checkboxFill.isChecked(): + imageFlags |= ImageFlags.Fill + if self.checkboxStretch.isChecked(): + imageFlags |= ImageFlags.Stretch if self.checkboxQuantize.isChecked(): imageFlags |= ImageFlags.Quantize if self.checkboxFrame.isChecked(): diff --git a/mangle/ui/options.ui b/mangle/ui/options.ui index 29cedbb..b31b690 100644 --- a/mangle/ui/options.ui +++ b/mangle/ui/options.ui @@ -215,9 +215,23 @@ - + - Scale and crop images to fill the screen + Fit to screen with borders + + + + + + + Fill screen by cropping + + + + + + + Stretch images to fill screen