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

Commit

Permalink
Added "Split left and right pages" option
Browse files Browse the repository at this point in the history
Also cleaned formatting according to Eclipse PyDev
  • Loading branch information
catmanjan committed Oct 17, 2013
1 parent 9c065b5 commit 1b2c530
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 47 deletions.
2 changes: 2 additions & 0 deletions mangle.pyw
Expand Up @@ -17,7 +17,9 @@


import sys

from PyQt4 import QtGui

from mangle.book import MainWindowBook


Expand Down
4 changes: 2 additions & 2 deletions mangle/about.py
Expand Up @@ -14,10 +14,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.


import os.path
import util
from PyQt4 import QtGui, uic

import util


class DialogAbout(QtGui.QDialog):
def __init__(self, parent):
Expand Down
32 changes: 16 additions & 16 deletions mangle/book.py
Expand Up @@ -14,18 +14,19 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.


import os
import os.path
from os.path import basename
import util
import os.path
import tempfile
from PyQt4 import QtGui, QtCore, QtXml, uic
from zipfile import ZipFile
from image import ImageFlags

from PyQt4 import QtGui, QtCore, QtXml, uic
from natsort import natsorted

from about import DialogAbout
from options import DialogOptions
from convert import DialogConvert
from natsort import natsorted
from image import ImageFlags
from options import DialogOptions
import util


class Book(object):
Expand Down Expand Up @@ -250,7 +251,7 @@ def onBookExport(self):
QtGui.QMessageBox.warning(self, 'Mangle', 'This book has no images to export')
return

if not self.book.titleSet: # if self.book.title is None:
if not self.book.titleSet: # if self.book.title is None:
dialog = DialogOptions(self, self.book)
if dialog.exec_() == QtGui.QDialog.Rejected:
return
Expand Down Expand Up @@ -378,7 +379,7 @@ def addImageDirs(self, directories):
filenames = []

for directory in directories:
for root, subdirs, subfiles in os.walk(unicode(directory)):
for root, _, subfiles in os.walk(unicode(directory)):
for filename in subfiles:
path = os.path.join(root, filename)
if self.isImageFile(path):
Expand All @@ -402,17 +403,16 @@ def addCBZFiles(self, filenames):
for f in cbzFile.namelist():
if f.endswith('/'):
try:
os.makedirs(path+f)
os.makedirs(path + f)
except:
pass #the dir exists so we are going to extract the images only.
pass # the dir exists so we are going to extract the images only.
else:
cbzFile.extract(f, path)
#Add the directories
if os.path.isdir(unicode(path)):
if os.path.isdir(unicode(path)): # Add the directories
directories.append(path)
#Add the files
self.addImageDirs(directories)

self.addImageDirs(directories) # Add the files


def isImageFile(self, filename):
imageExts = ['.jpeg', '.jpg', '.gif', '.png']
Expand Down
17 changes: 10 additions & 7 deletions mangle/convert.py
Expand Up @@ -14,10 +14,13 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.


import os, shutil
import os
import shutil

from PyQt4 import QtGui, QtCore
import image

import cbz
import image
import pdfimage


Expand All @@ -39,8 +42,8 @@ def __init__(self, parent, book, directory):

self.pdf = None
if "PDF" in self.book.outputFormat:
self.pdf = pdfimage.PDFImage(self.bookPath, str(self.book.title), str(self.book.device))
self.pdf = pdfimage.PDFImage(self.bookPath, str(self.book.title), str(self.book.device))



def showEvent(self, event):
Expand All @@ -56,9 +59,9 @@ def hideEvent(self, event):
# Close the archive if we created a CBZ file
if self.archive is not None:
self.archive.close()
#Close and generate the PDF File
# Close and generate the PDF File
if self.pdf is not None:
self.pdf.close()
self.pdf.close()

# Remove image directory if the user didn't wish for images
if 'Image' not in self.book.outputFormat:
Expand Down Expand Up @@ -108,7 +111,7 @@ def onTimer(self):
if self.archive is not None:
self.archive.addFile(target)
if self.pdf is not None:
self.pdf.addImage(target)
self.pdf.addImage(target)
except RuntimeError, error:
result = QtGui.QMessageBox.critical(
self,
Expand Down
39 changes: 32 additions & 7 deletions mangle/image.py
Expand Up @@ -14,6 +14,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.


import os

from PIL import Image, ImageDraw


Expand All @@ -23,6 +25,8 @@ class ImageFlags:
Frame = 1 << 2
Quantize = 1 << 3
Stretch = 1 << 4
Split = 1 << 5
SplitRight = 1 << 6


class KindleData:
Expand Down Expand Up @@ -79,6 +83,18 @@ class KindleData:
'Kindle DXG': ((824, 1200), Palette15a),
'Kindle Paperwhite': ((758, 1024), Palette15b)
}


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

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


def splitRight(image):
widthImg, heightImg = image.size

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


def quantizeImage(image, palette):
Expand All @@ -93,8 +109,8 @@ def quantizeImage(image, palette):


def stretchImage(image, size):
widthDev, heightDev = size
return image.resize((widthDev, heightDev), Image.ANTIALIAS)
widthDev, heightDev = size
return image.resize((widthDev, heightDev), Image.ANTIALIAS)

def resizeImage(image, size):
widthDev, heightDev = size
Expand Down Expand Up @@ -175,15 +191,24 @@ def convertImage(source, target, device, flags):
raise RuntimeError('Cannot read image file %s' % source)
image = formatImage(image)
if flags & ImageFlags.Orient:
image = orientImage(image, size)
image = orientImage(image, size)
if flags & ImageFlags.SplitRight:
image = splitRight(image)
elif flags & ImageFlags.Split:
image = splitLeft(image)
# Recurse for right page
fileName, fileExtension = os.path.splitext(target)
newTarget = fileName + "r" + fileExtension
print(newTarget)
convertImage(source, newTarget, device, flags | ImageFlags.SplitRight)
if flags & ImageFlags.Resize:
image = resizeImage(image, size)
image = resizeImage(image, size)
if flags & ImageFlags.Stretch:
image = stretchImage(image, size)
image = stretchImage(image, size)
if flags & ImageFlags.Frame:
image = frameImage(image, tuple(palette[:3]), tuple(palette[-3:]), size)
image = frameImage(image, tuple(palette[:3]), tuple(palette[-3:]), size)
if flags & ImageFlags.Quantize:
image = quantizeImage(image, palette)
image = quantizeImage(image, palette)

try:
image.save(target)
Expand Down
12 changes: 7 additions & 5 deletions mangle/options.py
Expand Up @@ -14,10 +14,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.


import os.path
import util
from PyQt4 import QtGui, QtCore, uic
from PyQt4 import QtGui, uic

from image import ImageFlags
import util


class DialogOptions(QtGui.QDialog):
Expand Down Expand Up @@ -59,11 +59,13 @@ def moveDialogToOptions(self):
if self.checkboxResize.isChecked():
imageFlags |= ImageFlags.Resize
if self.checkboxStretch.isChecked():
imageFlags |= ImageFlags.Stretch
imageFlags |= ImageFlags.Stretch
if self.checkboxQuantize.isChecked():
imageFlags |= ImageFlags.Quantize
if self.checkboxFrame.isChecked():
imageFlags |= ImageFlags.Frame
imageFlags |= ImageFlags.Frame
if self.checkboxSplit.isChecked():
imageFlags |= ImageFlags.Split

modified = (
self.book.title != title or
Expand Down
5 changes: 3 additions & 2 deletions mangle/pdfimage.py
Expand Up @@ -17,9 +17,10 @@
import os.path

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter

from image import KindleData


class PDFImage(object):
def __init__(self, path, title, device):
outputDirectory = os.path.dirname(path)
Expand All @@ -28,7 +29,7 @@ def __init__(self, path, title, device):
self.currentDevice = device
self.bookTitle = title
self.pageSize = KindleData.Profiles[self.currentDevice][0]
#pagesize could be letter or A4 for standarization but we need to control some image sizes
# pagesize could be letter or A4 for standarization but we need to control some image sizes
self.canvas = canvas.Canvas(outputPath, pagesize=self.pageSize)
self.canvas.setAuthor("Mangle")
self.canvas.setTitle(self.bookTitle)
Expand Down
7 changes: 7 additions & 0 deletions mangle/ui/options.ui
Expand Up @@ -158,6 +158,13 @@
<string>Draw frame around images</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkboxSplit">
<property name="text">
<string>Split left and right pages</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_3">
Expand Down
1 change: 0 additions & 1 deletion mangle/util.py
Expand Up @@ -15,7 +15,6 @@


import os.path
import re
import sys


Expand Down
13 changes: 6 additions & 7 deletions setup.py
Expand Up @@ -16,16 +16,15 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.


import sys
from distutils.core import setup
import py2exe
import sys


sys.argv.append('py2exe')
setup(
name = 'Mangle',
windows = [{'script': 'mangle.pyw'}],
data_files = [('', ['LICENSE']),
name='Mangle',
windows=[{'script': 'mangle.pyw'}],
data_files=[('', ['LICENSE']),
('mangle/ui', ['mangle/ui/book.ui',
'mangle/ui/about.ui',
'mangle/ui/options.ui']),
Expand All @@ -39,11 +38,11 @@
'mangle/img/save_file.png',
'mangle/img/shift_down.png',
'mangle/img/shift_up.png'])],
options = {'py2exe': {
options={'py2exe': {
'bundle_files': 1,
'includes': ['sip'],
'packages': ['reportlab.pdfbase'],
'dll_excludes': ['w9xpopen.exe']
}},
zipfile = None
zipfile=None
)

0 comments on commit 1b2c530

Please sign in to comment.