Make function naming in the util.img module consistent

This commit is contained in:
Kovid Goyal 2016-05-09 21:19:15 +05:30
parent 58b05d6a25
commit 25cb6a57f1
6 changed files with 34 additions and 39 deletions

View File

@ -15,7 +15,7 @@ from io import BytesIO
from calibre.customize.ui import metadata_plugins from calibre.customize.ui import metadata_plugins
from calibre.ebooks.metadata.sources.base import create_log from calibre.ebooks.metadata.sources.base import create_log
from calibre.ebooks.metadata.sources.prefs import msprefs from calibre.ebooks.metadata.sources.prefs import msprefs
from calibre.utils.img import save_cover_data_to, remove_borders, image_to_data, image_from_data from calibre.utils.img import save_cover_data_to, remove_borders_from_image, image_to_data, image_from_data
from calibre.utils.imghdr import identify from calibre.utils.imghdr import identify
class Worker(Thread): class Worker(Thread):
@ -62,7 +62,7 @@ def process_result(log, result):
try: try:
if getattr(plugin, 'auto_trim_covers', False): if getattr(plugin, 'auto_trim_covers', False):
img = image_from_data(data) img = image_from_data(data)
nimg = remove_borders(img) nimg = remove_borders_from_image(img)
if nimg is not img: if nimg is not img:
data = image_to_data(nimg) data = image_to_data(nimg)
fmt, width, height = identify(data) fmt, width, height = identify(data)

View File

@ -200,12 +200,12 @@ class MyBlockingBusy(QDialog): # {{{
if covers: if covers:
cache.set_cover({book_id:covers[-1][0]}) cache.set_cover({book_id:covers[-1][0]})
elif args.cover_action == 'trim': elif args.cover_action == 'trim':
from calibre.utils.img import remove_borders, image_to_data, image_from_data from calibre.utils.img import remove_borders_from_image, image_to_data, image_from_data
for book_id in self.ids: for book_id in self.ids:
cdata = cache.cover(book_id) cdata = cache.cover(book_id)
if cdata: if cdata:
img = image_from_data(cdata) img = image_from_data(cdata)
nimg = remove_borders(img) nimg = remove_borders_from_image(img)
if nimg is not img: if nimg is not img:
cdata = image_to_data(nimg) cdata = image_to_data(nimg)
cache.set_cover({book_id:cdata}) cache.set_cover({book_id:cdata})

View File

@ -1108,9 +1108,9 @@ class Cover(ImageView): # {{{
cdata = self.current_val cdata = self.current_val
if not cdata: if not cdata:
return return
from calibre.utils.img import remove_borders, image_to_data, image_from_data from calibre.utils.img import remove_borders_from_image, image_to_data, image_from_data
img = image_from_data(cdata) img = image_from_data(cdata)
nimg = remove_borders(img) nimg = remove_borders_from_image(img)
if nimg is not img: if nimg is not img:
self.cdata_before_trim = cdata self.cdata_before_trim = cdata
self.current_val = image_to_data(nimg, fmt='png') self.current_val = image_to_data(nimg, fmt='png')

View File

@ -22,8 +22,8 @@ from calibre.gui2.dnd import (
from calibre.gui2.tweak_book import capitalize from calibre.gui2.tweak_book import capitalize
from calibre.utils.imghdr import identify from calibre.utils.imghdr import identify
from calibre.utils.img import ( from calibre.utils.img import (
remove_borders, gaussian_sharpen, gaussian_blur, image_to_data, despeckle, remove_borders_from_image, gaussian_sharpen_image, gaussian_blur_image, image_to_data, despeckle_image,
normalize, oil_paint normalize_image, oil_paint_image
) )
def painter(func): def painter(func):
@ -101,7 +101,7 @@ class AutoTrim(Trim):
TEXT = _('Auto-trim image') TEXT = _('Auto-trim image')
def __call__(self, canvas): def __call__(self, canvas):
return remove_borders(canvas.current_image) return remove_borders_from_image(canvas.current_image)
class Rotate(Command): class Rotate(Command):
@ -135,7 +135,7 @@ class Sharpen(Command):
Command.__init__(self, canvas) Command.__init__(self, canvas)
def __call__(self, canvas): def __call__(self, canvas):
return gaussian_sharpen(canvas.current_image, sigma=self.sigma) return gaussian_sharpen_image(canvas.current_image, sigma=self.sigma)
class Blur(Sharpen): class Blur(Sharpen):
@ -143,7 +143,7 @@ class Blur(Sharpen):
FUNC = 'blur' FUNC = 'blur'
def __call__(self, canvas): def __call__(self, canvas):
return gaussian_blur(canvas.current_image, sigma=self.sigma) return gaussian_blur_image(canvas.current_image, sigma=self.sigma)
class Oilify(Command): class Oilify(Command):
@ -154,21 +154,21 @@ class Oilify(Command):
Command.__init__(self, canvas) Command.__init__(self, canvas)
def __call__(self, canvas): def __call__(self, canvas):
return oil_paint(canvas.current_image, radius=self.radius) return oil_paint_image(canvas.current_image, radius=self.radius)
class Despeckle(Command): class Despeckle(Command):
TEXT = _('De-speckle image') TEXT = _('De-speckle image')
def __call__(self, canvas): def __call__(self, canvas):
return despeckle(canvas.current_image) return despeckle_image(canvas.current_image)
class Normalize(Command): class Normalize(Command):
TEXT = _('Normalize image') TEXT = _('Normalize image')
def __call__(self, canvas): def __call__(self, canvas):
return normalize(canvas.current_image) return normalize_image(canvas.current_image)
class Replace(Command): class Replace(Command):

View File

@ -56,7 +56,8 @@ def image_and_format_from_data(data):
fmt = bytes(r.format()).decode('utf-8') fmt = bytes(r.format()).decode('utf-8')
return r.read(), fmt return r.read(), fmt
def add_borders(img, left=0, top=0, right=0, bottom=0, border_color='#ffffff'): def add_borders_to_image(img, left=0, top=0, right=0, bottom=0, border_color='#ffffff'):
img = image_from_data(img)
if not (left > 0 or right > 0 or top > 0 or bottom > 0): if not (left > 0 or right > 0 or top > 0 or bottom > 0):
return img return img
canvas = QImage(img.width() + left + right, img.height() + top + bottom, QImage.Format_RGB32) canvas = QImage(img.width() + left + right, img.height() + top + bottom, QImage.Format_RGB32)
@ -171,13 +172,7 @@ def normalize_format_name(fmt):
fmt = 'jpeg' fmt = 'jpeg'
return fmt return fmt
def add_borders_to_image(img_data, left=0, top=0, right=0, bottom=0, def grayscale_image(img):
border_color='#ffffff', fmt='jpg'):
img = image_from_data(img_data)
img = add_borders(img, left=left, top=top, right=right, bottom=bottom, border_color=border_color)
return image_to_data(img, fmt=fmt)
def to_grayscale(img):
if imageops is not None: if imageops is not None:
return imageops.grayscale(img) return imageops.grayscale(img)
return img return img
@ -219,7 +214,7 @@ def save_cover_data_to(data, path=None, bgcolor='#ffffff', resize_to=None, compr
if grayscale: if grayscale:
if not img.allGray(): if not img.allGray():
changed = True changed = True
img = to_grayscale(img) img = grayscale_image(img)
if path is None: if path is None:
return image_to_data(img, compression_quality, fmt) if changed else data return image_to_data(img, compression_quality, fmt) if changed else data
with lopen(path, 'wb') as f: with lopen(path, 'wb') as f:
@ -263,7 +258,7 @@ def rotate_image(img, degrees):
t.rotate(degrees) t.rotate(degrees)
return image_from_data(img).transformed(t) return image_from_data(img).transformed(t)
def remove_borders(img, fuzz=None): def remove_borders_from_image(img, fuzz=None):
''' Try to auto-detect and remove any borders from the image. Returns ''' Try to auto-detect and remove any borders from the image. Returns
the image itself if no borders could be removed. `fuzz` is a measure of the image itself if no borders could be removed. `fuzz` is a measure of
what colors are considered identical (must be a number between 0 and 255 in what colors are considered identical (must be a number between 0 and 255 in
@ -274,32 +269,32 @@ def remove_borders(img, fuzz=None):
ans = imageops.remove_borders(image_from_data(img), max(0, fuzz)) ans = imageops.remove_borders(image_from_data(img), max(0, fuzz))
return ans if ans.size() != img.size() else img return ans if ans.size() != img.size() else img
def gaussian_sharpen(img, radius=0, sigma=3, high_quality=True): def gaussian_sharpen_image(img, radius=0, sigma=3, high_quality=True):
if imageops is None: if imageops is None:
raise RuntimeError(imageops_err) raise RuntimeError(imageops_err)
return imageops.gaussian_sharpen(image_from_data(img), max(0, radius), sigma, high_quality) return imageops.gaussian_sharpen(image_from_data(img), max(0, radius), sigma, high_quality)
def gaussian_blur(img, radius=-1, sigma=3): def gaussian_blur_image(img, radius=-1, sigma=3):
if imageops is None: if imageops is None:
raise RuntimeError(imageops_err) raise RuntimeError(imageops_err)
return imageops.gaussian_blur(image_from_data(img), max(0, radius), sigma) return imageops.gaussian_blur(image_from_data(img), max(0, radius), sigma)
def despeckle(img): def despeckle_image(img):
if imageops is None: if imageops is None:
raise RuntimeError(imageops_err) raise RuntimeError(imageops_err)
return imageops.despeckle(image_from_data(img)) return imageops.despeckle(image_from_data(img))
def oil_paint(img, radius=-1, high_quality=True): def oil_paint_image(img, radius=-1, high_quality=True):
if imageops is None: if imageops is None:
raise RuntimeError(imageops_err) raise RuntimeError(imageops_err)
return imageops.oil_paint(image_from_data(img), radius, high_quality) return imageops.oil_paint(image_from_data(img), radius, high_quality)
def normalize(img): def normalize_image(img):
if imageops is None: if imageops is None:
raise RuntimeError(imageops_err) raise RuntimeError(imageops_err)
return imageops.normalize(image_from_data(img)) return imageops.normalize(image_from_data(img))
def quantize(img, max_colors=256, dither=True, palette=''): def quantize_image(img, max_colors=256, dither=True, palette=''):
''' Quantize the image to contain a maximum of `max_colors` colors. By ''' Quantize the image to contain a maximum of `max_colors` colors. By
default a palette is chosen automatically, if you want to use a fixed default a palette is chosen automatically, if you want to use a fixed
palette, then pass in a list of color names in the `palette` variable. If palette, then pass in a list of color names in the `palette` variable. If
@ -434,12 +429,12 @@ def test(): # {{{
if glob('*.bak'): if glob('*.bak'):
raise SystemExit('Spurious .bak files left behind') raise SystemExit('Spurious .bak files left behind')
img = image_from_data(I('devices/kindle.jpg', data=True, allow_user_override=False)) img = image_from_data(I('devices/kindle.jpg', data=True, allow_user_override=False))
quantize(img) quantize_image(img)
oil_paint(img) oil_paint_image(img)
gaussian_sharpen(img) gaussian_sharpen_image(img)
gaussian_blur(img) gaussian_blur_image(img)
despeckle(img) despeckle_image(img)
remove_borders(img) remove_borders_from_image(img)
# }}} # }}}
if __name__ == '__main__': # {{{ if __name__ == '__main__': # {{{

View File

@ -28,7 +28,7 @@ from calibre.utils.threadpool import WorkRequest, ThreadPool, NoResultsPending
from calibre.ptempfile import PersistentTemporaryFile from calibre.ptempfile import PersistentTemporaryFile
from calibre.utils.date import now as nowf from calibre.utils.date import now as nowf
from calibre.utils.icu import numeric_sort_key from calibre.utils.icu import numeric_sort_key
from calibre.utils.img import save_cover_data_to, add_borders_to_image from calibre.utils.img import save_cover_data_to, add_borders_to_image, image_to_data
from calibre.utils.localization import canonicalize_lang from calibre.utils.localization import canonicalize_lang
from calibre.utils.logging import ThreadSafeWrapper from calibre.utils.logging import ThreadSafeWrapper
@ -1276,10 +1276,10 @@ class BasicNewsRecipe(Recipe):
if not cdata: if not cdata:
return return
if self.cover_margins[0] or self.cover_margins[1]: if self.cover_margins[0] or self.cover_margins[1]:
cdata = add_borders_to_image(cdata, cdata = image_to_data(add_borders_to_image(cdata,
left=self.cover_margins[0],right=self.cover_margins[0], left=self.cover_margins[0],right=self.cover_margins[0],
top=self.cover_margins[1],bottom=self.cover_margins[1], top=self.cover_margins[1],bottom=self.cover_margins[1],
border_color=self.cover_margins[2]) border_color=self.cover_margins[2]))
cpath = os.path.join(self.output_dir, 'cover.jpg') cpath = os.path.join(self.output_dir, 'cover.jpg')
save_cover_data_to(cdata, cpath) save_cover_data_to(cdata, cpath)