mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Remove IM from the MOBI subsystem
This commit is contained in:
parent
9de04a07bf
commit
19651b3e26
@ -13,7 +13,7 @@ from threading import Event
|
|||||||
|
|
||||||
from calibre import prints
|
from calibre import prints
|
||||||
from calibre.utils.config import OptionParser
|
from calibre.utils.config import OptionParser
|
||||||
from calibre.utils.magick.draw import save_cover_data_to
|
from calibre.utils.img import save_cover_data_to
|
||||||
from calibre.ebooks.metadata import string_to_authors
|
from calibre.ebooks.metadata import string_to_authors
|
||||||
from calibre.ebooks.metadata.opf2 import metadata_to_opf
|
from calibre.ebooks.metadata.opf2 import metadata_to_opf
|
||||||
from calibre.ebooks.metadata.sources.base import create_log
|
from calibre.ebooks.metadata.sources.base import create_log
|
||||||
|
@ -14,7 +14,7 @@ from calibre.ebooks.oeb.base import XHTML, XHTML_NS, urlnormalize
|
|||||||
from calibre.ebooks.oeb.stylizer import Stylizer
|
from calibre.ebooks.oeb.stylizer import Stylizer
|
||||||
from calibre.ebooks.oeb.transforms.flatcss import KeyMapper
|
from calibre.ebooks.oeb.transforms.flatcss import KeyMapper
|
||||||
from calibre.ebooks.mobi.utils import convert_color_for_font_tag
|
from calibre.ebooks.mobi.utils import convert_color_for_font_tag
|
||||||
from calibre.utils.magick.draw import identify_data
|
from calibre.utils.imghdr import identify
|
||||||
|
|
||||||
MBP_NS = 'http://mobipocket.com/ns/mbp'
|
MBP_NS = 'http://mobipocket.com/ns/mbp'
|
||||||
def MBP(name):
|
def MBP(name):
|
||||||
@ -445,8 +445,8 @@ class MobiMLizer(object):
|
|||||||
href)
|
href)
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
width, height = identify_data(item.data)[:2]
|
width, height = identify(item.data)[1:]
|
||||||
except:
|
except Exception:
|
||||||
self.oeb.logger.warn('Invalid image:', href)
|
self.oeb.logger.warn('Invalid image:', href)
|
||||||
else:
|
else:
|
||||||
if 'width' not in istate.attrib and 'height' not in \
|
if 'width' not in istate.attrib and 'height' not in \
|
||||||
|
@ -8,17 +8,10 @@ __copyright__ = '2014, Kovid Goyal <kovid at kovidgoyal.net>'
|
|||||||
|
|
||||||
from struct import unpack_from, error
|
from struct import unpack_from, error
|
||||||
|
|
||||||
from calibre.utils.magick.draw import identify_data
|
|
||||||
from calibre.utils.imghdr import what
|
from calibre.utils.imghdr import what
|
||||||
|
|
||||||
def find_imgtype(data):
|
def find_imgtype(data):
|
||||||
imgtype = what(None, data)
|
return what(None, data) or 'unknown'
|
||||||
if imgtype is None:
|
|
||||||
try:
|
|
||||||
imgtype = identify_data(data)[2]
|
|
||||||
except Exception:
|
|
||||||
imgtype = 'unknown'
|
|
||||||
return imgtype
|
|
||||||
|
|
||||||
class Container(object):
|
class Container(object):
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ from calibre.ebooks.metadata import MetaInformation
|
|||||||
from calibre.ebooks.metadata.opf2 import OPFCreator, OPF
|
from calibre.ebooks.metadata.opf2 import OPFCreator, OPF
|
||||||
from calibre.ebooks.metadata.toc import TOC
|
from calibre.ebooks.metadata.toc import TOC
|
||||||
from calibre.ebooks.mobi.reader.headers import BookHeader
|
from calibre.ebooks.mobi.reader.headers import BookHeader
|
||||||
from calibre.utils.magick.draw import save_cover_data_to
|
from calibre.utils.img import save_cover_data_to
|
||||||
from calibre.utils.imghdr import what
|
from calibre.utils.imghdr import what
|
||||||
|
|
||||||
class TopazError(ValueError):
|
class TopazError(ValueError):
|
||||||
@ -863,7 +863,7 @@ class MobiReader(object):
|
|||||||
|
|
||||||
path = os.path.join(output_dir, '%05d.jpg' % image_index)
|
path = os.path.join(output_dir, '%05d.jpg' % image_index)
|
||||||
try:
|
try:
|
||||||
if what(None, data) not in {'jpg', 'jpeg', 'gif', 'png', 'bmp', 'webp'}:
|
if what(None, data) not in {'jpg', 'jpeg', 'gif', 'png', 'bmp'}:
|
||||||
continue
|
continue
|
||||||
save_cover_data_to(data, path, minify_to=(10000, 10000))
|
save_cover_data_to(data, path, minify_to=(10000, 10000))
|
||||||
except Exception:
|
except Exception:
|
||||||
|
@ -11,7 +11,7 @@ import struct, string, zlib, os
|
|||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
from calibre.utils.magick.draw import Image, save_cover_data_to, thumbnail
|
from calibre.utils.img import save_cover_data_to, scale_image, image_to_data, image_from_data, resize_image
|
||||||
from calibre.utils.imghdr import what
|
from calibre.utils.imghdr import what
|
||||||
from calibre.ebooks import normalize
|
from calibre.ebooks import normalize
|
||||||
from tinycss.color3 import parse_color_string
|
from tinycss.color3 import parse_color_string
|
||||||
@ -148,34 +148,27 @@ def rescale_image(data, maxsizeb=IMAGE_MAX_SIZE, dimen=None):
|
|||||||
width, height = dimen
|
width, height = dimen
|
||||||
else:
|
else:
|
||||||
width = height = dimen
|
width = height = dimen
|
||||||
data = thumbnail(data, width=width, height=height,
|
data = scale_image(data, width=width, height=height, compression_quality=90)[-1]
|
||||||
compression_quality=90)[-1]
|
|
||||||
else:
|
else:
|
||||||
# Replace transparent pixels with white pixels and convert to JPEG
|
# Replace transparent pixels with white pixels and convert to JPEG
|
||||||
data = save_cover_data_to(data, 'img.jpg', return_data=True)
|
data = save_cover_data_to(data)
|
||||||
if len(data) <= maxsizeb:
|
if len(data) <= maxsizeb:
|
||||||
return data
|
return data
|
||||||
orig_data = data
|
orig_data = data # save it in case compression fails
|
||||||
img = Image()
|
quality = 90
|
||||||
quality = 95
|
while len(data) > maxsizeb and quality >= 5:
|
||||||
|
data = image_to_data(image_from_data(orig_data), compression_quality=quality)
|
||||||
img.load(data)
|
|
||||||
while len(data) >= maxsizeb and quality >= 10:
|
|
||||||
quality -= 5
|
quality -= 5
|
||||||
img.set_compression_quality(quality)
|
|
||||||
data = img.export('jpg')
|
|
||||||
if len(data) <= maxsizeb:
|
if len(data) <= maxsizeb:
|
||||||
return data
|
return data
|
||||||
orig_data = data
|
orig_data = data
|
||||||
|
|
||||||
scale = 0.9
|
scale = 0.9
|
||||||
while len(data) >= maxsizeb and scale >= 0.05:
|
while len(data) > maxsizeb and scale >= 0.05:
|
||||||
img = Image()
|
img = image_from_data(data)
|
||||||
img.load(orig_data)
|
w, h = img.width(), img.height()
|
||||||
w, h = img.size
|
img = resize_image(img, int(scale*w), int(scale*h))
|
||||||
img.size = (int(scale*w), int(scale*h))
|
data = image_to_data(img, compression_quality=quality)
|
||||||
img.set_compression_quality(quality)
|
|
||||||
data = img.export('jpg')
|
|
||||||
scale -= 0.05
|
scale -= 0.05
|
||||||
return data
|
return data
|
||||||
|
|
||||||
@ -391,9 +384,11 @@ def mobify_image(data):
|
|||||||
fmt = what(None, data)
|
fmt = what(None, data)
|
||||||
|
|
||||||
if fmt == 'png':
|
if fmt == 'png':
|
||||||
im = Image()
|
from PIL import Image
|
||||||
im.load(data)
|
im = Image.open(BytesIO(data))
|
||||||
data = im.export('gif')
|
buf = BytesIO()
|
||||||
|
im.save(buf, 'gif')
|
||||||
|
data = buf.getvalue()
|
||||||
return data
|
return data
|
||||||
|
|
||||||
# Font records {{{
|
# Font records {{{
|
||||||
|
@ -45,15 +45,14 @@ class Resources(object):
|
|||||||
try:
|
try:
|
||||||
return func(data)
|
return func(data)
|
||||||
except Exception:
|
except Exception:
|
||||||
from calibre.utils.magick.draw import identify_data
|
if 'png' != what(None, data):
|
||||||
if 'png' != identify_data(data)[-1].lower():
|
|
||||||
raise
|
raise
|
||||||
with PersistentTemporaryFile(suffix='.png') as pt:
|
with PersistentTemporaryFile(suffix='.png') as pt:
|
||||||
pt.write(data)
|
pt.write(data)
|
||||||
try:
|
try:
|
||||||
from calibre.utils.img import optimize_png
|
from calibre.utils.img import optimize_png
|
||||||
optimize_png(pt.name)
|
optimize_png(pt.name)
|
||||||
data = open(pt.name, 'rb').read()
|
data = lopen(pt.name, 'rb').read()
|
||||||
finally:
|
finally:
|
||||||
os.remove(pt.name)
|
os.remove(pt.name)
|
||||||
return func(data)
|
return func(data)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user