Remove IM from Comic Input

This commit is contained in:
Kovid Goyal 2016-05-10 08:46:24 +05:30
parent c82f04c3b1
commit 1cf86cb89e
2 changed files with 38 additions and 43 deletions

View File

@ -89,14 +89,13 @@ class PageProcessor(list): # {{{
self.render() self.render()
def render(self): def render(self):
from calibre.utils.magick import Image from calibre.utils.img import image_from_data, scale_image, crop_image
img = Image() with lopen(self.path_to_page, 'rb') as f:
img.open(self.path_to_page) img = image_from_data(f.read())
width, height = img.size width, height = img.width(), img.height()
if self.num == 0: # First image so create a thumbnail from it if self.num == 0: # First image so create a thumbnail from it
thumb = img.clone with lopen(os.path.join(self.dest, 'thumbnail.png'), 'wb') as f:
thumb.thumbnail(60, 80) f.write(scale_image(img, as_png=True)[-1])
thumb.save(os.path.join(self.dest, 'thumbnail.png'))
self.pages = [img] self.pages = [img]
if width > height: if width > height:
if self.opts.landscape: if self.opts.landscape:
@ -104,29 +103,28 @@ class PageProcessor(list): # {{{
else: else:
split1, split2 = img.clone, img.clone split1, split2 = img.clone, img.clone
half = int(width/2) half = int(width/2)
split1.crop(half-1, height, 0, 0) split1 = crop_image(img, 0, 0, half - 1, height)
split2.crop(half-1, height, half, 0) split2 = crop_image(img, half, 0, half-1, height)
self.pages = [split2, split1] if self.opts.right2left else [split1, split2] self.pages = [split2, split1] if self.opts.right2left else [split1, split2]
self.process_pages() self.process_pages()
def process_pages(self): def process_pages(self):
from calibre.utils.magick import PixelWand from calibre.utils.img import (
for i, wand in enumerate(self.pages): image_to_data, rotate_image, remove_borders_from_image, normalize_image,
pw = PixelWand() add_borders_to_image, resize_image, gaussian_sharpen_image, grayscale_image,
pw.color = '#ffffff' despeckle_image, quantize_image
)
wand.set_border_color(pw) for i, img in enumerate(self.pages):
if self.rotate: if self.rotate:
wand.rotate(pw, -90) img = rotate_image(img, -90)
if not self.opts.disable_trim: if not self.opts.disable_trim:
wand.trim(25*65535/100) img = remove_borders_from_image(img)
wand.set_page(0, 0, 0, 0) # Clear page after trim, like a "+repage"
# Do the Photoshop "Auto Levels" equivalent # Do the Photoshop "Auto Levels" equivalent
if not self.opts.dont_normalize: if not self.opts.dont_normalize:
wand.normalize() img = normalize_image(img)
sizex, sizey = wand.size sizex, sizey = img.width(), img.height()
SCRWIDTH, SCRHEIGHT = self.opts.output_profile.comic_screen_size SCRWIDTH, SCRHEIGHT = self.opts.output_profile.comic_screen_size
@ -153,9 +151,8 @@ class PageProcessor(list): # {{{
if newsizex < MAX_SCREEN_SIZE and newsizey < MAX_SCREEN_SIZE: if newsizex < MAX_SCREEN_SIZE and newsizey < MAX_SCREEN_SIZE:
# Too large and resizing fails, so better # Too large and resizing fails, so better
# to leave it as original size # to leave it as original size
wand.size = (newsizex, newsizey) img = resize_image(img, newsizex, newsizey)
wand.set_border_color(pw) img = add_borders_to_image(img, left=deltax, right=deltax, top=deltay, bottom=deltay)
wand.add_border(pw, deltax, deltay)
elif self.opts.wide: elif self.opts.wide:
# Keep aspect and Use device height as scaled image width so landscape mode is clean # Keep aspect and Use device height as scaled image width so landscape mode is clean
aspect = float(sizex) / float(sizey) aspect = float(sizex) / float(sizey)
@ -177,31 +174,27 @@ class PageProcessor(list): # {{{
if newsizex < MAX_SCREEN_SIZE and newsizey < MAX_SCREEN_SIZE: if newsizex < MAX_SCREEN_SIZE and newsizey < MAX_SCREEN_SIZE:
# Too large and resizing fails, so better # Too large and resizing fails, so better
# to leave it as original size # to leave it as original size
wand.size = (newsizex, newsizey) img = resize_image(img, newsizex, newsizey)
wand.set_border_color(pw) img = add_borders_to_image(img, left=deltax, right=deltax, top=deltay, bottom=deltay)
wand.add_border(pw, deltax, deltay)
else: else:
if SCRWIDTH < MAX_SCREEN_SIZE and SCRHEIGHT < MAX_SCREEN_SIZE: if SCRWIDTH < MAX_SCREEN_SIZE and SCRHEIGHT < MAX_SCREEN_SIZE:
wand.size = (SCRWIDTH, SCRHEIGHT) img = resize_image(img, SCRWIDTH, SCRHEIGHT)
if not self.opts.dont_sharpen: if not self.opts.dont_sharpen:
wand.sharpen(0.0, 1.0) img = gaussian_sharpen_image(img, 0.0, 1.0)
if not self.opts.dont_grayscale: if not self.opts.dont_grayscale:
wand.type = 'GrayscaleType' img = grayscale_image(img)
if self.opts.despeckle: if self.opts.despeckle:
wand.despeckle() img = despeckle_image(img)
wand.quantize(self.opts.colors) if self.opts.output_format.lower() == 'png' and self.opts.colors:
img = quantize_image(img, max_colors=min(256, self.opts.colors))
dest = '%d_%d.%s'%(self.num, i, self.opts.output_format) dest = '%d_%d.%s'%(self.num, i, self.opts.output_format)
dest = os.path.join(self.dest, dest) dest = os.path.join(self.dest, dest)
if dest.lower().endswith('.png'): with lopen(dest, 'wb') as f:
dest += '8' f.write(image_to_data(img, fmt=self.opts.output_format))
wand.save(dest)
if dest.endswith('8'):
dest = dest[:-1]
os.rename(dest+'8', dest)
self.append(dest) self.append(dest)
# }}} # }}}

View File

@ -23,10 +23,10 @@ class ComicInput(InputFormatPlugin):
core_usage = -1 core_usage = -1
options = set([ options = set([
OptionRecommendation(name='colors', recommended_value=256, OptionRecommendation(name='colors', recommended_value=0,
help=_('Number of colors for grayscale image conversion. Default: ' help=_('Reduce the number of colors used in the image. This works only'
'%default. Values of less than 256 may result in blurred text ' ' if you choose the PNG output format. It is useful to reduce file sizes.'
'on your device if you are creating your comics in EPUB format.')), ' Set to zero to turn off. Maximum value is 256. It is off by default.')),
OptionRecommendation(name='dont_normalize', recommended_value=False, OptionRecommendation(name='dont_normalize', recommended_value=False,
help=_('Disable normalize (improve contrast) color range ' help=_('Disable normalize (improve contrast) color range '
'for pictures. Default: False')), 'for pictures. Default: False')),
@ -178,7 +178,8 @@ class ComicInput(InputFormatPlugin):
if not os.path.exists(cdir): if not os.path.exists(cdir):
os.makedirs(cdir) os.makedirs(cdir)
pages = self.get_pages(fname, cdir) pages = self.get_pages(fname, cdir)
if not pages: continue if not pages:
continue
wrappers = self.create_wrappers(pages) wrappers = self.create_wrappers(pages)
comics.append((title, pages, wrappers)) comics.append((title, pages, wrappers))
@ -191,7 +192,8 @@ class ComicInput(InputFormatPlugin):
entries = [] entries = []
def href(x): def href(x):
if len(comics) == 1: return os.path.basename(x) if len(comics) == 1:
return os.path.basename(x)
return '/'.join(x.split(os.sep)[-2:]) return '/'.join(x.split(os.sep)[-2:])
for comic in comics: for comic in comics: