1
0
mirror of https://github.com/ciromattia/kcc synced 2025-12-13 17:56:30 +00:00

Update droplet

This commit is contained in:
Ciro Mattia Gonano
2013-01-18 11:58:14 +01:00
parent 9ee851d04b
commit e4847e6cf4
2 changed files with 237 additions and 93 deletions

View File

@@ -26,6 +26,9 @@
# WARNING: PIL is required for all image mangling! # WARNING: PIL is required for all image mangling!
# 1.30 - Fixed an issue in OPF generation for device resolution # 1.30 - Fixed an issue in OPF generation for device resolution
# Reworked options system (call with -h option to get the inline help) # Reworked options system (call with -h option to get the inline help)
# 1.40 - Added some options for controlling image optimization
# Further optimization (ImageOps, page numbering cut, autocontrast)
# 1.41 - Fixed a serious bug on resizing when img ratio was bigger than device one
# #
# Todo: # Todo:
# - Add gracefully exit for CBR if no rarfile.py and no unrar # - Add gracefully exit for CBR if no rarfile.py and no unrar
@@ -48,9 +51,9 @@ class HTMLbuilder:
def __init__(self, dstdir, file): def __init__(self, dstdir, file):
self.file = file self.file = file
filename = getImageFileName(file) filename = getImageFileName(file)
if (filename != None): if filename is not None:
htmlfile = dstdir + '/' + filename[0] + '.html' htmlfile = dstdir + '/' + filename[0] + '.html'
f = open(htmlfile, "w"); f = open(htmlfile, "w")
f.writelines(["<!DOCTYPE html SYSTEM \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n", f.writelines(["<!DOCTYPE html SYSTEM \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n",
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n", "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n",
"<head>\n", "<head>\n",
@@ -63,12 +66,12 @@ class HTMLbuilder:
"</html>" "</html>"
]) ])
f.close() f.close()
return None return
class NCXbuilder: class NCXbuilder:
def __init__(self, dstdir, title): def __init__(self, dstdir, title):
ncxfile = dstdir + '/content.ncx' ncxfile = dstdir + '/content.ncx'
f = open(ncxfile, "w"); f = open(ncxfile, "w")
f.writelines(["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", f.writelines(["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
"<!DOCTYPE ncx PUBLIC \"-//NISO//DTD ncx 2005-1//EN\" \"http://www.daisy.org/z3986/2005/ncx-2005-1.dtd\">\n", "<!DOCTYPE ncx PUBLIC \"-//NISO//DTD ncx 2005-1//EN\" \"http://www.daisy.org/z3986/2005/ncx-2005-1.dtd\">\n",
"<ncx version=\"2005-1\" xml:lang=\"en-US\" xmlns=\"http://www.daisy.org/z3986/2005/ncx/\">\n", "<ncx version=\"2005-1\" xml:lang=\"en-US\" xmlns=\"http://www.daisy.org/z3986/2005/ncx/\">\n",
@@ -83,9 +86,9 @@ class OPFBuilder:
def __init__(self, profile, dstdir, title, filelist): def __init__(self, profile, dstdir, title, filelist):
opffile = dstdir + '/content.opf' opffile = dstdir + '/content.opf'
# read the first file resolution # read the first file resolution
deviceres, palette = image.ProfileData.Profiles[profile] profilelabel, deviceres, palette = image.ProfileData.Profiles[profile]
imgres = str(deviceres[0]) + "x" + str(deviceres[1]) imgres = str(deviceres[0]) + "x" + str(deviceres[1])
f = open(opffile, "w"); f = open(opffile, "w")
f.writelines(["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", f.writelines(["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",
"<package version=\"2.0\" unique-identifier=\"PrimaryID\" xmlns=\"http://www.idpf.org/2007/opf\">\n", "<package version=\"2.0\" unique-identifier=\"PrimaryID\" xmlns=\"http://www.idpf.org/2007/opf\">\n",
"<metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:opf=\"http://www.idpf.org/2007/opf\">\n", "<metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:opf=\"http://www.idpf.org/2007/opf\">\n",
@@ -101,10 +104,10 @@ class OPFBuilder:
for filename in filelist: for filename in filelist:
f.write("<item id=\"page_" + filename[0] + "\" href=\"" + filename[0] + ".html\" media-type=\"application/xhtml+xml\"/>\n") f.write("<item id=\"page_" + filename[0] + "\" href=\"" + filename[0] + ".html\" media-type=\"application/xhtml+xml\"/>\n")
for filename in filelist: for filename in filelist:
if ('.png' == filename[1]): if '.png' == filename[1]:
mt = 'image/png'; mt = 'image/png'
else: else:
mt = 'image/jpeg'; mt = 'image/jpeg'
f.write("<item id=\"img_" + filename[0] + "\" href=\"" + filename[0] + filename[1] + "\" media-type=\"" + mt + "\"/>\n") f.write("<item id=\"img_" + filename[0] + "\" href=\"" + filename[0] + filename[1] + "\" media-type=\"" + mt + "\"/>\n")
f.write("</manifest>\n<spine toc=\"ncx\">\n") f.write("</manifest>\n<spine toc=\"ncx\">\n")
for filename in filelist: for filename in filelist:
@@ -115,7 +118,7 @@ class OPFBuilder:
def getImageFileName(file): def getImageFileName(file):
filename = os.path.splitext(file) filename = os.path.splitext(file)
if (filename[0].startswith('.') or (filename[1].lower() != '.png' and filename[1].lower() != '.jpg' and filename[1].lower() != '.jpeg')): if filename[0].startswith('.') or (filename[1].lower() != '.png' and filename[1].lower() != '.jpg' and filename[1].lower() != '.jpeg'):
return None return None
return filename return filename
@@ -134,9 +137,6 @@ def Copyright():
def Usage(): def Usage():
print "Generates HTML, NCX and OPF for a Comic ebook from a bunch of images" print "Generates HTML, NCX and OPF for a Comic ebook from a bunch of images"
print "Optimized for creating Mobipockets to be read into Kindle Paperwhite" print "Optimized for creating Mobipockets to be read into Kindle Paperwhite"
#print "Usage:"
#print " %s <profile> <dir> <title>" % sys.argv[0]
#print " <title> is optional"
parser.print_help() parser.print_help()
def main(argv=None): def main(argv=None):
@@ -144,18 +144,28 @@ def main(argv=None):
usage = "Usage: %prog [options] comic_file|comic_folder" usage = "Usage: %prog [options] comic_file|comic_folder"
parser = OptionParser(usage=usage, version=__version__) parser = OptionParser(usage=usage, version=__version__)
parser.add_option("-p", "--profile", action="store", dest="profile", default="KHD", parser.add_option("-p", "--profile", action="store", dest="profile", default="KHD",
help="Device profile (choose one among K1, K2, K3, K4, KHD [default])") help="Device profile (choose one among K1, K2, K3, K4, KDX, KDXG or KHD) [default=KHD]")
parser.add_option("-t", "--title", action="store", dest="title", default="comic", parser.add_option("-t", "--title", action="store", dest="title", default="defaulttitle",
help="Comic title") help="Comic title [default=filename]")
parser.add_option("-m", "--manga-style", action="store_true", dest="righttoleft", default=False, parser.add_option("-m", "--manga-style", action="store_true", dest="righttoleft", default=False,
help="Split pages 'manga style' (right-to-left reading)") help="Split pages 'manga style' (right-to-left reading) [default=False]")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
help="Verbose output [default=False]")
parser.add_option("-i", "--image-processing", action="store_false", dest="imgproc", default=True,
help="Apply image preprocessing (page splitting and optimizations) [default=True]")
parser.add_option("--upscale-images", action="store_true", dest="upscale", default=False,
help="Resize images smaller than device's resolution [default=False]")
parser.add_option("--stretch-images", action="store_true", dest="stretch", default=False,
help="Stretch images to device's resolution [default=False]")
parser.add_option("--cut-page-numbers", action="store_false", dest="cutpagenumbers", default=True,
help="Try to cut page numbering on images [default=True]")
options, args = parser.parse_args(argv) options, args = parser.parse_args(argv)
if len(args) != 1: if len(args) != 1:
parser.print_help() parser.print_help()
return return
dir = args[0] dir = args[0]
fname = os.path.splitext(dir) fname = os.path.splitext(dir)
if (fname[1].lower() == '.pdf'): if fname[1].lower() == '.pdf':
pdf = pdfjpgextract.PdfJpgExtract(dir) pdf = pdfjpgextract.PdfJpgExtract(dir)
pdf.extract() pdf.extract()
dir = pdf.getPath() dir = pdf.getPath()
@@ -164,38 +174,59 @@ def main(argv=None):
if cbx.isCbxFile(): if cbx.isCbxFile():
cbx.extract() cbx.extract()
dir = cbx.getPath() dir = cbx.getPath()
else:
try:
import shutil
shutil.copytree(dir, dir + "_orig")
#dir = dir + "_orig"
except OSError as exc:
raise
filelist = [] filelist = []
try: if options.imgproc:
print "Splitting double pages..." print "Processing images..."
for file in os.listdir(dir): try:
if (getImageFileName(file) != None): if options.verbose:
img = image.ComicPage(dir+'/'+file, options.profile) print "Splitting double pages..."
img.splitPage(dir, options.righttoleft) for file in os.listdir(dir):
for file in os.listdir(dir): if getImageFileName(file) is not None:
if (getImageFileName(file) != None): print ".",
print "Optimizing " + file + " for " + options.profile img = image.ComicPage(dir+'/'+file, options.profile)
img = image.ComicPage(dir+'/'+file, options.profile) img.splitPage(dir, options.righttoleft)
img.resizeImage() for file in os.listdir(dir):
#img.frameImage() if getImageFileName(file) is not None:
img.quantizeImage() if options.verbose:
img.saveToDir(dir) print "Optimizing " + file + " for " + options.profile
except ImportError: else:
print "Could not load PIL, not optimizing image" print ".",
img = image.ComicPage(dir+'/'+file, options.profile)
img.optimizeImage()
img.cropWhiteSpace(10.0)
if options.cutpagenumbers:
img.cutPageNumber()
img.resizeImage(options.upscale,options.stretch)
img.quantizeImage()
img.saveToDir(dir)
except ImportError:
print "Could not load PIL, not optimizing image"
print "Creating ePub structure..."
for file in os.listdir(dir): for file in os.listdir(dir):
if (getImageFileName(file) != None and isInFilelist(file,filelist) == False): if getImageFileName(file) is not None and isInFilelist(file,filelist) == False:
# put credits at the end # put credits at the end
if "credits" in file.lower(): if "credits" in file.lower():
os.rename(dir+'/'+file, dir+'/ZZZ999_'+file) os.rename(dir+'/'+file, dir+'/ZZZ999_'+file)
file = 'ZZZ999_'+file file = 'ZZZ999_'+file
filename = HTMLbuilder(dir,file).getResult() filename = HTMLbuilder(dir,file).getResult()
if (filename != None): if filename is not None:
filelist.append(filename) filelist.append(filename)
if options.title == 'defaulttitle':
options.title = os.path.basename(dir)
NCXbuilder(dir,options.title) NCXbuilder(dir,options.title)
# ensure we're sorting files alphabetically # ensure we're sorting files alphabetically
filelist = sorted(filelist, key=lambda name: name[0]) filelist = sorted(filelist, key=lambda name: name[0].lower())
OPFBuilder(options.profile,dir,options.title,filelist) OPFBuilder(options.profile,dir,options.title,filelist)
if __name__ == "__main__": if __name__ == "__main__":
Copyright() Copyright()
main(sys.argv[1:]) main(sys.argv[1:])

View File

@@ -1,4 +1,6 @@
# Copyright (C) 2010 Alex Yatskov # Copyright (C) 2010 Alex Yatskov
# Copyright (C) 2011 Stanislav (proDOOMman) Kosolapov <prodoomman@gmail.com>
# Copyright (C) 2012-2013 Ciro Mattia Gonano <ciromattia@gmail.com>
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
@@ -14,7 +16,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import os import os
from PIL import Image, ImageDraw from PIL import Image, ImageOps, ImageDraw, ImageStat
class ImageFlags: class ImageFlags:
Orient = 1 << 0 Orient = 1 << 0
@@ -32,7 +34,7 @@ class ProfileData:
0xff, 0xff, 0xff 0xff, 0xff, 0xff
] ]
Palette15a = [ Palette15 = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
@@ -50,13 +52,14 @@ class ProfileData:
0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
] ]
Palette15b = [ Palette16 = [
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
0x66, 0x66, 0x66,
0x77, 0x77, 0x77, 0x77, 0x77, 0x77,
0x88, 0x88, 0x88, 0x88, 0x88, 0x88,
0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
@@ -69,18 +72,19 @@ class ProfileData:
] ]
Profiles = { Profiles = {
'K1': ((600, 800), Palette4), 'K1': ("Kindle", (600, 800), Palette4),
'K2': ((600, 800), Palette15a), 'K2': ("Kindle 2", (600, 800), Palette15),
'K3': ((600, 800), Palette15a), 'K3': ("Kindle 3/Keyboard", (600, 800), Palette16),
'K4': ((600, 800), Palette15b), 'K4': ("Kindle 4/NT/Touch", (600, 800), Palette16),
'KHD': ((758, 1024), Palette15b), 'KHD': ("Kindle Paperwhite", (758, 1024), Palette16),
'KDX': ((824, 1200), Palette15a) 'KDX': ("Kindle DX", (824, 1200), Palette15),
'KDXG': ("Kindle DXG", (824, 1200), Palette16)
} }
class ComicPage: class ComicPage:
def __init__(self,source,device): def __init__(self,source,device):
try: try:
self.size, self.palette = ProfileData.Profiles[device] self.profile_label, self.size, self.palette = ProfileData.Profiles[device]
except KeyError: except KeyError:
raise RuntimeError('Unexpected output device %s' % device) raise RuntimeError('Unexpected output device %s' % device)
try: try:
@@ -92,56 +96,57 @@ class ComicPage:
def saveToDir(self,targetdir): def saveToDir(self,targetdir):
filename = os.path.basename(self.origFileName) filename = os.path.basename(self.origFileName)
print "Saving to " + targetdir + '/' + filename #print "Saving to " + targetdir + '/' + filename
try: try:
self.image = self.image.convert('L') # convert to grayscale self.image = self.image.convert('L') # convert to grayscale
self.image.save(targetdir + '/' + filename,"JPEG") self.image.save(targetdir + '/' + filename,"JPEG")
except IOError as e: except IOError as e:
raise RuntimeError('Cannot write image in directory %s: %s' %(targetdir,e)) raise RuntimeError('Cannot write image in directory %s: %s' %(targetdir,e))
def optimizeImage(self):
self.image = ImageOps.autocontrast(self.image)
def quantizeImage(self): def quantizeImage(self):
colors = len(self.palette) / 3 colors = len(self.palette) / 3
if colors < 256: if colors < 256:
palette = self.palette + self.palette[:3] * (256 - colors) self.palette = self.palette + self.palette[:3] * (256 - colors)
palImg = Image.new('P', (1, 1)) palImg = Image.new('P', (1, 1))
palImg.putpalette(palette) palImg.putpalette(self.palette)
self.image = self.image.quantize(palette=palImg) self.image = self.image.quantize(palette=palImg)
def stretchImage(self): def resizeImage(self,upscale=False, stretch=False):
widthDev, heightDev = self.size method = Image.ANTIALIAS
self.image = self.image.resize((widthDev, heightDev), Image.ANTIALIAS) if self.image.size[0] <= self.size[0] and self.image.size[1] <= self.size[1]:
if not upscale:
return self.image
else:
method = Image.NEAREST
def resizeImage(self): if stretch: # if stretching call directly resize() without other considerations.
widthDev, heightDev = self.size self.image = self.image.resize(self.size,method)
widthImg, heightImg = self.image.size
if widthImg <= widthDev and heightImg <= heightDev:
return self.image return self.image
ratioImg = float(widthImg) / float(heightImg)
ratioWidth = float(widthImg) / float(widthDev)
ratioHeight = float(heightImg) / float(heightDev)
if ratioWidth > ratioHeight:
widthImg = widthDev
heightImg = int(widthDev / ratioImg)
elif ratioWidth < ratioHeight:
heightImg = heightDev
widthImg = int(heightDev * ratioImg)
else:
widthImg, heightImg = self.size
self.image = self.image.resize((widthImg, heightImg), Image.ANTIALIAS)
def orientImage(self): ratioDev = float(self.size[0]) / float(self.size[1])
widthDev, heightDev = self.size if (float(self.image.size[0]) / float(self.image.size[1])) < ratioDev:
widthImg, heightImg = self.image.size diff = int(self.image.size[1] * ratioDev) - self.image.size[0]
if (widthImg > heightImg) != (widthDev > heightDev): newImage = Image.new('RGB', (self.image.size[0] + diff, self.image.size[1]), (255,255,255))
self.image = self.image.rotate(90, Image.BICUBIC, True) newImage.paste(self.image, (diff / 2, 0, diff / 2 + self.image.size[0], self.image.size[1]))
self.image = newImage
elif (float(self.image.size[0]) / float(self.image.size[1])) > ratioDev:
diff = int(self.image.size[0] / ratioDev) - self.image.size[1]
newImage = Image.new('RGB', (self.image.size[0], self.image.size[1] + diff), (255,255,255))
newImage.paste(self.image, (0, diff / 2, self.image.size[0], diff / 2 + self.image.size[1]))
self.image = newImage
self.image = ImageOps.fit(self.image, self.size, method = method, centering = (0.5,0.5))
return self.image
def splitPage(self, targetdir, righttoleft=False): def splitPage(self, targetdir, righttoleft=False):
width, height = self.image.size width, height = self.image.size
dstwidth, dstheight = self.size dstwidth, dstheight = self.size
print "Image is %d x %d" % (width,height) #print "Image is %d x %d" % (width,height)
# only split if origin is not oriented the same as target # only split if origin is not oriented the same as target
if (width > height) != (dstwidth > dstheight): if (width > height) != (dstwidth > dstheight):
if (width > height): if width > height:
# source is landscape, so split by the width # source is landscape, so split by the width
leftbox = (0, 0, width/2, height) leftbox = (0, 0, width/2, height)
rightbox = (width/2, 0, width, height) rightbox = (width/2, 0, width, height)
@@ -153,7 +158,7 @@ class ComicPage:
fileone = targetdir + '/' + filename[0] + '-1' + filename[1] fileone = targetdir + '/' + filename[0] + '-1' + filename[1]
filetwo = targetdir + '/' + filename[0] + '-2' + filename[1] filetwo = targetdir + '/' + filename[0] + '-2' + filename[1]
try: try:
if (righttoleft == True): if righttoleft:
pageone = self.image.crop(rightbox) pageone = self.image.crop(rightbox)
pagetwo = self.image.crop(leftbox) pagetwo = self.image.crop(leftbox)
else: else:
@@ -164,7 +169,7 @@ class ComicPage:
os.remove(self.origFileName) os.remove(self.origFileName)
except IOError as e: except IOError as e:
raise RuntimeError('Cannot write image in directory %s: %s' %(targetdir,e)) raise RuntimeError('Cannot write image in directory %s: %s' %(targetdir,e))
return (fileone,filetwo) return fileone,filetwo
return None return None
def frameImage(self): def frameImage(self):
@@ -190,18 +195,126 @@ class ComicPage:
draw.rectangle([corner1, corner2], outline=foreground) draw.rectangle([corner1, corner2], outline=foreground)
self.image = imageBg self.image = imageBg
# for debug purposes (this file is not meant to be called directly
if __name__ == "__main__": def cutPageNumber(self):
import sys widthImg, heightImg = self.image.size
imgfile = sys.argv[1] delta = 2
img = ComicPage(imgfile, "KHD") diff = delta
pages = img.splitPage('temp/',False) fixedThreshold = 5
if (pages != None): if ImageStat.Stat(self.image).var[0] < 2*fixedThreshold:
print "%s, %s" % pages return self.image
sys.exit(0) while ImageStat.Stat(self.image.crop((0,heightImg-diff,widthImg,heightImg))).var[0] < fixedThreshold\
img.orientImage() and diff < heightImg:
img.resizeImage() diff += delta
img.frameImage() diff -= delta
img.quantizeImage() pageNumberCut1 = diff
img.saveToDir("temp/") if diff<delta:
sys.exit(0) diff=delta
oldStat=ImageStat.Stat(self.image.crop((0,heightImg-diff,widthImg,heightImg))).var[0]
diff += delta
while ImageStat.Stat(self.image.crop((0,heightImg-diff,widthImg,heightImg))).var[0] - oldStat > 0\
and diff < heightImg/4:
oldStat=ImageStat.Stat(self.image.crop((0,heightImg-diff,widthImg,heightImg))).var[0]
diff += delta
diff -= delta
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:
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:
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:
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 pageNumberCut3 < heightImg/4-delta:
diff=pageNumberCut3
else:
diff=pageNumberCut1
self.image = self.image.crop((0,0,widthImg,heightImg-diff))
return self.image
def cropWhiteSpace(self, threshold):
widthImg, heightImg = self.image.size
delta = 10
diff = delta
# top
while ImageStat.Stat(self.image.crop((0,0,widthImg,diff))).var[0] < threshold and diff < heightImg:
diff += delta
diff -= delta
# print "Top crop: %s"%diff
self.image = self.image.crop((0,diff,widthImg,heightImg))
widthImg, heightImg = self.image.size
diff = delta
# left
while ImageStat.Stat(self.image.crop((0,0,diff,heightImg))).var[0] < threshold and diff < widthImg:
diff += delta
diff -= delta
# print "Left crop: %s"%diff
self.image = self.image.crop((diff,0,widthImg,heightImg))
widthImg, heightImg = self.image.size
diff = delta
# down
while ImageStat.Stat(self.image.crop((0,heightImg-diff,widthImg,heightImg))).var[0] < threshold\
and diff < heightImg:
diff += delta
diff -= delta
# print "Down crop: %s"%diff
self.image = self.image.crop((0,0,widthImg,heightImg-diff))
widthImg, heightImg = self.image.size
diff = delta
# right
while ImageStat.Stat(self.image.crop((widthImg-diff,0,widthImg,heightImg))).var[0] < threshold\
and diff < widthImg:
diff += delta
diff -= delta
# print "Right crop: %s"%diff
self.image = self.image.crop((0,0 ,widthImg-diff,heightImg))
# print "New size: %sx%s"%(self.image.size[0],self.image.size[1])
return self.image
def addProgressbar(self, file_number, files_totalnumber, size, howoften):
if file_number//howoften!=float(file_number)/howoften:
return self.image
white = (255,255,255)
black = (0,0,0)
widthDev, heightDev = size
widthImg, heightImg = self.image.size
pastePt = (
max(0, (widthDev - widthImg) / 2),
max(0, (heightDev - heightImg) / 2)
)
imageBg = Image.new('RGB',size,white)
imageBg.paste(self.image, pastePt)
self.image = imageBg
widthImg, heightImg = self.image.size
draw = ImageDraw.Draw(self.image)
#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)
#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)
#The 50%
if i==5:
draw.rectangle([(widthImg/2-1,heightImg-5), (widthImg/2+1,heightImg)],outline=black,fill=notch_colour)
return self.image