1
0
mirror of https://github.com/ciromattia/kcc synced 2025-12-18 20:21:48 +00:00

Mainly adhere to PEP 8 code style (http://www.python.org/dev/peps/pep-0008/)

Add some commented code for working on Panel view enhancement and natural sorting.
This commit is contained in:
Ciro Mattia Gonano
2013-03-01 16:22:11 +01:00
parent 1edbe389ba
commit 32a0038c49
3 changed files with 142 additions and 132 deletions

View File

@@ -20,6 +20,7 @@ __docformat__ = 'restructuredtext en'
import os import os
class CBxArchive: class CBxArchive:
def __init__(self, origFileName): def __init__(self, origFileName):
self.cbxexts = ['.zip', '.cbz', '.rar', '.cbr'] self.cbxexts = ['.zip', '.cbz', '.rar', '.cbr']
@@ -69,10 +70,10 @@ class CBxArchive:
self.extractCBR(targetdir) self.extractCBR(targetdir)
elif '.cbz' == self.filename[1].lower() or '.zip' == self.filename[1].lower(): elif '.cbz' == self.filename[1].lower() or '.zip' == self.filename[1].lower():
self.extractCBZ(targetdir) self.extractCBZ(targetdir)
dir = os.listdir(targetdir) adir = os.listdir(targetdir)
if len(dir) == 1 and os.path.isdir(os.path.join(targetdir,dir[0])): if len(adir) == 1 and os.path.isdir(os.path.join(targetdir, adir[0])):
import shutil import shutil
for f in os.listdir(os.path.join(targetdir,dir[0])): for f in os.listdir(os.path.join(targetdir, adir[0])):
shutil.move(os.path.join(targetdir,dir[0],f),targetdir) shutil.move(os.path.join(targetdir, adir[0], f), targetdir)
os.rmdir(os.path.join(targetdir,dir[0])) os.rmdir(os.path.join(targetdir, adir[0]))
return targetdir return targetdir

View File

@@ -21,11 +21,17 @@ __copyright__ = '2012-2013, Ciro Mattia Gonano <ciromattia@gmail.com>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
from Tkinter import * from Tkinter import *
import tkFileDialog, tkMessageBox, ttk import tkFileDialog
import comic2ebook, kindlestrip import tkMessageBox
import ttk
import comic2ebook
import kindlestrip
from image import ProfileData from image import ProfileData
from subprocess import call, Popen, PIPE, STDOUT from subprocess import call
import os, shutil, stat import os
import shutil
import stat
class MainWindow: class MainWindow:
@@ -58,8 +64,8 @@ class MainWindow:
def refresh_list(self): def refresh_list(self):
self.filelocation.config(state=NORMAL) self.filelocation.config(state=NORMAL)
self.filelocation.delete(0, END) self.filelocation.delete(0, END)
for file in self.filelist: for afile in self.filelist:
self.filelocation.insert(END, file) self.filelocation.insert(END, afile)
self.filelocation.config(state=DISABLED) self.filelocation.config(state=DISABLED)
def initialize(self): def initialize(self):
@@ -107,9 +113,9 @@ class MainWindow:
self.submit.grid(column=3) self.submit.grid(column=3)
self.progressbar.grid(column=0, columnspan=4, sticky=W + E + N + S) self.progressbar.grid(column=0, columnspan=4, sticky=W + E + N + S)
# self.debug = Listbox(self.master) self.notelabel = Label(self.master,
# self.debug.grid(row=9,columnspan=4,sticky=W+E+N+S) text="GUI can seem frozen while converting, kindly wait until some message appears!")
# self.debug.insert(END, os.environ['PATH']) self.notelabel.grid(column=0, columnspan=4, sticky=W + E + N + S)
def start_conversion(self): def start_conversion(self):
self.progressbar.start() self.progressbar.start()
@@ -120,7 +126,6 @@ class MainWindow:
if len(self.filelist) < 1: if len(self.filelist) < 1:
tkMessageBox.showwarning('No file selected', "You should really select some files to convert...") tkMessageBox.showwarning('No file selected', "You should really select some files to convert...")
return return
tkMessageBox.showinfo('Starting conversion', "KCC will now start converting files. GUI can seem frozen, kindly wait until some message appears!")
profilekey = ProfileData.ProfileLabels[self.profile.get()] profilekey = ProfileData.ProfileLabels[self.profile.get()]
argv = ["-p", profilekey] argv = ["-p", profilekey]
if self.options['image_preprocess'].get() == 0: if self.options['image_preprocess'].get() == 0:
@@ -138,8 +143,8 @@ class MainWindow:
errors = False errors = False
for entry in self.filelist: for entry in self.filelist:
self.master.update() self.master.update()
try:
subargv = list(argv) subargv = list(argv)
try:
subargv.append(entry) subargv.append(entry)
epub_path = comic2ebook.main(subargv) epub_path = comic2ebook.main(subargv)
except Exception, err: except Exception, err:
@@ -147,7 +152,7 @@ class MainWindow:
errors = True errors = True
continue continue
if self.options['epub_only'] == 1: if self.options['epub_only'] == 1:
continue; continue
try: try:
retcode = call("kindlegen \"" + epub_path + "\"", shell=True) retcode = call("kindlegen \"" + epub_path + "\"", shell=True)
if retcode < 0: if retcode < 0:
@@ -158,8 +163,8 @@ class MainWindow:
tkMessageBox.showerror('Error kindlegen', "Error on file %s:\n%s" % (epub_path, e)) tkMessageBox.showerror('Error kindlegen', "Error on file %s:\n%s" % (epub_path, e))
errors = True errors = True
continue continue
try:
mobifile = epub_path.replace('.epub', '.mobi') mobifile = epub_path.replace('.epub', '.mobi')
try:
shutil.move(mobifile, mobifile + '_tostrip') shutil.move(mobifile, mobifile + '_tostrip')
kindlestrip.main((mobifile + '_tostrip', mobifile)) kindlestrip.main((mobifile + '_tostrip', mobifile))
os.remove(mobifile + '_tostrip') os.remove(mobifile + '_tostrip')
@@ -178,7 +183,7 @@ class MainWindow:
"Conversion successfully done!" "Conversion successfully done!"
) )
def remove_readonly(self, fn, path, excinfo): def remove_readonly(self, fn, path):
if fn is os.rmdir: if fn is os.rmdir:
os.chmod(path, stat.S_IWRITE) os.chmod(path, stat.S_IWRITE)
os.rmdir(path) os.rmdir(path)
@@ -191,5 +196,3 @@ class MainWindow:
self.master = master self.master = master
self.master.title(title) self.master.title(title)
self.initialize() self.initialize()

View File

@@ -22,6 +22,7 @@ __docformat__ = 'restructuredtext en'
import os import os
from PIL import Image, ImageOps, ImageDraw, ImageStat from PIL import Image, ImageOps, ImageDraw, ImageStat
class ImageFlags: class ImageFlags:
Orient = 1 << 0 Orient = 1 << 0
Resize = 1 << 1 Resize = 1 << 1
@@ -95,6 +96,7 @@ class ProfileData:
"Kindle DXG": 'KDXG' "Kindle DXG": 'KDXG'
} }
class ComicPage: class ComicPage:
def __init__(self, source, device): def __init__(self, source, device):
try: try:
@@ -122,7 +124,7 @@ class ComicPage:
def quantizeImage(self): def quantizeImage(self):
colors = len(self.palette) / 3 colors = len(self.palette) / 3
if colors < 256: if colors < 256:
self.palette = self.palette + self.palette[:3] * (256 - colors) self.palette += self.palette[:3] * (256 - colors)
palImg = Image.new('P', (1, 1)) palImg = Image.new('P', (1, 1))
palImg.putpalette(self.palette) palImg.putpalette(self.palette)
self.image = self.image.quantize(palette=palImg) self.image = self.image.quantize(palette=palImg)
@@ -213,7 +215,6 @@ class ComicPage:
draw.rectangle([corner1, corner2], outline=foreground) draw.rectangle([corner1, corner2], outline=foreground)
self.image = imageBg self.image = imageBg
def cutPageNumber(self): def cutPageNumber(self):
widthImg, heightImg = self.image.size widthImg, heightImg = self.image.size
delta = 2 delta = 2
@@ -238,26 +239,28 @@ class ComicPage:
pageNumberCut2 = diff pageNumberCut2 = diff
diff += delta diff += delta
oldStat = ImageStat.Stat(self.image.crop((0, heightImg - diff, widthImg, heightImg - pageNumberCut2))).var[0] oldStat = ImageStat.Stat(self.image.crop((0, heightImg - diff, widthImg, heightImg - pageNumberCut2))).var[0]
while ImageStat.Stat(self.image.crop((0,heightImg-diff,widthImg,heightImg-pageNumberCut2))).var[0] < fixedThreshold+oldStat\ while ImageStat.Stat(self.image.crop((0, heightImg - diff, widthImg, heightImg - pageNumberCut2))).var[0]\
and diff < heightImg/4: < fixedThreshold + oldStat and diff < heightImg / 4:
diff += delta diff += delta
diff -= delta diff -= delta
pageNumberCut3 = diff pageNumberCut3 = diff
delta = 5 delta = 5
diff = delta diff = delta
while ImageStat.Stat(self.image.crop((0,heightImg-pageNumberCut2,diff,heightImg))).var[0] < fixedThreshold and diff < widthImg: while ImageStat.Stat(self.image.crop((0, heightImg - pageNumberCut2, diff, heightImg))).var[0] < fixedThreshold\
and diff < widthImg:
diff += delta diff += delta
diff -= delta diff -= delta
pageNumberX1 = diff pageNumberX1 = diff
diff = delta diff = delta
while ImageStat.Stat(self.image.crop((widthImg-diff,heightImg-pageNumberCut2,widthImg,heightImg))).var[0] < fixedThreshold and diff < widthImg: while ImageStat.Stat(self.image.crop((widthImg - diff, heightImg - pageNumberCut2,
widthImg, heightImg))).var[0] < fixedThreshold and diff < widthImg:
diff += delta diff += delta
diff -= delta diff -= delta
pageNumberX2 = widthImg - diff pageNumberX2 = widthImg - diff
if pageNumberCut3 - pageNumberCut1 > 2 * delta\ if pageNumberCut3 - pageNumberCut1 > 2 * delta\
and float(pageNumberX2 - pageNumberX1) / float(pageNumberCut2 - pageNumberCut1) <= 9.0\ and float(pageNumberX2 - pageNumberX1) / float(pageNumberCut2 - pageNumberCut1) <= 9.0\
and ImageStat.Stat(self.image.crop((0,heightImg-pageNumberCut3,widthImg,heightImg))).var[0] / ImageStat.Stat(self.image).var[0] < 0.1\ and ImageStat.Stat(self.image.crop((0, heightImg - pageNumberCut3, widthImg, heightImg))).var[0]\
/ ImageStat.Stat(self.image).var[0] < 0.1\
and pageNumberCut3 < heightImg / 4 - delta: and pageNumberCut3 < heightImg / 4 - delta:
diff = pageNumberCut3 diff = pageNumberCut3
else: else:
@@ -323,15 +326,18 @@ class ComicPage:
#Black rectangle #Black rectangle
draw.rectangle([(0, heightImg - 3), (widthImg, heightImg)], outline=black, fill=black) draw.rectangle([(0, heightImg - 3), (widthImg, heightImg)], outline=black, fill=black)
#White rectangle #White rectangle
draw.rectangle([(widthImg*file_number/files_totalnumber,heightImg-3), (widthImg-1,heightImg)], outline=black, fill=white) draw.rectangle([(widthImg * file_number / files_totalnumber, heightImg - 3), (widthImg - 1, heightImg)],
outline=black, fill=white)
#Making notches #Making notches
for i in range(1, 10): for i in range(1, 10):
if i <= (10 * file_number / files_totalnumber): if i <= (10 * file_number / files_totalnumber):
notch_colour = white # White notch_colour = white # White
else: else:
notch_colour = black # Black notch_colour = black # Black
draw.line([(widthImg*float(i)/10,heightImg-3), (widthImg*float(i)/10,heightImg)],fill=notch_colour) draw.line([(widthImg * float(i) / 10, heightImg - 3), (widthImg * float(i) / 10, heightImg)],
fill=notch_colour)
#The 50% #The 50%
if i == 5: if i == 5:
draw.rectangle([(widthImg/2-1,heightImg-5), (widthImg/2+1,heightImg)],outline=black,fill=notch_colour) draw.rectangle([(widthImg / 2 - 1, heightImg - 5), (widthImg / 2 + 1, heightImg)],
outline=black, fill=notch_colour)
return self.image return self.image