def get_picture_size(self): from calibre.utils.magick import Image self.make_temp_cbz_file() zf = ZipFile(self.file) files = zf.namelist() size_x, size_y = 0, 0 index = 1 while index < 10 and index < len(files): fname = files[index] if fname.lower().rpartition('.')[-1] in IMG_EXTENSIONS: with zf.open(fname) as ffile: img = Image() try: img.open(ffile) size_x, size_y = img.size except: pass if size_x < size_y: break index += 1 zf.close() size = round(size_x * size_y / 1000000, 2) return size
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)
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)
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 = tweaks['generate_cover_foot_font'] if not foot_font: 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.)) 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.)) 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)
def postprocess_html(self, soup, first): #process all the images for tag in soup.findAll(lambda tag: tag.name.lower()=='img' and tag.has_key('src')): iurl = tag['src'] img = Image() img.open(iurl) if img < 0: raise RuntimeError('Out of memory') img.type = "GrayscaleType" img.save(iurl) return soup
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)
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])
def render(self): from calibre.utils.magick import Image img = Image() img.open(self.path_to_page) width, height = img.size if self.num == 0: # First image so create a thumbnail from it thumb = img.clone thumb.thumbnail(60, 80) thumb.save(os.path.join(self.dest, 'thumbnail.png')) self.pages = [img] if width > height: if self.opts.landscape: self.rotate = True else: split1, split2 = img.clone, img.clone half = int(width/2) split1.crop(half-1, height, 0, 0) split2.crop(half-1, height, half, 0) self.pages = [split2, split1] if self.opts.right2left else [split1, split2] self.process_pages()
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])
def create_cover_page(top_lines, bottom_lines, display_image, options, image_path, output_format='jpg'): from calibre.gui2 import ensure_app ensure_app() (width, height) = options.get(cfg.KEY_SIZE, (590, 750)) margins = options.get(cfg.KEY_MARGINS) (top_mgn, bottom_mgn, left_mgn, right_mgn, image_mgn) = ( margins['top'], margins['bottom'], margins['left'], margins['right'], margins['image']) left_mgn = min([left_mgn, (width / 2) - 10]) left_text_margin = left_mgn if left_mgn > 0 else 10 right_mgn = min([right_mgn, (width / 2) - 10]) right_text_margin = right_mgn if right_mgn > 0 else 10 colors = options[cfg.KEY_COLORS] bgcolor, border_color, fill_color, stroke_color = ( colors['background'], colors['border'], colors['fill'], colors['stroke']) if not options.get(cfg.KEY_COLOR_APPLY_STROKE, False): stroke_color = None auto_reduce_font = options.get(cfg.KEY_FONTS_AUTOREDUCED, False) borders = options[cfg.KEY_BORDERS] (cover_border_width, image_border_width) = ( borders['coverBorder'], borders['imageBorder']) is_background_image = options.get(cfg.KEY_BACKGROUND_IMAGE, False) if image_path: if not os.path.exists(image_path) or os.path.getsize(image_path) == 0: display_image = is_background_image = False canvas = create_canvas(width - cover_border_width * 2, height - cover_border_width * 2, bgcolor) if cover_border_width > 0: canvas = add_border(canvas, cover_border_width, border_color, bgcolor) if is_background_image: logo = Image() logo.open(image_path) outer_margin = 0 if cover_border_width == 0 else cover_border_width logo.size = (width - outer_margin * 2, height - outer_margin * 2) left = top = outer_margin canvas.compose(logo, int(left), int(top)) top = top_mgn if len(top_lines) > 0: for line in top_lines: twand = create_colored_text_wand(line, fill_color, stroke_color) top = draw_sized_text( canvas, twand, line, top, left_text_margin, right_text_margin, auto_reduce_font) top += line.bottom_margin top -= top_lines[-1].bottom_margin if len(bottom_lines) > 0: # Draw this on a fake canvas so can determine the space required fake_canvas = create_canvas(width, height, bgcolor) footer_height = 0 for line in bottom_lines: line.twand = create_colored_text_wand( line, fill_color, stroke_color) footer_height = draw_sized_text( fake_canvas, line.twand, line, footer_height, left_text_margin, right_text_margin, auto_reduce_font) footer_height += line.bottom_margin footer_height -= bottom_lines[-1].bottom_margin footer_top = height - footer_height - bottom_mgn bottom = footer_top # Re-use the text wand from previously which we will have adjusted the # font size on for line in bottom_lines: bottom = draw_sized_text( canvas, line.twand, line, bottom, left_text_margin, right_text_margin, auto_reduce_font=False) bottom += line.bottom_margin available = (width - (left_mgn + right_mgn), int(footer_top - top) - (image_mgn * 2)) else: available = (width - (left_mgn + right_mgn), int(height - top) - bottom_mgn - (image_mgn * 2)) if not is_background_image and display_image and available[1] > 40: logo = Image() logo.open(image_path) lwidth, lheight = logo.size available = (available[0] - image_border_width * 2, available[1] - image_border_width * 2) scaled, lwidth, lheight = fit_image(lwidth, lheight, *available) if not scaled and options.get(cfg.KEY_RESIZE_IMAGE_TO_FIT, False): scaled, lwidth, lheight = scaleup_image( lwidth, lheight, *available) if scaled: logo.size = (lwidth, lheight) if image_border_width > 0: logo = add_border(logo, image_border_width, border_color, bgcolor) left = int(max(0, (width - lwidth) / 2.)) top = top + image_mgn + ((available[1] - lheight) / 2.) canvas.compose(logo, int(left), int(top)) return canvas.export(output_format)
def get_image_size(image_path): logo = Image() logo.open(image_path) return logo.size