Skip to content

Commit

Permalink
add --mozjpeg option and GUI checkBox
Browse files Browse the repository at this point in the history
  • Loading branch information
darodi committed Apr 10, 2022
1 parent 62984d0 commit a5c2a2c
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ build/
KindleComicConverter.egg-info/
.idea/
/venv/
/kindlegen.exe
10 changes: 10 additions & 0 deletions gui/KCC.ui
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QCheckBox" name="mozJpegBox">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; font-weight:600; text-decoration: underline;&quot;&gt;Unchecked - Off&lt;br/&gt;&lt;/span&gt;Faster&lt;/p&gt;&lt;p&gt;&lt;span style=&quot; font-weight:600; text-decoration: underline;&quot;&gt;Checked - On&lt;br/&gt;&lt;/span&gt;10-20% smaller JPEG file, with the same image quality, but processing time multiplied by 2&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>use mozJpeg</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down
7 changes: 6 additions & 1 deletion kindlecomicconverter/KCC_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# PERFORMANCE OF THIS SOFTWARE.

import os
import re
import sys
from urllib.parse import unquote
from urllib.request import urlopen, urlretrieve, Request
Expand Down Expand Up @@ -143,7 +144,9 @@ def run(self):
except Exception:
return
latestVersion = XML.childNodes[0].getElementsByTagName('LatestVersion')[0].childNodes[0].toxml()
if StrictVersion(latestVersion) > StrictVersion(__version__):
if ("beta" not in __version__ and StrictVersion(latestVersion) > StrictVersion(__version__)) \
or ("beta" in __version__
and StrictVersion(latestVersion) >= StrictVersion(re.sub(r'-beta.*', '', __version__))):
if sys.platform.startswith('win'):
self.newVersion = latestVersion
self.md5 = XML.childNodes[0].getElementsByTagName('MD5')[0].childNodes[0].toxml()
Expand Down Expand Up @@ -279,6 +282,8 @@ def run(self):
options.forcecolor = True
if GUI.disableProcessingBox.isChecked():
options.noprocessing = True
if GUI.mozJpegBox.isChecked():
options.mozjpeg = True
if GUI.currentMode > 2:
options.customwidth = str(GUI.widthBox.value())
options.customheight = str(GUI.heightBox.value())
Expand Down
7 changes: 6 additions & 1 deletion kindlecomicconverter/KCC_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Form implementation generated from reading ui file 'gui/KCC.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
Expand Down Expand Up @@ -62,6 +62,9 @@ def setupUi(self, mainWindow):
self.disableProcessingBox = QtWidgets.QCheckBox(self.optionWidget)
self.disableProcessingBox.setObjectName("disableProcessingBox")
self.gridLayout_2.addWidget(self.disableProcessingBox, 3, 2, 1, 1)
self.mozJpegBox = QtWidgets.QCheckBox(self.optionWidget)
self.mozJpegBox.setObjectName("mozJpegBox")
self.gridLayout_2.addWidget(self.mozJpegBox, 3, 1, 1, 1)
self.gridLayout.addWidget(self.optionWidget, 4, 0, 1, 2)
self.gammaWidget = QtWidgets.QWidget(self.centralWidget)
self.gammaWidget.setVisible(False)
Expand Down Expand Up @@ -258,6 +261,8 @@ def retranslateUi(self, mainWindow):
self.qualityBox.setText(_translate("mainWindow", "Panel View 4/2/HQ"))
self.disableProcessingBox.setToolTip(_translate("mainWindow", "<html><head/><body><p style=\'white-space:pre\'>Do not process any image, ignore profil and processing options</p></body></html>"))
self.disableProcessingBox.setText(_translate("mainWindow", "Disable processing"))
self.mozJpegBox.setToolTip(_translate("mainWindow", "<html><head/><body><p><span style=\" font-weight:600; text-decoration: underline;\">Unchecked - Off<br/></span>Faster</p><p><span style=\" font-weight:600; text-decoration: underline;\">Checked - On<br/></span>10-20% smaller JPEG file, with the same image quality, but processing time multiplied by 2</p></body></html>"))
self.mozJpegBox.setText(_translate("mainWindow", "use mozJpeg"))
self.gammaLabel.setText(_translate("mainWindow", "Gamma: Auto"))
self.directoryButton.setToolTip(_translate("mainWindow", "<html><head/><body><p style=\'white-space:pre\'>Add directory containing JPG, PNG or GIF files to queue.<br/><span style=\" font-weight:600;\">CBR, CBZ and CB7 files inside will not be processed!</span></p></body></html>"))
self.directoryButton.setText(_translate("mainWindow", "Add directory"))
Expand Down
2 changes: 2 additions & 0 deletions kindlecomicconverter/comic2ebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,8 @@ def makeParser():
help="Don't convert images to grayscale")
processingOptions.add_option("--forcepng", action="store_true", dest="forcepng", default=False,
help="Create PNG files instead JPEG")
processingOptions.add_option("--mozjpeg", action="store_true", dest="mozjpeg", default=False,
help="Create JPEG files using mozJpeg")

customProfileOptions.add_option("--customwidth", type="int", dest="customwidth", default=0,
help="Replace screen width provided by device profile")
Expand Down
17 changes: 10 additions & 7 deletions kindlecomicconverter/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import io
import os
import mozjpeg_lossless_optimization
from PIL import Image, ImageOps, ImageStat, ImageChops, ImageFilter
Expand Down Expand Up @@ -245,12 +245,15 @@ def saveToDir(self):
self.image.save(self.targetPath, 'PNG', optimize=1)
else:
self.targetPath += '.jpg'
self.image.save(self.targetPath, 'JPEG', optimize=1, quality=85)
with open(self.targetPath, "rb") as input_jpeg_file:
input_jpeg_bytes = input_jpeg_file.read()
output_jpeg_bytes = mozjpeg_lossless_optimization.optimize(input_jpeg_bytes)
with open(self.targetPath, "wb") as output_jpeg_file:
output_jpeg_file.write(output_jpeg_bytes)
if self.opt.mozjpeg:
with io.BytesIO() as output:
self.image.save(output, format="JPEG")
input_jpeg_bytes = output.getvalue()
output_jpeg_bytes = mozjpeg_lossless_optimization.optimize(input_jpeg_bytes)
with open(self.targetPath, "wb") as output_jpeg_file:
output_jpeg_file.write(output_jpeg_bytes)
else:
self.image.save(self.targetPath, 'JPEG', optimize=1, quality=85)
return [md5Checksum(self.targetPath), flags, self.orgPath]
except IOError as err:
raise RuntimeError('Cannot save image. ' + str(err))
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ Pillow>=5.2.0
psutil>=5.0.0
python-slugify>=1.2.1,<3.0.0
raven>=6.0.0
# PyQt5-tools
mozjpeg-lossless-optimization

0 comments on commit a5c2a2c

Please sign in to comment.