本文整理汇总了Python中calibre.utils.magick.Image.export方法的典型用法代码示例。如果您正苦于以下问题:Python Image.export方法的具体用法?Python Image.export怎么用?Python Image.export使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类calibre.utils.magick.Image
的用法示例。
在下文中一共展示了Image.export方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: browse_icon
# 需要导入模块: from calibre.utils.magick import Image [as 别名]
# 或者: from calibre.utils.magick.Image import export [as 别名]
def browse_icon(self, name="blank.png"):
cherrypy.response.headers["Content-Type"] = "image/png"
cherrypy.response.headers["Last-Modified"] = self.last_modified(self.build_time)
if not hasattr(self, "__browse_icon_cache__"):
self.__browse_icon_cache__ = {}
if name not in self.__browse_icon_cache__:
if name.startswith("_"):
name = sanitize_file_name2(name[1:])
try:
with open(os.path.join(config_dir, "tb_icons", name), "rb") as f:
data = f.read()
except:
raise cherrypy.HTTPError(404, "no icon named: %r" % name)
else:
try:
data = I(name, data=True)
except:
raise cherrypy.HTTPError(404, "no icon named: %r" % name)
img = Image()
img.load(data)
width, height = img.size
scaled, width, height = fit_image(width, height, 48, 48)
if scaled:
img.size = (width, height)
self.__browse_icon_cache__[name] = img.export("png")
return self.__browse_icon_cache__[name]
示例2: browse_icon
# 需要导入模块: from calibre.utils.magick import Image [as 别名]
# 或者: from calibre.utils.magick.Image import export [as 别名]
def browse_icon(self, name='blank.png'):
cherrypy.response.headers['Content-Type'] = 'image/png'
cherrypy.response.headers['Last-Modified'] = self.last_modified(self.build_time)
if not hasattr(self, '__browse_icon_cache__'):
self.__browse_icon_cache__ = {}
if name not in self.__browse_icon_cache__:
if name.startswith('_'):
name = sanitize_file_name2(name[1:])
try:
with open(os.path.join(config_dir, 'tb_icons', name), 'rb') as f:
data = f.read()
except:
raise cherrypy.HTTPError(404, 'no icon named: %r'%name)
else:
try:
data = I(name, data=True)
except:
raise cherrypy.HTTPError(404, 'no icon named: %r'%name)
img = Image()
img.load(data)
width, height = img.size
scaled, width, height = fit_image(width, height, 48, 48)
if scaled:
img.size = (width, height)
self.__browse_icon_cache__[name] = img.export('png')
return self.__browse_icon_cache__[name]
示例3: extract_raster_image
# 需要导入模块: from calibre.utils.magick import Image [as 别名]
# 或者: from calibre.utils.magick.Image import export [as 别名]
def extract_raster_image(wmf_data):
try:
wmf, wmf_err = plugins['wmf']
except KeyError:
raise Unavailable('libwmf not available on this platform')
if wmf_err:
raise Unavailable(wmf_err)
if iswindows:
import sys, os
appdir = sys.app_dir
if isinstance(appdir, unicode):
appdir = appdir.encode(filesystem_encoding)
fdir = os.path.join(appdir, 'wmffonts')
wmf.set_font_dir(fdir)
data = ''
with TemporaryDirectory('wmf2png') as tdir:
with CurrentDir(tdir):
wmf.render(wmf_data)
images = list(sorted(glob.glob('*.png')))
if not images:
raise NoRaster('No raster images in WMF')
data = open(images[0], 'rb').read()
im = Image()
im.load(data)
pw = PixelWand()
pw.color = '#ffffff'
im.rotate(pw, 180)
return im.export('png')
示例4: fb2mlize_images
# 需要导入模块: from calibre.utils.magick import Image [as 别名]
# 或者: from calibre.utils.magick.Image import export [as 别名]
def fb2mlize_images(self):
'''
This function uses the self.image_hrefs dictionary mapping. It is populated by the dump_text function.
'''
from calibre.ebooks.oeb.base import OEB_RASTER_IMAGES
images = []
for item in self.oeb_book.manifest:
# Don't write the image if it's not referenced in the document's text.
if item.href not in self.image_hrefs:
continue
if item.media_type in OEB_RASTER_IMAGES:
try:
if item.media_type != 'image/jpeg':
im = Image()
im.load(item.data)
im.set_compression_quality(70)
imdata = im.export('jpg')
raw_data = b64encode(imdata)
else:
raw_data = b64encode(item.data)
# Don't put the encoded image on a single line.
data = ''
col = 1
for char in raw_data:
if col == 72:
data += '\n'
col = 1
col += 1
data += char
images.append('<binary id="%s" content-type="image/jpeg">%s\n</binary>' % (self.image_hrefs[item.href], data))
except Exception as e:
self.log.error('Error: Could not include file %s because '
'%s.' % (item.href, e))
return ''.join(images)
示例5: convert_image
# 需要导入模块: from calibre.utils.magick import Image [as 别名]
# 或者: from calibre.utils.magick.Image import export [as 别名]
def convert_image(url, data, sizes, grayscale, removetrans, imgtype="jpg", background="#ffffff"):
export = False
img = Image()
img.load(data)
owidth, oheight = img.size
nwidth, nheight = sizes
scaled, nwidth, nheight = fit_image(owidth, oheight, nwidth, nheight)
if normalize_format_name(img.format) == "gif" and GifInfo(StringIO(data), CHECK_IS_ANIMATED).frameCount > 1:
raise exceptions.RejectImage("Animated gifs come out purely--not going to use it.")
if scaled:
img.size = (nwidth, nheight)
export = True
if normalize_format_name(img.format) != imgtype:
export = True
if removetrans and img.has_transparent_pixels():
canvas = Image()
canvas.create_canvas(int(img.size[0]), int(img.size[1]), unicode(background))
canvas.compose(img)
img = canvas
export = True
if grayscale and img.type != "GrayscaleType":
img.type = "GrayscaleType"
export = True
if export:
return (img.export(convtype[imgtype]), imgtype, imagetypes[imgtype])
else:
logger.debug("image used unchanged")
return (data, imgtype, imagetypes[imgtype])
示例6: convert_image
# 需要导入模块: from calibre.utils.magick import Image [as 别名]
# 或者: from calibre.utils.magick.Image import export [as 别名]
def convert_image(url,data,sizes,grayscale,
removetrans,imgtype="jpg",background='#ffffff'):
export = False
img = Image()
img.load(data)
owidth, oheight = img.size
nwidth, nheight = sizes
scaled, nwidth, nheight = fit_image(owidth, oheight, nwidth, nheight)
if scaled:
img.size = (nwidth, nheight)
export = True
if normalize_format_name(img.format) != imgtype:
export = True
if removetrans and img.has_transparent_pixels():
canvas = Image()
canvas.create_canvas(int(img.size[0]), int(img.size[1]), str(background))
canvas.compose(img)
img = canvas
export = True
if grayscale and img.type != "GrayscaleType":
img.type = "GrayscaleType"
export = True
if export:
return (img.export(convtype[imgtype]),imgtype,imagetypes[imgtype])
else:
logger.debug("image used unchanged")
return (data,imgtype,imagetypes[imgtype])
示例7: resize_image
# 需要导入模块: from calibre.utils.magick import Image [as 别名]
# 或者: from calibre.utils.magick.Image import export [as 别名]
def resize_image(self, raw, base, max_width, max_height):
img = Image()
img.load(raw)
resized, nwidth, nheight = fit_image(img.size[0], img.size[1], max_width, max_height)
if resized:
img.size = (nwidth, nheight)
base, ext = os.path.splitext(base)
base = base + '-%dx%d%s' % (max_width, max_height, ext)
raw = img.export(ext[1:])
return raw, base, resized
示例8: to_png
# 需要导入模块: from calibre.utils.magick import Image [as 别名]
# 或者: from calibre.utils.magick.Image import export [as 别名]
def to_png(bmp):
# ImageMagick does not convert some bmp files correctly, while Qt does,
# so try Qt first. See for instance:
# https://bugs.launchpad.net/calibre/+bug/934167
# ImageMagick bug report:
# http://www.imagemagick.org/discourse-server/viewtopic.php?f=3&t=20350
from PyQt5.Qt import QImage, QByteArray, QBuffer
i = QImage()
if i.loadFromData(bmp):
ba = QByteArray()
buf = QBuffer(ba)
buf.open(QBuffer.WriteOnly)
i.save(buf, 'png')
return bytes(ba.data())
from calibre.utils.magick import Image
img = Image()
img.load(bmp)
return img.export('png')
示例9: browse_icon
# 需要导入模块: from calibre.utils.magick import Image [as 别名]
# 或者: from calibre.utils.magick.Image import export [as 别名]
def browse_icon(self, name='blank.png'):
cherrypy.response.headers['Content-Type'] = 'image/png'
cherrypy.response.headers['Last-Modified'] = self.last_modified(self.build_time)
if not hasattr(self, '__browse_icon_cache__'):
self.__browse_icon_cache__ = {}
if name not in self.__browse_icon_cache__:
try:
data = I(name, data=True)
except:
raise cherrypy.HTTPError(404, 'no icon named: %r'%name)
img = Image()
img.load(data)
width, height = img.size
scaled, width, height = fit_image(width, height, 48, 48)
if scaled:
img.size = (width, height)
self.__browse_icon_cache__[name] = img.export('png')
return self.__browse_icon_cache__[name]
示例10: rescale_image
# 需要导入模块: from calibre.utils.magick import Image [as 别名]
# 或者: from calibre.utils.magick.Image import export [as 别名]
def rescale_image(self, data):
orig_w, orig_h, ifmt = identify_data(data)
orig_data = data # save it in case compression fails
if self.scale_news_images is not None:
wmax, hmax = self.scale_news_images
scale, new_w, new_h = fit_image(orig_w, orig_h, wmax, hmax)
if scale:
data = thumbnail(data, new_w, new_h, compression_quality=95)[-1]
orig_w = new_w
orig_h = new_h
if self.compress_news_images_max_size is None:
if self.compress_news_images_auto_size is None: # not compressing
return data
else:
maxsizeb = (orig_w * orig_h)/self.compress_news_images_auto_size
else:
maxsizeb = self.compress_news_images_max_size * 1024
scaled_data = data # save it in case compression fails
if len(scaled_data) <= maxsizeb: # no compression required
return scaled_data
img = Image()
quality = 95
img.load(data)
while len(data) >= maxsizeb and quality >= 5:
quality -= 5
img.set_compression_quality(quality)
data = img.export('jpg')
if len(data) >= len(scaled_data): # compression failed
return orig_data if len(orig_data) <= len(scaled_data) else scaled_data
if len(data) >= len(orig_data): # no improvement
return orig_data
return data
示例11: do_all
# 需要导入模块: from calibre.utils.magick import Image [as 别名]
# 或者: from calibre.utils.magick.Image import export [as 别名]
def do_all(self):
cache = self.db.new_api
args = self.args
# Title and authors
if args.do_swap_ta:
title_map = cache.all_field_for('title', self.ids)
authors_map = cache.all_field_for('authors', self.ids)
def new_title(authors):
ans = authors_to_string(authors)
return titlecase(ans) if args.do_title_case else ans
new_title_map = {bid:new_title(authors) for bid, authors in authors_map.iteritems()}
new_authors_map = {bid:string_to_authors(title) for bid, title in title_map.iteritems()}
cache.set_field('authors', new_authors_map)
cache.set_field('title', new_title_map)
if args.do_title_case and not args.do_swap_ta:
title_map = cache.all_field_for('title', self.ids)
cache.set_field('title', {bid:titlecase(title) for bid, title in title_map.iteritems()})
if args.do_title_sort:
lang_map = cache.all_field_for('languages', self.ids)
title_map = cache.all_field_for('title', self.ids)
def get_sort(book_id):
if args.languages:
lang = args.languages[0]
else:
try:
lang = lang_map[book_id][0]
except (KeyError, IndexError, TypeError, AttributeError):
lang = 'eng'
return title_sort(title_map[book_id], lang=lang)
cache.set_field('sort', {bid:get_sort(bid) for bid in self.ids})
if args.au:
authors = string_to_authors(args.au)
cache.set_field('authors', {bid:authors for bid in self.ids})
if args.do_auto_author:
aus_map = cache.author_sort_strings_for_books(self.ids)
cache.set_field('author_sort', {book_id:' & '.join(aus_map[book_id]) for book_id in aus_map})
if args.aus and args.do_aus:
cache.set_field('author_sort', {bid:args.aus for bid in self.ids})
# Covers
if args.cover_action == 'remove':
cache.set_cover({bid:None for bid in self.ids})
elif args.cover_action == 'generate':
from calibre.ebooks import calibre_cover
from calibre.ebooks.metadata import fmt_sidx
from calibre.gui2 import config
for book_id in self.ids:
mi = self.db.get_metadata(book_id, index_is_id=True)
series_string = None
if mi.series:
series_string = _('Book %(sidx)s of %(series)s')%dict(
sidx=fmt_sidx(mi.series_index,
use_roman=config['use_roman_numerals_for_series_number']),
series=mi.series)
cdata = calibre_cover(mi.title, mi.format_field('authors')[-1],
series_string=series_string)
cache.set_cover({book_id:cdata})
elif args.cover_action == 'fromfmt':
for book_id in self.ids:
fmts = cache.formats(book_id, verify_formats=False)
if fmts:
covers = []
for fmt in fmts:
fmtf = cache.format(book_id, fmt, as_file=True)
if fmtf is None:
continue
cdata, area = get_cover_data(fmtf, fmt)
if cdata:
covers.append((cdata, area))
covers.sort(key=lambda x: x[1])
if covers:
cache.set_cover({book_id:covers[-1][0]})
elif args.cover_action == 'trim':
from calibre.utils.magick import Image
for book_id in self.ids:
cdata = cache.cover(book_id)
if cdata:
im = Image()
im.load(cdata)
im.trim(tweaks['cover_trim_fuzz_value'])
cdata = im.export('jpg')
cache.set_cover({book_id:cdata})
elif args.cover_action == 'clone':
cdata = None
for book_id in self.ids:
cdata = cache.cover(book_id)
if cdata:
break
if cdata:
cache.set_cover({bid:cdata for bid in self.ids if bid != book_id})
# Formats
if args.do_remove_format:
#.........这里部分代码省略.........
示例12: process_images
# 需要导入模块: from calibre.utils.magick import Image [as 别名]
# 或者: from calibre.utils.magick.Image import export [as 别名]
def process_images(self, soup, baseurl):
diskpath = unicode_path(os.path.join(self.current_dir, 'images'))
if not os.path.exists(diskpath):
os.mkdir(diskpath)
c = 0
for tag in soup.findAll(lambda tag: tag.name.lower()=='img' and tag.has_key('src')):
iurl = tag['src']
if iurl.startswith('data:image/'):
try:
data = b64decode(iurl.partition(',')[-1])
except:
self.log.exception('Failed to decode embedded image')
continue
else:
if callable(self.image_url_processor):
iurl = self.image_url_processor(baseurl, iurl)
if not urlparse.urlsplit(iurl).scheme:
iurl = urlparse.urljoin(baseurl, iurl, False)
with self.imagemap_lock:
if self.imagemap.has_key(iurl):
tag['src'] = self.imagemap[iurl]
continue
try:
data = self.fetch_url(iurl)
if data == 'GIF89a\x01':
# Skip empty GIF files as PIL errors on them anyway
continue
except Exception:
self.log.exception('Could not fetch image ', iurl)
continue
c += 1
fname = ascii_filename('img'+str(c))
if isinstance(fname, unicode):
fname = fname.encode('ascii', 'replace')
itype = imghdr.what(None, data)
if itype is None and b'<svg' in data[:1024]:
# SVG image
imgpath = os.path.join(diskpath, fname+'.svg')
with self.imagemap_lock:
self.imagemap[iurl] = imgpath
with open(imgpath, 'wb') as x:
x.write(data)
tag['src'] = imgpath
else:
try:
if itype not in {'png', 'jpg', 'jpeg'}:
itype == 'png' if itype == 'gif' else 'jpg'
im = Image()
im.load(data)
data = im.export(itype)
else:
identify_data(data)
imgpath = os.path.join(diskpath, fname+'.'+itype)
with self.imagemap_lock:
self.imagemap[iurl] = imgpath
with open(imgpath, 'wb') as x:
x.write(data)
tag['src'] = imgpath
except:
traceback.print_exc()
continue