def invert_image(image_path): from PIL import Image from fitz import Pixmap pix = Pixmap(image_path) pix.invertIRect(pix.irect) buffer = BytesIO(pix.getImageData()) del pix return Image.open(buffer)
def to_raw_dict(cls, image:fitz.Pixmap, bbox:fitz.Rect): '''Store Pixmap to raw dict.''' return { 'type': BlockType.IMAGE.value, 'bbox': tuple(bbox), 'ext': 'png', 'width': image.width, 'height': image.height, 'image': image.getPNGData() }
def _pixmap_to_cv_image(pixmap: fitz.Pixmap): '''Convert fitz Pixmap to opencv image. Args: pixmap (fitz.Pixmap): PyMuPDF Pixmap. ''' import cv2 as cv import numpy as np img_byte = pixmap.tobytes() return cv.imdecode(np.frombuffer(img_byte, np.uint8), cv.IMREAD_COLOR)
def manga_to_PDF(dir_path, one_file=True, width=None, height=None): if dir_path[-1] == '\\' or dir_path[-1] == '/': dir_path = dir_path[0:-1] if not os.path.isdir(dir_path): raise ValueError('传入的路径并非文件夹') from fitz import Document, Pixmap, Rect from glob import _iglob as glob if one_file: title = os.path.basename(dir_path) with Document() as doc: for file_path in glob(os.path.join(dir_path, "*", "*.jpg"), False, False): pixmap = Pixmap(file_path) if width and height: pixmap = Pixmap(pixmap, width, height, None) elif width: pixmap = Pixmap(pixmap, width, int(pixmap.height / pixmap.width * width), None) elif height: pixmap = Pixmap(pixmap, int(pixmap.width / pixmap.height * height), height, None) rect = Rect(0, 0, pixmap.width, pixmap.height) page = doc.newPage(width=pixmap.width, height=pixmap.height) page.insertImage(rect, pixmap=pixmap) doc.save(os.path.join(dir_path, title + ".pdf"), deflate=True) else: for chap in glob(os.path.join(dir_path, "*"), False, True): title = os.path.basename(chap) with Document() as doc: for file_path in glob(os.path.join(chap, "*.jpg"), False, False): pixmap = Pixmap(file_path) if width and height: pixmap = Pixmap(pixmap, width, height, None) elif width: pixmap = Pixmap( pixmap, width, int(pixmap.height / pixmap.width * width), None) elif height: pixmap = Pixmap( pixmap, int(pixmap.width / pixmap.height * height), height, None) rect = Rect(0, 0, pixmap.width, pixmap.height) page = doc.newPage(width=pixmap.width, height=pixmap.height) page.insertImage(rect, pixmap=pixmap) doc.save(os.path.join(dir_path, title + ".pdf"), deflate=True)
def get_images(page, imblocks, page_index): im_list = [] i = 0 pagepix = page.getPixmap(page_index) pw = pagepix.width ph = pagepix.height # print("PH: {}\nPW: {}\n\n".format(ph, pw)) for block in imblocks: try: ending = block["ext"] pix = Pixmap(block["image"]) x0 = block["bbox"][0] y0 = block["bbox"][1] x1 = block["bbox"][2] y1 = block["bbox"][3] w = pix.width h = pix.height filename = "static/images/image_{}-{}.{}".format( page_index, i, ending) if is_relevant_image(pw, ph, pix, x0, y0, x1, y1): if pix.n < 5: # this is GRAY or RGB pix.writePNG(filename) else: # CMYK: convert to RGB first pix1 = fitz.Pixmap(fitz.csRGB, pix) pix1.writePNG(filename) pix1 = None pix = None if h > w: im_list.append((filename, "H")) else: im_list.append((filename, "W")) i += 1 except: pass finally: pass return im_list
def _to_raw_dict(image: fitz.Pixmap, bbox: fitz.Rect): '''Store Pixmap ``image`` to raw dict. Args: image (fitz.Pixmap): Pixmap to store. bbox (fitz.Rect): Boundary box the pixmap. Returns: dict: Raw dict of the pixmap. ''' return { 'type': BlockType.IMAGE.value, 'bbox': tuple(bbox), 'width': image.width, 'height': image.height, 'image': image.tobytes() }
def _to_raw_dict(cls, image: fitz.Pixmap, bbox: fitz.Rect): """Store Pixmap ``image`` to raw dict. Args: image (fitz.Pixmap): Pixmap to store. bbox (fitz.Rect): Boundary box the pixmap. Returns: dict: Raw dict of the pixmap. """ return { 'type': BlockType.IMAGE.value, 'bbox': tuple(bbox), 'ext': 'png', 'width': image.width, 'height': image.height, 'image': image.getPNGData() }