Merge pull request #18 from naparuba/master

Add: left=>right split, Kobo Aura h2o and fix natsort sorting
This commit is contained in:
Alex Yatskov 2015-09-26 09:27:56 +09:00
commit 62ddf64add
10 changed files with 58 additions and 9 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@ dist
*.pyo
*.mngl
*.cbz
*.bat

View File

@ -80,7 +80,6 @@ of your choice. First you should make sure that you have the required dependenci
* [Python 2.7](http://www.python.org/download/releases/2.7/)
* [Python Imaging Library (PIL)](http://www.pythonware.com/products/pil/)
* [ReportLab](https://pypi.python.org/pypi/reportlab)
* [natsort](https://pypi.python.org/pypi/natsort/3.0.1)
* [py2exe](http://www.py2exe.org/) (optional, for Windows distribution only)
Now you can fetch the [latest version of the code](https://github.com/FooSoft/mangle/) and run the `mangle.pyw` script

View File

@ -13,14 +13,13 @@
# 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 re
from os.path import basename
import os.path
import tempfile
from zipfile import ZipFile
from PyQt4 import QtGui, QtCore, QtXml, uic
from natsort import natsorted
from about import DialogAbout
from convert import DialogConvert
@ -29,6 +28,13 @@ from options import DialogOptions
import util
# Sort function use to sort files in a natural order, by lowering
# characters, and manage multi levels of integers (tome 1/ page 1.jpg, etc etc)
def natural_key(string_):
"""See http://www.codinghorror.com/blog/archives/001018.html"""
return [int(s) if s.isdigit() else s.lower() for s in re.split(r'(\d+)', string_)]
class Book(object):
DefaultDevice = 'Kindle Paperwhite'
DefaultOutputFormat = 'CBZ only'
@ -368,13 +374,15 @@ class MainWindowBook(QtGui.QMainWindow):
for i in xrange(0, self.listWidgetFiles.count()):
filenamesListed.append(self.listWidgetFiles.item(i).text())
for filename in natsorted(filenames):
# Get files but in a natural sorted order
for filename in sorted(filenames, key=natural_key):
if filename not in filenamesListed:
filename = QtCore.QString(filename)
self.listWidgetFiles.addItem(filename)
self.book.images.append(filename)
self.book.modified = True
def addImageDirs(self, directories):
filenames = []
@ -387,6 +395,7 @@ class MainWindowBook(QtGui.QMainWindow):
self.addImageFiles(filenames)
def addCBZFiles(self, filenames):
directories = []
tempDir = tempfile.gettempdir()

View File

@ -129,6 +129,14 @@ class DialogConvert(QtGui.QProgressDialog):
# Change target once again for left page
target = os.path.join(self.bookPath, '%05d.png' % (index * 2 + 1))
# For right page (if requested), but in inverted mode
if(self.book.imageFlags & ImageFlags.SplitInverse):
# New path based on modified index
target = os.path.join(self.bookPath, '%05d.png' % (index * 2 + 0))
self.convertAndSave(source, target, device, flags ^ ImageFlags.SplitInverse | ImageFlags.SplitLeft, archive, pdf)
# Change target once again for left page
target = os.path.join(self.bookPath, '%05d.png' % (index * 2 + 1))
# Convert page
self.convertAndSave(source, target, device, flags, archive, pdf)

View File

@ -19,14 +19,17 @@ import os
from PIL import Image, ImageDraw
class ImageFlags:
Orient = 1 << 0
Resize = 1 << 1
Frame = 1 << 2
Quantize = 1 << 3
Stretch = 1 << 4
Split = 1 << 5
SplitRight = 1 << 6
Split = 1 << 5 # split right then left
SplitRight = 1 << 6 # split only the right page
SplitLeft = 1 << 7 # split only the left page
SplitInverse = 1 << 8 # split left then right page
class KindleData:
@ -82,7 +85,8 @@ class KindleData:
'Kindle DX': ((824, 1200), Palette15a),
'Kindle DXG': ((824, 1200), Palette15a),
'Kindle Touch': ((600, 800), Palette15a),
'Kindle Paperwhite': ((758, 1024), Palette15b) # resolution given in manual, see http://kindle.s3.amazonaws.com/Kindle_Paperwhite_Users_Guide.pdf
'Kindle Paperwhite': ((758, 1024), Palette15b), # resolution given in manual, see http://kindle.s3.amazonaws.com/Kindle_Paperwhite_Users_Guide.pdf
'KoBo Aura H2o': ((1080, 1430), Palette15a), # resolution from http://www.fnac.com/Liseuse-Numerique-Kobo-by-Fnac-Kobo-Aura-H2O-Noir/a7745120/w-4
}
@ -204,10 +208,20 @@ def convertImage(source, target, device, flags):
# Format according to palette
image = formatImage(image)
# Apply flag transforms
# Second pass of first split
if flags & ImageFlags.SplitRight:
image = splitRight(image)
if flags & ImageFlags.Split:
# First pass of first split option
if (flags & ImageFlags.Split):
image = splitLeft(image)
# First pass of second splitting option
if flags & ImageFlags.SplitLeft:
image = splitLeft(image)
# second pass of second splitting option
if (flags & ImageFlags.SplitInverse):
image = splitRight(image)
if flags & ImageFlags.Orient:
image = orientImage(image, size)
if flags & ImageFlags.Resize:

BIN
mangle/img/book.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 398 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 827 B

After

Width:  |  Height:  |  Size: 545 B

View File

@ -66,6 +66,8 @@ class DialogOptions(QtGui.QDialog):
imageFlags |= ImageFlags.Frame
if self.checkboxSplit.isChecked():
imageFlags |= ImageFlags.Split
if self.checkboxSplitInverse.isChecked():
imageFlags |= ImageFlags.SplitInverse
modified = (
self.book.title != title or

View File

@ -180,6 +180,10 @@
</property>
</action>
<action name="actionBookOptions">
<property name="icon">
<iconset>
<normaloff>../img/book.png</normaloff>../img/book.png</iconset>
</property>
<property name="text">
<string>&amp;Options...</string>
</property>

View File

@ -101,6 +101,11 @@
<string>Kindle Paperwhite</string>
</property>
</item>
<item>
<property name="text">
<string>KoBo Aura H2o</string>
</property>
</item>
</widget>
</item>
<item row="1" column="0">
@ -170,6 +175,13 @@
<string>Split images into two pages (right, left)</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkboxSplitInverse">
<property name="text">
<string>Split images into two pages (left, right)</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_3">