mirror of
https://github.com/ciromattia/kcc
synced 2025-12-15 10:46:40 +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:
@@ -20,6 +20,7 @@ __docformat__ = 'restructuredtext en'
|
||||
|
||||
import os
|
||||
|
||||
|
||||
class CBxArchive:
|
||||
def __init__(self, origFileName):
|
||||
self.cbxexts = ['.zip', '.cbz', '.rar', '.cbr']
|
||||
@@ -69,10 +70,10 @@ class CBxArchive:
|
||||
self.extractCBR(targetdir)
|
||||
elif '.cbz' == self.filename[1].lower() or '.zip' == self.filename[1].lower():
|
||||
self.extractCBZ(targetdir)
|
||||
dir = os.listdir(targetdir)
|
||||
if len(dir) == 1 and os.path.isdir(os.path.join(targetdir,dir[0])):
|
||||
adir = os.listdir(targetdir)
|
||||
if len(adir) == 1 and os.path.isdir(os.path.join(targetdir, adir[0])):
|
||||
import shutil
|
||||
for f in os.listdir(os.path.join(targetdir,dir[0])):
|
||||
shutil.move(os.path.join(targetdir,dir[0],f),targetdir)
|
||||
os.rmdir(os.path.join(targetdir,dir[0]))
|
||||
for f in os.listdir(os.path.join(targetdir, adir[0])):
|
||||
shutil.move(os.path.join(targetdir, adir[0], f), targetdir)
|
||||
os.rmdir(os.path.join(targetdir, adir[0]))
|
||||
return targetdir
|
||||
|
||||
35
kcc/gui.py
35
kcc/gui.py
@@ -21,11 +21,17 @@ __copyright__ = '2012-2013, Ciro Mattia Gonano <ciromattia@gmail.com>'
|
||||
__docformat__ = 'restructuredtext en'
|
||||
|
||||
from Tkinter import *
|
||||
import tkFileDialog, tkMessageBox, ttk
|
||||
import comic2ebook, kindlestrip
|
||||
import tkFileDialog
|
||||
import tkMessageBox
|
||||
import ttk
|
||||
import comic2ebook
|
||||
import kindlestrip
|
||||
from image import ProfileData
|
||||
from subprocess import call, Popen, PIPE, STDOUT
|
||||
import os, shutil, stat
|
||||
from subprocess import call
|
||||
import os
|
||||
import shutil
|
||||
import stat
|
||||
|
||||
|
||||
class MainWindow:
|
||||
|
||||
@@ -58,8 +64,8 @@ class MainWindow:
|
||||
def refresh_list(self):
|
||||
self.filelocation.config(state=NORMAL)
|
||||
self.filelocation.delete(0, END)
|
||||
for file in self.filelist:
|
||||
self.filelocation.insert(END, file)
|
||||
for afile in self.filelist:
|
||||
self.filelocation.insert(END, afile)
|
||||
self.filelocation.config(state=DISABLED)
|
||||
|
||||
def initialize(self):
|
||||
@@ -107,9 +113,9 @@ class MainWindow:
|
||||
self.submit.grid(column=3)
|
||||
self.progressbar.grid(column=0, columnspan=4, sticky=W + E + N + S)
|
||||
|
||||
# self.debug = Listbox(self.master)
|
||||
# self.debug.grid(row=9,columnspan=4,sticky=W+E+N+S)
|
||||
# self.debug.insert(END, os.environ['PATH'])
|
||||
self.notelabel = Label(self.master,
|
||||
text="GUI can seem frozen while converting, kindly wait until some message appears!")
|
||||
self.notelabel.grid(column=0, columnspan=4, sticky=W + E + N + S)
|
||||
|
||||
def start_conversion(self):
|
||||
self.progressbar.start()
|
||||
@@ -120,7 +126,6 @@ class MainWindow:
|
||||
if len(self.filelist) < 1:
|
||||
tkMessageBox.showwarning('No file selected', "You should really select some files to convert...")
|
||||
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()]
|
||||
argv = ["-p", profilekey]
|
||||
if self.options['image_preprocess'].get() == 0:
|
||||
@@ -138,8 +143,8 @@ class MainWindow:
|
||||
errors = False
|
||||
for entry in self.filelist:
|
||||
self.master.update()
|
||||
try:
|
||||
subargv = list(argv)
|
||||
try:
|
||||
subargv.append(entry)
|
||||
epub_path = comic2ebook.main(subargv)
|
||||
except Exception, err:
|
||||
@@ -147,7 +152,7 @@ class MainWindow:
|
||||
errors = True
|
||||
continue
|
||||
if self.options['epub_only'] == 1:
|
||||
continue;
|
||||
continue
|
||||
try:
|
||||
retcode = call("kindlegen \"" + epub_path + "\"", shell=True)
|
||||
if retcode < 0:
|
||||
@@ -158,8 +163,8 @@ class MainWindow:
|
||||
tkMessageBox.showerror('Error kindlegen', "Error on file %s:\n%s" % (epub_path, e))
|
||||
errors = True
|
||||
continue
|
||||
try:
|
||||
mobifile = epub_path.replace('.epub', '.mobi')
|
||||
try:
|
||||
shutil.move(mobifile, mobifile + '_tostrip')
|
||||
kindlestrip.main((mobifile + '_tostrip', mobifile))
|
||||
os.remove(mobifile + '_tostrip')
|
||||
@@ -178,7 +183,7 @@ class MainWindow:
|
||||
"Conversion successfully done!"
|
||||
)
|
||||
|
||||
def remove_readonly(self, fn, path, excinfo):
|
||||
def remove_readonly(self, fn, path):
|
||||
if fn is os.rmdir:
|
||||
os.chmod(path, stat.S_IWRITE)
|
||||
os.rmdir(path)
|
||||
@@ -191,5 +196,3 @@ class MainWindow:
|
||||
self.master = master
|
||||
self.master.title(title)
|
||||
self.initialize()
|
||||
|
||||
|
||||
|
||||
28
kcc/image.py
28
kcc/image.py
@@ -22,6 +22,7 @@ __docformat__ = 'restructuredtext en'
|
||||
import os
|
||||
from PIL import Image, ImageOps, ImageDraw, ImageStat
|
||||
|
||||
|
||||
class ImageFlags:
|
||||
Orient = 1 << 0
|
||||
Resize = 1 << 1
|
||||
@@ -95,6 +96,7 @@ class ProfileData:
|
||||
"Kindle DXG": 'KDXG'
|
||||
}
|
||||
|
||||
|
||||
class ComicPage:
|
||||
def __init__(self, source, device):
|
||||
try:
|
||||
@@ -122,7 +124,7 @@ class ComicPage:
|
||||
def quantizeImage(self):
|
||||
colors = len(self.palette) / 3
|
||||
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.putpalette(self.palette)
|
||||
self.image = self.image.quantize(palette=palImg)
|
||||
@@ -213,7 +215,6 @@ class ComicPage:
|
||||
draw.rectangle([corner1, corner2], outline=foreground)
|
||||
self.image = imageBg
|
||||
|
||||
|
||||
def cutPageNumber(self):
|
||||
widthImg, heightImg = self.image.size
|
||||
delta = 2
|
||||
@@ -238,26 +239,28 @@ class ComicPage:
|
||||
pageNumberCut2 = diff
|
||||
diff += delta
|
||||
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\
|
||||
and diff < heightImg/4:
|
||||
while ImageStat.Stat(self.image.crop((0, heightImg - diff, widthImg, heightImg - pageNumberCut2))).var[0]\
|
||||
< fixedThreshold + oldStat and diff < heightImg / 4:
|
||||
diff += delta
|
||||
diff -= delta
|
||||
pageNumberCut3 = diff
|
||||
delta = 5
|
||||
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
|
||||
pageNumberX1 = diff
|
||||
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
|
||||
pageNumberX2 = widthImg - diff
|
||||
|
||||
if pageNumberCut3 - pageNumberCut1 > 2 * delta\
|
||||
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:
|
||||
diff = pageNumberCut3
|
||||
else:
|
||||
@@ -323,15 +326,18 @@ class ComicPage:
|
||||
#Black rectangle
|
||||
draw.rectangle([(0, heightImg - 3), (widthImg, heightImg)], outline=black, fill=black)
|
||||
#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
|
||||
for i in range(1, 10):
|
||||
if i <= (10 * file_number / files_totalnumber):
|
||||
notch_colour = white # White
|
||||
else:
|
||||
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%
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user