123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- # coding:utf-8
- from PIL import Image
- import os
- def extract_image(xObject, base_dir, image_name):
- try:
- if xObject.attrs["Subtype"].name == 'Image':
- size = (xObject.attrs["Width"], xObject.attrs["Height"])
- data = xObject.get_data()
- if xObject.attrs["ColorSpace"].name == 'DeviceRGB':
- mode = "RGB"
- else:
- mode = "P"
- # 保存图片的文件名前缀
- img_pre = os.path.join(base_dir, image_name)
- if 'Filter' in xObject.attrs:
- if xObject.attrs["Filter"].name == 'FlateDecode':
- img = Image.frombytes(mode, size, data)
- img.save(img_pre + ".png")
- return img_pre + ".png"
- elif xObject.attrs["Filter"].name == 'DCTDecode':
- img = open(img_pre + ".jpg", "wb")
- img.write(data)
- img.close()
- return img_pre + ".jpg"
- elif xObject.attrs["Filter"].name == 'JPXDecode':
- img = open(img_pre + ".jp2", "wb")
- img.write(data)
- img.close()
- return img_pre + ".jp2"
- elif xObject.attrs["Filter"].name == 'CCITTFaxDecode':
- img = open(img_pre + ".tiff", "wb")
- img.write(data)
- img.close()
- return img_pre + ".tiff"
- else:
- img = Image.frombytes(mode, size, data)
- img.save(img_pre + ".png")
- return img_pre + ".png"
- except Exception as e:
- # print(f"图片提取失败-->{e}")
- return None
- class Picture(object):
- def __init__(self, min_y, max_y, height, width, image_path):
- self.min_y = min_y
- self.max_y = max_y
- self.width = width
- self.height = height
- self.image_path = image_path
- self.content = ""
|