本文整理汇总了Python中calibre.utils.magick.Image类的典型用法代码示例。如果您正苦于以下问题:Python Image类的具体用法?Python Image怎么用?Python Image使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Image类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_cover
def create_cover(report, icons=(), cols=5, size=60, padding=8):
icons = icons or tuple(default_cover_icons(cols))
rows = int(math.ceil(len(icons) / cols))
canvas = create_canvas(cols * (size + padding), rows * (size + padding), '#eeeeee')
y = -size - padding // 2
x = 0
for i, icon in enumerate(icons):
if i % cols == 0:
y += padding + size
x = padding // 2
else:
x += size + padding
if report and icon in report.name_map:
ipath = os.path.join(report.path, report.name_map[icon])
else:
ipath = I(icon, allow_user_override=False)
img = Image()
with open(ipath, 'rb') as f:
img.load(f.read())
scaled, nwidth, nheight = fit_image(img.size[0], img.size[1], size, size)
img.size = nwidth, nheight
dx = (size - nwidth) // 2
canvas.compose(img, x + dx, y)
return canvas.export('JPEG')
示例2: browse_icon
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: browse_icon
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]
示例4: _data_to_image
def _data_to_image(data):
if isinstance(data, Image):
img = data
else:
img = Image()
img.load(data)
return img
示例5: add_borders_to_image
def add_borders_to_image(img_data, left=0, top=0, right=0, bottom=0, border_color="#ffffff", fmt="jpg"):
img = Image()
img.load(img_data)
lwidth, lheight = img.size
canvas = create_canvas(lwidth + left + right, lheight + top + bottom, border_color)
canvas.compose(img, left, top)
return canvas.export(fmt)
示例6: convert_image
def convert_image(url, data, sizes, grayscale, removetrans, imgtype="jpg", background="#ffffff"):
export = False
img = Image.open(StringIO(data))
owidth, oheight = img.size
nwidth, nheight = sizes
scaled, nwidth, nheight = fit_image(owidth, oheight, nwidth, nheight)
if scaled:
img = img.resize((nwidth, nheight), Image.ANTIALIAS)
export = True
if normalize_format_name(img.format) != imgtype:
if img.mode == "P":
# convert pallete gifs to RGB so jpg save doesn't fail.
img = img.convert("RGB")
export = True
if removetrans and img.mode == "RGBA":
background = Image.new("RGBA", img.size, background)
# Paste the image on top of the background
background.paste(img, img)
img = background.convert("RGB")
export = True
if grayscale and img.mode != "L":
img = img.convert("L")
export = True
if export:
outsio = StringIO()
img.save(outsio, convtype[imgtype])
return (outsio.getvalue(), imgtype, imagetypes[imgtype])
else:
logger.debug("image used unchanged")
return (data, imgtype, imagetypes[imgtype])
示例7: identify_data
def identify_data(data):
'''
Identify the image in data. Returns a 3-tuple
(width, height, format)
or raises an Exception if data is not an image.
'''
img = Image()
return img.identify(data)
示例8: resize_image
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
示例9: check_raster_images
def check_raster_images(name, mt, raw):
errors = []
i = Image()
try:
i.load(raw)
except Exception as e:
errors.append(InvalidImage(as_unicode(e.message), name))
else:
if i.colorspace == 'CMYKColorspace':
errors.append(CMYKImage(_('Image is in the CMYK colorspace'), name))
return errors
示例10: flip_image
def flip_image(img, flip):
from calibre.utils.magick import Image
im = Image()
im.open(img)
if b'x' in flip:
im.flip(True)
if b'y' in flip:
im.flip()
im.save(img)
示例11: qimage_to_magick
def qimage_to_magick(img):
ans = Image()
fmt = get_pixel_map()
if not img.hasAlphaChannel():
if img.format() != img.Format_RGB32:
img = img.convertToFormat(QImage.Format_RGB32)
fmt = fmt.replace('A', 'P')
else:
if img.format() != img.Format_ARGB32:
img = img.convertToFormat(QImage.Format_ARGB32)
raw = img.constBits().ascapsule()
ans.constitute(img.width(), img.height(), fmt, raw)
return ans
示例12: identify_data
def identify_data(data):
'''
Identify the image in data. Returns a 3-tuple
(width, height, format)
or raises an Exception if data is not an image.
'''
img = Image()
if hasattr(img, 'identify'):
img.identify(data)
else:
img.load(data)
width, height = img.size
fmt = img.format
return (width, height, fmt)
示例13: identify_data
def identify_data(data):
"""
Identify the image in data. Returns a 3-tuple
(width, height, format)
or raises an Exception if data is not an image.
"""
if data.startswith(b"<?xml"):
# ImageMagick segfaults when trying to identify SVG images
raise ValueError("Identifying svg images is not supported")
img = Image()
img.identify(data)
width, height = img.size
fmt = img.format
return (width, height, fmt)
示例14: create_cover_page
def create_cover_page(
top_lines,
logo_path,
width=590,
height=750,
bgcolor="#ffffff",
output_format="jpg",
texture_data=None,
texture_opacity=1.0,
):
"""
Create the standard calibre cover page and return it as a byte string in
the specified output_format.
"""
canvas = create_canvas(width, height, bgcolor)
if texture_data and hasattr(canvas, "texture"):
texture = Image()
texture.load(texture_data)
texture.set_opacity(texture_opacity)
canvas.texture(texture)
bottom = 10
for line in top_lines:
twand = create_text_wand(line.font_size, font_path=line.font_path)
bottom = draw_centered_text(canvas, twand, line.text, bottom)
bottom += line.bottom_margin
bottom -= top_lines[-1].bottom_margin
foot_font = P("fonts/liberation/LiberationMono-Regular.ttf")
vanity = create_text_arc(__appname__ + " " + __version__, 24, font=foot_font, bgcolor="#00000000")
lwidth, lheight = vanity.size
left = int(max(0, (width - lwidth) / 2.0))
top = height - lheight - 10
canvas.compose(vanity, left, top)
available = (width, int(top - bottom) - 20)
if available[1] > 40:
logo = Image()
logo.open(logo_path)
lwidth, lheight = logo.size
scaled, lwidth, lheight = fit_image(lwidth, lheight, *available)
if scaled:
logo.size = (lwidth, lheight)
left = int(max(0, (width - lwidth) / 2.0))
top = bottom + 10
extra = int((available[1] - lheight) / 2.0)
if extra > 0:
top += extra
canvas.compose(logo, left, top)
return canvas.export(output_format)
示例15: identify_data
def identify_data(data):
'''
Identify the image in data. Returns a 3-tuple
(width, height, format)
or raises an Exception if data is not an image.
'''
if data.startswith(b'<?xml'):
# ImageMagick segfaults when trying to identify SVG images
raise ValueError('Identifying svg images is not supported')
img = Image()
if hasattr(img, 'identify'):
img.identify(data)
else:
img.load(data)
width, height = img.size
fmt = img.format
return (width, height, fmt)