Skip to content
This repository has been archived by the owner on Oct 31, 2022. It is now read-only.

Fix bug where splitting pages resulted in out-of-order and over-writt… #27

Merged
merged 1 commit into from
Aug 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions mangle/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __init__(self, parent, book, directory):
self.setWindowTitle('Exporting book...')
self.setMaximum(len(self.book.images))
self.setValue(0)
self.increment = 0

self.archive = None
if 'CBZ' in self.book.outputFormat:
Expand Down Expand Up @@ -79,7 +80,8 @@ def convertAndSave(self, source, target, device, flags, archive, pdf):

def onTimer(self):
index = self.value()
target = os.path.join(self.bookPath, '%05d.png' % index)
pages_split = self.increment
target = os.path.join(self.bookPath, '%05d.png' % (index + pages_split))
source = unicode(self.book.images[index])

if index == 0:
Expand Down Expand Up @@ -121,30 +123,34 @@ def onTimer(self):
archive = self.archive
pdf = self.pdf

# Maybe the user ask for a split, but the picture is not a large one, so skip
# it but only for this picture
if (flags & ImageFlags.Split) or (flags & ImageFlags.SplitInverse):
# Check if page wide enough to split
if (flags & ImageFlags.SplitRightLeft) or (flags & ImageFlags.SplitLeftRight):
if not image.isSplitable(source):
# remove split flags
splitFlags= [ImageFlags.Split, ImageFlags.SplitInverse, ImageFlags.SplitRight, ImageFlags.SplitLeft]
splitFlags = [ImageFlags.SplitRightLeft, ImageFlags.SplitLeftRight, ImageFlags.SplitRight,
ImageFlags.SplitLeft]
for f in splitFlags:
flags &= ~f

# For right page (if requested in options and need for this image)
if (flags & ImageFlags.Split):
# New path based on modified index
target = os.path.join(self.bookPath, '%05d.png' % (index * 2 + 0))
self.convertAndSave(source, target, device, flags ^ ImageFlags.Split | ImageFlags.SplitRight, archive, pdf)
# Change target once again for left page
target = os.path.join(self.bookPath, '%05d.png' % (index * 2 + 1))
if flags & ImageFlags.SplitRightLeft:
self.convertAndSave(source, target, device,
flags ^ ImageFlags.SplitRightLeft | ImageFlags.SplitRight,
archive, pdf)

# Change target for left page
target = os.path.join(self.bookPath, '%05d.png' % (index + pages_split + 1))
self.increment += 1

# For right page (if requested), but in inverted mode
if (flags & 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))
if flags & ImageFlags.SplitLeftRight:
self.convertAndSave(source, target, device,
flags ^ ImageFlags.SplitLeftRight | ImageFlags.SplitLeft,
archive, pdf)

# Change target for left page
target = os.path.join(self.bookPath, '%05d.png' % (index + pages_split + 1))
self.increment += 1

# Convert page
self.convertAndSave(source, target, device, flags, archive, pdf)
Expand Down
41 changes: 17 additions & 24 deletions mangle/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@


class ImageFlags:
Orient = 1 << 0
Resize = 1 << 1
Frame = 1 << 2
Quantize = 1 << 3
Stretch = 1 << 4
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
AutoCrop = 1 << 9 # split left then right page
Orient = 1 << 0
Resize = 1 << 1
Frame = 1 << 2
Quantize = 1 << 3
Stretch = 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


class KindleData:
Expand Down Expand Up @@ -103,12 +103,14 @@ def func_wrapper(*args, **kwargs):
return func(*args, **kwargs)
except IOError: # Exception from PIL about bad image
return args[0]

return func_wrapper


@protect_bad_image
def splitLeft(image):
widthImg, heightImg = image.size

return image.crop((0, 0, widthImg / 2, heightImg))


Expand Down Expand Up @@ -189,6 +191,7 @@ def autoCropImage(image):
except TypeError: # bad image, specific to chops
return image
image = image.crop((x0, y0, xend, yend))

return image


Expand Down Expand Up @@ -254,29 +257,22 @@ def convertImage(source, target, device, flags):
# Load image from source path
image = loadImage(source)



# Format according to palette
image = formatImage(image)
# Apply flag transforms

# Second pass of first split
# Apply flag transforms
if flags & ImageFlags.SplitRight:
image = splitRight(image)
# First pass of first split option
if (flags & ImageFlags.Split):
if flags & ImageFlags.SplitRightLeft:
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):
if flags & ImageFlags.SplitLeftRight:
image = splitRight(image)

# Auto crop the image, but before manage size and co, clean the source so
if flags & ImageFlags.AutoCrop:
image = autoCropImage(image)

if flags & ImageFlags.Orient:
image = orientImage(image, size)
if flags & ImageFlags.Resize:
Expand All @@ -285,10 +281,7 @@ def convertImage(source, target, device, flags):
image = stretchImage(image, size)
if flags & ImageFlags.Frame:
image = frameImage(image, tuple(palette[:3]), tuple(palette[-3:]), size)



if flags & ImageFlags.Quantize:
image = quantizeImage(image, palette)

saveImage(image, target)
saveImage(image, target)
4 changes: 2 additions & 2 deletions mangle/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ def moveDialogToOptions(self):
if self.checkboxFrame.isChecked():
imageFlags |= ImageFlags.Frame
if self.checkboxSplit.isChecked():
imageFlags |= ImageFlags.Split
imageFlags |= ImageFlags.SplitRightLeft
if self.checkboxSplitInverse.isChecked():
imageFlags |= ImageFlags.SplitInverse
imageFlags |= ImageFlags.SplitLeftRight
if self.checkboxAutoCrop.isChecked():
imageFlags |= ImageFlags.AutoCrop

Expand Down