1
0
mirror of https://github.com/ciromattia/kcc synced 2026-07-03 11:35:34 +00:00

Code cleanup

This commit is contained in:
Paweł Jastrzębski
2013-03-12 14:00:52 +01:00
parent 93a98db43e
commit e3127ed516
2 changed files with 65 additions and 140 deletions
+19 -81
View File
@@ -272,12 +272,7 @@ def applyImgOptimization(img, isSplit=False, toRight=False):
img.cropWhiteSpace(10.0) img.cropWhiteSpace(10.0)
if options.cutpagenumbers: if options.cutpagenumbers:
img.cutPageNumber() img.cutPageNumber()
if options.fakepanelview: img.resizeImage(options.upscale, options.stretch, options.black_borders, options.fakepanelviewlandscape, isSplit, toRight, options.landscapemode)
img.resizeImage(True, False, options.black_borders, False, False, False)
elif options.fakepanelviewlandscape:
img.resizeImage(False, False, options.black_borders, False, False, False, True)
else:
img.resizeImage(options.upscale, options.stretch, options.black_borders, isSplit, toRight, options.landscapemode)
if not options.notquantize: if not options.notquantize:
img.quantizeImage() img.quantizeImage()
@@ -318,58 +313,19 @@ def dirImgProcess(path):
if facing == "right": if facing == "right":
splitCount += 1 splitCount += 1
facing = "left" facing = "left"
if options.fakepanelview: if options.fakepanelview or options.fakepanelviewlandscape:
img0 = image.ComicPage(split[0], options.profile) img0 = image.ComicPage(split[0], options.profile)
splitA = img0.splitPageFakePanelView(dirpath, options.righttoleft)
img00 = image.ComicPage(splitA[0], options.profile)
applyImgOptimization(img00)
img00.saveToDir(dirpath, options.notquantize)
img01 = image.ComicPage(splitA[1], options.profile)
applyImgOptimization(img01)
img01.saveToDir(dirpath, options.notquantize)
img02 = image.ComicPage(splitA[2], options.profile)
applyImgOptimization(img02)
img02.saveToDir(dirpath, options.notquantize)
img03 = image.ComicPage(splitA[3], options.profile)
applyImgOptimization(img03)
img03.saveToDir(dirpath, options.notquantize)
img04 = image.ComicPage(splitA[4], options.profile)
applyImgOptimization(img04)
img04.saveToDir(dirpath, options.notquantize)
img1 = image.ComicPage(split[1], options.profile) img1 = image.ComicPage(split[1], options.profile)
splitB = img1.splitPageFakePanelView(dirpath, options.righttoleft) splitA = img0.splitPageFakePanelView(dirpath, options.righttoleft, options.fakepanelviewlandscape)
img10 = image.ComicPage(splitB[0], options.profile) splitB = img1.splitPageFakePanelView(dirpath, options.righttoleft, options.fakepanelviewlandscape)
applyImgOptimization(img10) for img in splitA:
img10.saveToDir(dirpath, options.notquantize) tempImg = image.ComicPage(img, options.profile)
img11 = image.ComicPage(splitB[1], options.profile) applyImgOptimization(tempImg)
applyImgOptimization(img11) tempImg.saveToDir(dirpath, options.notquantize)
img11.saveToDir(dirpath, options.notquantize) for img in splitB:
img12 = image.ComicPage(splitB[2], options.profile) tempImg = image.ComicPage(img, options.profile)
applyImgOptimization(img12) applyImgOptimization(tempImg)
img12.saveToDir(dirpath, options.notquantize) tempImg.saveToDir(dirpath, options.notquantize)
img13 = image.ComicPage(splitB[3], options.profile)
applyImgOptimization(img13)
img13.saveToDir(dirpath, options.notquantize)
img14 = image.ComicPage(splitB[4], options.profile)
applyImgOptimization(img14)
img14.saveToDir(dirpath, options.notquantize)
elif options.fakepanelviewlandscape:
img0 = image.ComicPage(split[0], options.profile)
splitA = img0.splitPageFakePanelViewLandscape(dirpath, options.righttoleft)
img01 = image.ComicPage(splitA[0], options.profile)
applyImgOptimization(img01)
img01.saveToDir(dirpath, options.notquantize)
img02 = image.ComicPage(splitA[1], options.profile)
applyImgOptimization(img02)
img02.saveToDir(dirpath, options.notquantize)
img1 = image.ComicPage(split[1], options.profile)
splitB = img1.splitPageFakePanelViewLandscape(dirpath, options.righttoleft)
img11 = image.ComicPage(splitB[0], options.profile)
applyImgOptimization(img11)
img11.saveToDir(dirpath, options.notquantize)
img12 = image.ComicPage(splitB[1], options.profile)
applyImgOptimization(img12)
img12.saveToDir(dirpath, options.notquantize)
else: else:
img0 = image.ComicPage(split[0], options.profile) img0 = image.ComicPage(split[0], options.profile)
applyImgOptimization(img0, True, toRight1) applyImgOptimization(img0, True, toRight1)
@@ -382,31 +338,12 @@ def dirImgProcess(path):
facing = "left" facing = "left"
else: else:
facing = "right" facing = "right"
if options.fakepanelview: if options.fakepanelview or options.fakepanelviewlandscape:
split = img.splitPageFakePanelView(dirpath, options.righttoleft) split = img.splitPageFakePanelView(dirpath, options.righttoleft, options.fakepanelviewlandscape)
img0 = image.ComicPage(split[0], options.profile) for img in split:
applyImgOptimization(img0) tempImg = image.ComicPage(img, options.profile)
img0.saveToDir(dirpath, options.notquantize) applyImgOptimization(tempImg)
img1 = image.ComicPage(split[1], options.profile) tempImg.saveToDir(dirpath, options.notquantize)
applyImgOptimization(img1)
img1.saveToDir(dirpath, options.notquantize)
img2 = image.ComicPage(split[2], options.profile)
applyImgOptimization(img2)
img2.saveToDir(dirpath, options.notquantize)
img3 = image.ComicPage(split[3], options.profile)
applyImgOptimization(img3)
img3.saveToDir(dirpath, options.notquantize)
img4 = image.ComicPage(split[4], options.profile)
applyImgOptimization(img4)
img4.saveToDir(dirpath, options.notquantize)
elif options.fakepanelviewlandscape:
split = img.splitPageFakePanelViewLandscape(dirpath, options.righttoleft)
img1 = image.ComicPage(split[0], options.profile)
applyImgOptimization(img1)
img1.saveToDir(dirpath, options.notquantize)
img2 = image.ComicPage(split[1], options.profile)
applyImgOptimization(img2)
img2.saveToDir(dirpath, options.notquantize)
else: else:
applyImgOptimization(img) applyImgOptimization(img)
img.saveToDir(dirpath, options.notquantize) img.saveToDir(dirpath, options.notquantize)
@@ -595,6 +532,7 @@ def checkOptions():
options.landscapemode = False options.landscapemode = False
if options.fakepanelview or options.fakepanelviewlandscape: if options.fakepanelview or options.fakepanelviewlandscape:
options.imgproc = True options.imgproc = True
options.upscale = True
options.rotate = False options.rotate = False
options.nosplitrotate = False options.nosplitrotate = False
if options.fakepanelview and options.fakepanelviewlandscape: if options.fakepanelview and options.fakepanelviewlandscape:
+46 -59
View File
@@ -139,13 +139,13 @@ class ComicPage:
palImg.putpalette(self.palette) palImg.putpalette(self.palette)
self.image = self.image.quantize(palette=palImg) self.image = self.image.quantize(palette=palImg)
def resizeImage(self, upscale=False, stretch=False, black_borders=False, isSplit=False, toRight=False, landscapeMode=False, fakepanelviewlandscape=False): def resizeImage(self, upscale=False, stretch=False, black_borders=False, fakePanelViewLandscape=False, isSplit=False, toRight=False, landscapeMode=False):
method = Image.ANTIALIAS method = Image.ANTIALIAS
if black_borders: if black_borders:
fill = 'black' fill = 'black'
else: else:
fill = 'white' fill = 'white'
if fakepanelviewlandscape: if fakePanelViewLandscape:
self.image = self.image.rotate(90) self.image = self.image.rotate(90)
if self.image.size[0] <= self.size[0] and self.image.size[1] <= self.size[1]: if self.image.size[0] <= self.size[0] and self.image.size[1] <= self.size[1]:
if not upscale: if not upscale:
@@ -221,69 +221,56 @@ class ComicPage:
return None return None
def splitPageFakePanelView(self, targetdir, righttoleft=False): def splitPageFakePanelView(self, targetdir, righttoleft=False, fakePanelWiewLandscape=False):
width, height = self.image.size width, height = self.image.size
topleftbox = (0, 0, ((width / 2) + (width/9)), ((height / 2) + (height/9))) if fakePanelWiewLandscape:
toprightbox = ((width / 2) - (width/9)), 0, width, ((height / 2) + (height/9)) topbox = (0, 0, width, ((height / 2) + (height/9)))
bottomleftbox = (0, ((height / 2) - (height/9)), ((width / 2) + (width/9)), height) bottombox = (0, ((height / 2) - (height/9)), width, height)
bototmrightbox = (((width / 2) - (width/9)), ((height / 2) - (height/9)), width, height) filename = os.path.splitext(os.path.basename(self.origFileName))
filename = os.path.splitext(os.path.basename(self.origFileName)) file1 = targetdir + '/' + filename[0] + '-1' + filename[1]
file0 = targetdir + '/' + filename[0] + '-0' + filename[1] file2 = targetdir + '/' + filename[0] + '-2' + filename[1]
file1 = targetdir + '/' + filename[0] + '-1' + filename[1] try:
file2 = targetdir + '/' + filename[0] + '-2' + filename[1] page1 = self.image.crop(topbox)
file3 = targetdir + '/' + filename[0] + '-3' + filename[1] page2 = self.image.crop(bottombox)
file4 = targetdir + '/' + filename[0] + '-4' + filename[1] page1.save(file1)
try: page2.save(file2)
if righttoleft: os.remove(self.origFileName)
page0 = self.image except IOError as e:
page1 = self.image.crop(toprightbox) raise RuntimeError('Cannot write image in directory %s: %s' % (targetdir, e))
page2 = self.image.crop(topleftbox) return file1, file2
page3 = self.image.crop(bototmrightbox) else:
page4 = self.image.crop(bottomleftbox) topleftbox = (0, 0, ((width / 2) + (width/9)), ((height / 2) + (height/9)))
else: toprightbox = ((width / 2) - (width/9)), 0, width, ((height / 2) + (height/9))
page0 = self.image bottomleftbox = (0, ((height / 2) - (height/9)), ((width / 2) + (width/9)), height)
page1 = self.image.crop(topleftbox) bototmrightbox = (((width / 2) - (width/9)), ((height / 2) - (height/9)), width, height)
page2 = self.image.crop(toprightbox) filename = os.path.splitext(os.path.basename(self.origFileName))
page3 = self.image.crop(bottomleftbox) file0 = targetdir + '/' + filename[0] + '-0' + filename[1]
page4 = self.image.crop(bototmrightbox) file1 = targetdir + '/' + filename[0] + '-1' + filename[1]
if page0.mode == "P": file2 = targetdir + '/' + filename[0] + '-2' + filename[1]
page0.save(file0, "PNG") file3 = targetdir + '/' + filename[0] + '-3' + filename[1]
page1.save(file1, "PNG") file4 = targetdir + '/' + filename[0] + '-4' + filename[1]
page2.save(file2, "PNG") try:
page3.save(file3, "PNG") if righttoleft:
page4.save(file4, "PNG") page0 = self.image
else: page1 = self.image.crop(toprightbox)
page2 = self.image.crop(topleftbox)
page3 = self.image.crop(bototmrightbox)
page4 = self.image.crop(bottomleftbox)
else:
page0 = self.image
page1 = self.image.crop(topleftbox)
page2 = self.image.crop(toprightbox)
page3 = self.image.crop(bottomleftbox)
page4 = self.image.crop(bototmrightbox)
page0.save(file0) page0.save(file0)
page1.save(file1) page1.save(file1)
page2.save(file2) page2.save(file2)
page3.save(file3) page3.save(file3)
page4.save(file4) page4.save(file4)
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 file0, file1, file2, file3, file4 return file0, file1, file2, file3, file4
def splitPageFakePanelViewLandscape(self, targetdir, righttoleft=False):
width, height = self.image.size
topbox = (0, 0, width, ((height / 2) + (height/9)))
bottombox = (0, ((height / 2) - (height/9)), width, height)
filename = os.path.splitext(os.path.basename(self.origFileName))
file1 = targetdir + '/' + filename[0] + '-1' + filename[1]
file2 = targetdir + '/' + filename[0] + '-2' + filename[1]
try:
page1 = self.image.crop(topbox)
page2 = self.image.crop(bottombox)
if page1.mode == "P":
page1.save(file1, "PNG")
page2.save(file2, "PNG")
else:
page1.save(file1)
page2.save(file2)
os.remove(self.origFileName)
except IOError as e:
raise RuntimeError('Cannot write image in directory %s: %s' % (targetdir, e))
return file1, file2
# def frameImage(self): # def frameImage(self):