示例#1
0
 def read_image_data(self, fname, base=None):
     if fname.startswith('file://'):
         src = fname[len('file://'):]
         if iswindows and src and src[0] == '/':
             src = src[1:]
         if not src or not os.path.exists(src):
             raise LinkedImageNotFound(src)
         with open(src, 'rb') as rawsrc:
             raw = rawsrc.read()
     else:
         try:
             raw = self.docx.read(fname)
         except KeyError:
             raise LinkedImageNotFound(fname)
     base = base or image_filename(fname.rpartition('/')[-1]) or 'image'
     ext = what(None, raw) or base.rpartition('.')[-1] or 'jpeg'
     if ext == 'emf':
         # For an example, see: https://bugs.launchpad.net/bugs/1224849
         self.log('Found an EMF image: %s, trying to extract embedded raster image' % fname)
         from calibre.utils.wmf.emf import emf_unwrap
         try:
             raw = emf_unwrap(raw)
         except Exception:
             self.log.exception('Failed to extract embedded raster image from EMF')
         else:
             ext = 'png'
     base = base.rpartition('.')[0]
     if not base:
         base = 'image'
     base += '.' + ext
     return raw, base
示例#2
0
 def read_image_data(self, fname, base=None):
     if fname.startswith('file://'):
         src = fname[len('file://'):]
         if iswindows and src and src[0] == '/':
             src = src[1:]
         if not src or not os.path.exists(src):
             raise LinkedImageNotFound(src)
         with open(src, 'rb') as rawsrc:
             raw = rawsrc.read()
     else:
         try:
             raw = self.docx.read(fname)
         except KeyError:
             raise LinkedImageNotFound(fname)
     base = base or image_filename(fname.rpartition('/')[-1]) or 'image'
     ext = what(None, raw) or base.rpartition('.')[-1] or 'jpeg'
     if ext == 'emf':
         # For an example, see: https://bugs.launchpad.net/bugs/1224849
         self.log('Found an EMF image: %s, trying to extract embedded raster image' % fname)
         from calibre.utils.wmf.emf import emf_unwrap
         try:
             raw = emf_unwrap(raw)
         except Exception:
             self.log.exception('Failed to extract embedded raster image from EMF')
         else:
             ext = 'png'
     base = base.rpartition('.')[0]
     if not base:
         base = 'image'
     base += '.' + ext
     return raw, base
示例#3
0
    def generate_filename(self, rid, base=None, rid_map=None):
        rid_map = self.rid_map if rid_map is None else rid_map
        fname = rid_map[rid]
        if fname in self.used:
            return self.used[fname]
        raw = self.docx.read(fname)
        base = base or ascii_filename(rid_map[rid].rpartition('/')[-1]).replace(' ', '_') or 'image'
        ext = what(None, raw) or base.rpartition('.')[-1] or 'jpeg'
        if ext == 'emf':
            # For an example, see: https://bugs.launchpad.net/bugs/1224849
            self.log('Found an EMF image: %s, trying to extract embedded raster image' % base)
            from calibre.utils.wmf.emf import emf_unwrap
            try:
                raw = emf_unwrap(raw)
            except Exception as e:
                self.log.exception('Failed to extract embedded raster image from EMF')
            else:
                ext = 'png'

        base = base.rpartition('.')[0]
        if not base:
            base = 'image'
        base += '.' + ext
        exists = frozenset(self.used.itervalues())
        c = 1
        name = base
        while name in exists:
            n, e = base.rpartition('.')[0::2]
            name = '%s-%d.%s' % (n, c, e)
            c += 1
        self.used[fname] = name
        with open(os.path.join(self.dest_dir, name), 'wb') as f:
            f.write(raw)
        self.all_images.add('images/' + name)
        return name
示例#4
0
文件: images.py 项目: botmtl/calibre
    def read_image_data(self, fname, base=None):
        if fname.startswith("file://"):
            src = fname[len("file://") :]
            if iswindows and src and src[0] == "/":
                src = src[1:]
            if not src or not os.path.exists(src):
                raise LinkedImageNotFound(src)
            with open(src, "rb") as rawsrc:
                raw = rawsrc.read()
        else:
            raw = self.docx.read(fname)
        base = base or ascii_filename(fname.rpartition("/")[-1]).replace(" ", "_") or "image"
        ext = what(None, raw) or base.rpartition(".")[-1] or "jpeg"
        if ext == "emf":
            # For an example, see: https://bugs.launchpad.net/bugs/1224849
            self.log("Found an EMF image: %s, trying to extract embedded raster image" % fname)
            from calibre.utils.wmf.emf import emf_unwrap

            try:
                raw = emf_unwrap(raw)
            except Exception:
                self.log.exception("Failed to extract embedded raster image from EMF")
            else:
                ext = "png"
        base = base.rpartition(".")[0]
        if not base:
            base = "image"
        base += "." + ext
        return raw, base
示例#5
0
    def generate_filename(self, rid, base=None, rid_map=None):
        rid_map = self.rid_map if rid_map is None else rid_map
        fname = rid_map[rid]
        if fname in self.used:
            return self.used[fname]
        if fname.startswith('file://'):
            src = fname[len('file://'):]
            if iswindows and src and src[0] == '/':
                src = src[1:]
            if not src or not os.path.exists(src):
                raise LinkedImageNotFound(src)
            with open(src, 'rb') as rawsrc:
                raw = rawsrc.read()
        else:
            raw = self.docx.read(fname)
        base = base or ascii_filename(
            rid_map[rid].rpartition('/')[-1]).replace(' ', '_') or 'image'
        ext = what(None, raw) or base.rpartition('.')[-1] or 'jpeg'
        if ext == 'emf':
            # For an example, see: https://bugs.launchpad.net/bugs/1224849
            self.log(
                'Found an EMF image: %s, trying to extract embedded raster image'
                % base)
            from calibre.utils.wmf.emf import emf_unwrap
            try:
                raw = emf_unwrap(raw)
            except Exception as e:
                self.log.exception(
                    'Failed to extract embedded raster image from EMF')
            else:
                ext = 'png'

        base = base.rpartition('.')[0]
        if not base:
            base = 'image'
        base += '.' + ext
        exists = frozenset(self.used.itervalues())
        c = 1
        name = base
        while name in exists:
            n, e = base.rpartition('.')[0::2]
            name = '%s-%d.%s' % (n, c, e)
            c += 1
        self.used[fname] = name
        with open(os.path.join(self.dest_dir, name), 'wb') as f:
            f.write(raw)
        self.all_images.add('images/' + name)
        return name