mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Dont use IM for auto-trim of images
This commit is contained in:
parent
b57ea0ffd0
commit
8c06b18e69
@ -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
|
from calibre.utils.img import save_cover_data_to, remove_borders, image_to_data, image_from_data
|
||||||
from calibre.utils.imghdr import identify
|
from calibre.utils.imghdr import identify
|
||||||
|
|
||||||
class Worker(Thread):
|
class Worker(Thread):
|
||||||
@ -61,11 +61,10 @@ def process_result(log, result):
|
|||||||
plugin, data = result
|
plugin, data = result
|
||||||
try:
|
try:
|
||||||
if getattr(plugin, 'auto_trim_covers', False):
|
if getattr(plugin, 'auto_trim_covers', False):
|
||||||
from calibre.utils.magick import Image
|
img = image_from_data(data)
|
||||||
im = Image()
|
nimg = remove_borders(img)
|
||||||
im.load(data)
|
if nimg is not img:
|
||||||
im.trim(10)
|
data = image_to_data(nimg)
|
||||||
data = im.export('JPEG')
|
|
||||||
fmt, width, height = identify(data)
|
fmt, width, height = identify(data)
|
||||||
if width < 0 or height < 0:
|
if width < 0 or height < 0:
|
||||||
raise ValueError('Could not read cover image dimensions')
|
raise ValueError('Could not read cover image dimensions')
|
||||||
|
@ -200,15 +200,15 @@ 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.magick import Image
|
from calibre.utils.img import remove_borders, 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:
|
||||||
im = Image()
|
img = image_from_data(cdata)
|
||||||
im.load(cdata)
|
nimg = remove_borders(img)
|
||||||
im.trim(tweaks['cover_trim_fuzz_value'])
|
if nimg is not img:
|
||||||
cdata = im.export('jpg')
|
cdata = image_to_data(nimg)
|
||||||
cache.set_cover({book_id:cdata})
|
cache.set_cover({book_id:cdata})
|
||||||
elif args.cover_action == 'clone':
|
elif args.cover_action == 'clone':
|
||||||
cdata = None
|
cdata = None
|
||||||
for book_id in self.ids:
|
for book_id in self.ids:
|
||||||
|
@ -1105,15 +1105,15 @@ class Cover(ImageView): # {{{
|
|||||||
self.current_val = None
|
self.current_val = None
|
||||||
|
|
||||||
def trim_cover(self, *args):
|
def trim_cover(self, *args):
|
||||||
from calibre.utils.magick import Image
|
|
||||||
cdata = self.current_val
|
cdata = self.current_val
|
||||||
if not cdata:
|
if not cdata:
|
||||||
return
|
return
|
||||||
im = Image()
|
from calibre.utils.img import remove_borders, image_to_data, image_from_data
|
||||||
im.load(cdata)
|
img = image_from_data(cdata)
|
||||||
im.trim(tweaks['cover_trim_fuzz_value'])
|
nimg = remove_borders(img)
|
||||||
self.current_val = im.export('png')
|
if nimg is not img:
|
||||||
self.cdata_before_trim = cdata
|
self.cdata_before_trim = cdata
|
||||||
|
self.current_val = image_to_data(nimg, fmt='png')
|
||||||
|
|
||||||
def manual_trim_cover(self):
|
def manual_trim_cover(self):
|
||||||
cdata = self.current_val
|
cdata = self.current_val
|
||||||
|
@ -19,8 +19,8 @@ from calibre.gui2 import error_dialog, pixmap_to_data
|
|||||||
from calibre.gui2.dnd import (
|
from calibre.gui2.dnd import (
|
||||||
IMAGE_EXTENSIONS, dnd_has_extension, dnd_has_image, dnd_get_image, DownloadDialog)
|
IMAGE_EXTENSIONS, dnd_has_extension, dnd_has_image, dnd_get_image, DownloadDialog)
|
||||||
from calibre.gui2.tweak_book import capitalize
|
from calibre.gui2.tweak_book import capitalize
|
||||||
from calibre.utils.config_base import tweaks
|
|
||||||
from calibre.utils.imghdr import identify
|
from calibre.utils.imghdr import identify
|
||||||
|
from calibre.utils.img import remove_borders
|
||||||
from calibre.utils.magick import qimage_to_magick
|
from calibre.utils.magick import qimage_to_magick
|
||||||
|
|
||||||
def painter(func):
|
def painter(func):
|
||||||
@ -98,10 +98,7 @@ class AutoTrim(Trim):
|
|||||||
TEXT = _('Auto-trim image')
|
TEXT = _('Auto-trim image')
|
||||||
|
|
||||||
def __call__(self, canvas):
|
def __call__(self, canvas):
|
||||||
img = canvas.current_image
|
return remove_borders(canvas.current_image)
|
||||||
i = qimage_to_magick(img)
|
|
||||||
i.trim(tweaks['cover_trim_fuzz_value'])
|
|
||||||
return i.to_qimage()
|
|
||||||
|
|
||||||
class Rotate(Command):
|
class Rotate(Command):
|
||||||
|
|
||||||
|
@ -200,10 +200,15 @@ def flip_image(img, horizontal=False, vertical=False):
|
|||||||
return image_from_data(img).mirrored(horizontal, vertical)
|
return image_from_data(img).mirrored(horizontal, vertical)
|
||||||
|
|
||||||
def remove_borders(img, fuzz=None):
|
def remove_borders(img, fuzz=None):
|
||||||
|
''' 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
|
||||||
|
what colors are considered identical (must be a number between 0 and 255 in
|
||||||
|
absolute intensity units). Default is from a tweak whose default value is 10. '''
|
||||||
if imageops is None:
|
if imageops is None:
|
||||||
raise RuntimeError(imageops_err)
|
raise RuntimeError(imageops_err)
|
||||||
fuzz = tweaks['cover_trim_fuzz_value'] if fuzz is None else fuzz
|
fuzz = tweaks['cover_trim_fuzz_value'] if fuzz is None else fuzz
|
||||||
return 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
|
||||||
|
|
||||||
def run_optimizer(file_path, cmd, as_filter=False, input_data=None):
|
def run_optimizer(file_path, cmd, as_filter=False, input_data=None):
|
||||||
file_path = os.path.abspath(file_path)
|
file_path = os.path.abspath(file_path)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user