Changed to split right -> left

This commit is contained in:
Jan 2013-10-17 22:02:51 +11:00
parent 1b2c5308eb
commit 0491b4f713
3 changed files with 52 additions and 23 deletions

View File

@ -18,6 +18,7 @@ import os
import shutil import shutil
from PyQt4 import QtGui, QtCore from PyQt4 import QtGui, QtCore
from image import ImageFlags
import cbz import cbz
import image import image
@ -68,6 +69,14 @@ class DialogConvert(QtGui.QProgressDialog):
shutil.rmtree(self.bookPath) shutil.rmtree(self.bookPath)
def convertAndSave(self, source, target, device, flags, archive, pdf):
image.convertImage(source, target, device, flags)
if archive is not None:
archive.addFile(target)
if pdf is not None:
pdf.addImage(target)
def onTimer(self): def onTimer(self):
index = self.value() index = self.value()
target = os.path.join(self.bookPath, '%05d.png' % index) target = os.path.join(self.bookPath, '%05d.png' % index)
@ -107,11 +116,26 @@ class DialogConvert(QtGui.QProgressDialog):
try: try:
if self.book.overwrite or not os.path.isfile(target): if self.book.overwrite or not os.path.isfile(target):
image.convertImage(source, target, str(self.book.device), self.book.imageFlags) device = str(self.book.device)
if self.archive is not None: flags = self.book.imageFlags
self.archive.addFile(target) archive = self.archive
if self.pdf is not None: pdf = self.pdf
self.pdf.addImage(target)
# For right page (if requested)
if(self.book.imageFlags & ImageFlags.Split):
# Increment counter to accomodate for extra page
index = index + 1
target = os.path.join(self.bookPath, '%05d.png' % index)
self.convertAndSave(source,
target
device,
flags ^ ImageFlags.Split | ImageFlags.SplitRight,
archive,
pdf)
# Convert page
self.convertAndSave(source, target, device, flags, archive, pdf)
except RuntimeError, error: except RuntimeError, error:
result = QtGui.QMessageBox.critical( result = QtGui.QMessageBox.critical(
self, self,

View File

@ -179,28 +179,36 @@ def frameImage(image, foreground, background, size):
return imageBg return imageBg
def loadImage(source):
try:
return Image.open(source)
except IOError:
raise RuntimeError('Cannot read image file %s' % source)
def saveImage(image, target):
try:
image.save(target)
except IOError:
raise RuntimeError('Cannot write image file %s' % target)
def convertImage(source, target, device, flags): def convertImage(source, target, device, flags):
try: try:
size, palette = KindleData.Profiles[device] size, palette = KindleData.Profiles[device]
except KeyError: except KeyError:
raise RuntimeError('Unexpected output device %s' % device) raise RuntimeError('Unexpected output device %s' % device)
# Load image from source path
try: image = loadImage(source)
image = Image.open(source) # Format according to palette
except IOError:
raise RuntimeError('Cannot read image file %s' % source)
image = formatImage(image) image = formatImage(image)
if flags & ImageFlags.Orient: # Apply flag transforms
image = orientImage(image, size)
if flags & ImageFlags.SplitRight: if flags & ImageFlags.SplitRight:
image = splitRight(image) image = splitRight(image)
elif flags & ImageFlags.Split: if flags & ImageFlags.Split:
image = splitLeft(image) image = splitLeft(image)
# Recurse for right page if flags & ImageFlags.Orient:
fileName, fileExtension = os.path.splitext(target) image = orientImage(image, size)
newTarget = fileName + "r" + fileExtension
print(newTarget)
convertImage(source, newTarget, device, flags | ImageFlags.SplitRight)
if flags & ImageFlags.Resize: if flags & ImageFlags.Resize:
image = resizeImage(image, size) image = resizeImage(image, size)
if flags & ImageFlags.Stretch: if flags & ImageFlags.Stretch:
@ -210,7 +218,4 @@ def convertImage(source, target, device, flags):
if flags & ImageFlags.Quantize: if flags & ImageFlags.Quantize:
image = quantizeImage(image, palette) image = quantizeImage(image, palette)
try: saveImage(image, target)
image.save(target)
except IOError:
raise RuntimeError('Cannot write image file %s' % target)

View File

@ -162,7 +162,7 @@
<item> <item>
<widget class="QCheckBox" name="checkboxSplit"> <widget class="QCheckBox" name="checkboxSplit">
<property name="text"> <property name="text">
<string>Split left and right pages</string> <string>Split images into two pages (right, left)</string>
</property> </property>
</widget> </widget>
</item> </item>