def flip_images(raw): for match in re.finditer('<IMG[^>]+/?>', raw, flags=re.I): img = match.group() m = re.search(r'class="(x|y|xy)flip"', img) if m is None: continue flip = m.group(1) src = re.search(r'src="([^"]+)"', img) if src is None: continue img = src.group(1) if not os.path.exists(img): continue flip_image(img, flip) raw = re.sub(r'<STYLE.+?</STYLE>\s*', '', raw, flags=re.I | re.DOTALL) counter = 0 def add_alt(m): nonlocal counter counter += 1 return m.group(1).rstrip('/') + f' alt="Image {counter}"/>' raw = re.sub('(<IMG[^>]+)/?>', add_alt, raw, flags=re.I) return raw
def flip_images(raw): for match in re.finditer('<IMG[^>]+/?>', raw, flags=re.I): img = match.group() m = re.search(r'class="(x|y|xy)flip"', img) if m is None: continue flip = m.group(1) src = re.search(r'src="([^"]+)"', img) if src is None: continue img = src.group(1) if not os.path.exists(img): continue flip_image(img, flip) raw = re.sub(r'<STYLE.+?</STYLE>\s*', '', raw, flags=re.I | re.DOTALL) return raw
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 flip_images(raw): for match in re.finditer('<IMG[^>]+/?>', raw, flags=re.I): img = match.group() m = re.search(r'class="(x|y|xy)flip"', img) if m is None: continue flip = m.group(1) src = re.search(r'src="([^"]+)"', img) if src is None: continue img = src.group(1) if not os.path.exists(img): continue flip_image(img, flip) raw = re.sub(r'<STYLE.+?</STYLE>\s*', '', raw, flags=re.I|re.DOTALL) return raw
def flip(self, vertical=True): self.img = flip_image(self.img, horizontal=not vertical, vertical=vertical)