def flip_image(img, flip): from calibre.utils.img import flip_image, image_and_format_from_data, image_to_data with lopen(img, 'r+b') as f: img, fmt = image_and_format_from_data(f.read()) img = flip_image(img, horizontal='x' in flip, vertical='y' in flip) f.seek(0), f.truncate() f.write(image_to_data(img, fmt=fmt))
def convert_webp(self, item): from calibre.utils.img import image_and_format_from_data, image_to_data img, fmt = image_and_format_from_data(item.data) if fmt == 'webp' and not img.isNull(): self.log.info(f'Converting WebP image {item.href} to PNG') item.data = image_to_data(img, fmt='PNG') item.media_type = 'image/png'
def __init__(self, path_or_bytes): if not isinstance(path_or_bytes, bytes): with open(path_or_bytes, 'rb') as f: path_or_bytes = f.read() self.img_data = path_or_bytes fmt, width, height = identify(path_or_bytes) self.img, self.fmt = image_and_format_from_data(path_or_bytes) self.width, self.height = self.img.width(), self.img.height() self.cache_key = self.img.cacheKey()
def load(self, data): if not data: raise ValueError('No image data present') self.img, self.read_format = image_and_format_from_data(data)
def identify(self, data): img, fmt = image_and_format_from_data(data) return img.width(), img.height(), fmt
def load(self, data): if not data: raise ValueError('Cannot open image from empty data string') img, self.read_format = image_and_format_from_data(data) self.from_qimage(img)