我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用PIL.ImageFile.ImageFile()。
def DQT(self, marker): # # Define quantization table. Support baseline 8-bit tables # only. Note that there might be more than one table in # each marker. # FIXME: The quantization tables can be used to estimate the # compression quality. n = i16(self.fp.read(2))-2 s = ImageFile._safe_read(self.fp, n) while len(s): if len(s) < 65: raise SyntaxError("bad quantization table marker") v = i8(s[0]) if v//16 == 0: self.quantization[v & 15] = array.array("b", s[1:65]) s = s[65:] else: return # FIXME: add code to read 16-bit tables! # raise SyntaxError, "bad quantization table element size" # # JPEG marker table
def _save(im, fp, filename): if im.mode != "1": raise IOError("cannot write mode %s as XBM" % im.mode) fp.write(("#define im_width %d\n" % im.size[0]).encode('ascii')) fp.write(("#define im_height %d\n" % im.size[1]).encode('ascii')) hotspot = im.encoderinfo.get("hotspot") if hotspot: fp.write(("#define im_x_hot %d\n" % hotspot[0]).encode('ascii')) fp.write(("#define im_y_hot %d\n" % hotspot[1]).encode('ascii')) fp.write(b"static char im_bits[] = {\n") ImageFile._save(im, fp, [("xbm", (0, 0)+im.size, 0, None)]) fp.write(b"};\n")
def _save(im, fp, filename): if im.mode[0] != "F": im = im.convert('F') hdr = makeSpiderHeader(im) if len(hdr) < 256: raise IOError("Error creating Spider header") # write the SPIDER header try: fp = open(filename, 'wb') except: raise IOError("Unable to open %s for writing" % filename) fp.writelines(hdr) rawmode = "F;32NF" # 32-bit native floating point ImageFile._save(im, fp, [("raw", (0, 0)+im.size, 0, (rawmode, 0, 1))]) fp.close()
def chunk_tRNS(self, pos, length): # transparency s = ImageFile._safe_read(self.fp, length) if self.im_mode == "P": if _simple_palette.match(s): i = s.find(b"\0") if i >= 0: self.im_info["transparency"] = i elif _null_palette.match(s): self.im_info["transparency"] = 0 else: self.im_info["transparency"] = s elif self.im_mode == "L": self.im_info["transparency"] = i16(s) elif self.im_mode == "RGB": self.im_info["transparency"] = i16(s), i16(s[2:]), i16(s[4:]) return s
def DQT(self, marker): # # Define quantization table. Support baseline 8-bit tables # only. Note that there might be more than one table in # each marker. # FIXME: The quantization tables can be used to estimate the # compression quality. n = i16(self.fp.read(2))-2 s = ImageFile._safe_read(self.fp, n) while len(s): if len(s) < 65: raise SyntaxError("bad quantization table marker") v = i8(s[0]) if v//16 == 0: self.quantization[v & 15] = array.array("B", s[1:65]) s = s[65:] else: return # FIXME: add code to read 16-bit tables! # raise SyntaxError, "bad quantization table element size" # # JPEG marker table
def crc(self, cid, data): "Read and verify checksum" # Skip CRC checks for ancillary chunks if allowed to load truncated images # 5th byte of first char is 1 [specs, section 5.4] if ImageFile.LOAD_TRUNCATED_IMAGES and (i8(cid[0]) >> 5 & 1): self.crc_skip(cid, data) return try: crc1 = Image.core.crc32(data, Image.core.crc32(cid)) crc2 = i16(self.fp.read(2)), i16(self.fp.read(2)) if crc1 != crc2: raise SyntaxError("broken PNG file (bad header checksum in %s)" % cid) except struct.error: raise SyntaxError("broken PNG file (incomplete checksum in %s)" % cid)
def chunk_tRNS(self, pos, length): # transparency s = ImageFile._safe_read(self.fp, length) if self.im_mode == "P": if _simple_palette.match(s): # tRNS contains only one full-transparent entry, # other entries are full opaque i = s.find(b"\0") if i >= 0: self.im_info["transparency"] = i else: # otherwise, we have a byte string with one alpha value # for each palette entry self.im_info["transparency"] = s elif self.im_mode == "L": self.im_info["transparency"] = i16(s) elif self.im_mode == "RGB": self.im_info["transparency"] = i16(s), i16(s[2:]), i16(s[4:]) return s
def chunk_tEXt(self, pos, length): # text s = ImageFile._safe_read(self.fp, length) try: k, v = s.split(b"\0", 1) except ValueError: # fallback for broken tEXt tags k = s v = b"" if k: if bytes is not str: k = k.decode('latin-1', 'strict') v = v.decode('latin-1', 'replace') self.im_info[k] = self.im_text[k] = v self.check_text_memory(len(v)) return s
def verify(self, endchunk=b"IEND"): # Simple approach; just calculate checksum for all remaining # blocks. Must be called directly after open. cids = [] while True: try: cid, pos, length = self.read() except struct.error: raise IOError("truncated PNG file") if cid == endchunk: break self.crc(cid, ImageFile._safe_read(self.fp, length)) cids.append(cid) return cids
def crc(self, cid, data): "Read and verify checksum" # Skip CRC checks for ancillary chunks if allowed to load truncated images # 5th byte of first char is 1 [specs, section 5.4] if ImageFile.LOAD_TRUNCATED_IMAGES and (i8(cid[0]) >> 5 & 1): self.crc_skip(cid, data) return try: crc1 = Image.core.crc32(data, Image.core.crc32(cid)) crc2 = i16(self.fp.read(2)), i16(self.fp.read(2)) if crc1 != crc2: raise SyntaxError("broken PNG file (bad header checksum in %r)" % cid) except struct.error: raise SyntaxError("broken PNG file (incomplete checksum in %r)" % cid)
def Skip(self, marker): n = i16(self.fp.read(2))-2 ImageFile._safe_read(self.fp, n)
def COM(self, marker): # # Comment marker. Store these in the APP dictionary. n = i16(self.fp.read(2))-2 s = ImageFile._safe_read(self.fp, n) self.app["COM"] = s # compatibility self.applist.append(("COM", s))
def load(self): if not self.fp: self.fp = self.ole.openstream(self.stream[:2] + ["Subimage 0000 Data"]) ImageFile.ImageFile.load(self) # # --------------------------------------------------------------------
def _save(im, fp, filename, check=0): try: image_type, rawmode = SAVE[im.mode] except KeyError: raise ValueError("Cannot save %s images as IM" % im.mode) try: frames = im.encoderinfo["frames"] except KeyError: frames = 1 if check: return check fp.write(("Image type: %s image\r\n" % image_type).encode('ascii')) if filename: fp.write(("Name: %s\r\n" % filename).encode('ascii')) fp.write(("Image size (x*y): %d*%d\r\n" % im.size).encode('ascii')) fp.write(("File size (no of images): %d\r\n" % frames).encode('ascii')) if im.mode == "P": fp.write(b"Lut: 1\r\n") fp.write(b"\000" * (511-fp.tell()) + b"\032") if im.mode == "P": fp.write(im.im.getpalette("RGB", "RGB;L")) # 768 bytes ImageFile._save(im, fp, [("raw", (0, 0)+im.size, 0, (rawmode, 0, -1))]) # # -------------------------------------------------------------------- # Registry
def verify(self, endchunk=b"IEND"): # Simple approach; just calculate checksum for all remaining # blocks. Must be called directly after open. cids = [] while True: cid, pos, length = self.read() if cid == endchunk: break self.crc(cid, ImageFile._safe_read(self.fp, length)) cids.append(cid) return cids
def chunk_IHDR(self, pos, length): # image header s = ImageFile._safe_read(self.fp, length) self.im_size = i32(s), i32(s[4:]) try: self.im_mode, self.im_rawmode = _MODES[(i8(s[8]), i8(s[9]))] except: pass if i8(s[12]): self.im_info["interlace"] = 1 if i8(s[11]): raise SyntaxError("unknown filter category") return s
def chunk_PLTE(self, pos, length): # palette s = ImageFile._safe_read(self.fp, length) if self.im_mode == "P": self.im_palette = "RGB", s return s
def chunk_gAMA(self, pos, length): # gamma setting s = ImageFile._safe_read(self.fp, length) self.im_info["gamma"] = i32(s) / 100000.0 return s
def chunk_pHYs(self, pos, length): # pixels per unit s = ImageFile._safe_read(self.fp, length) px, py = i32(s), i32(s[4:]) unit = i8(s[8]) if unit == 1: # meter dpi = int(px * 0.0254 + 0.5), int(py * 0.0254 + 0.5) self.im_info["dpi"] = dpi elif unit == 0: self.im_info["aspect"] = px, py return s
def chunk_zTXt(self, pos, length): # compressed text s = ImageFile._safe_read(self.fp, length) try: k, v = s.split(b"\0", 1) except ValueError: k = s v = b"" if v: comp_method = i8(v[0]) else: comp_method = 0 if comp_method != 0: raise SyntaxError("Unknown compression method %s in zTXt chunk" % comp_method) try: v = _safe_zlib_decompress(v[1:]) except zlib.error: v = b"" if k: if bytes is not str: k = k.decode('latin-1', 'strict') v = v.decode('latin-1', 'replace') self.im_info[k] = self.im_text[k] = v self.check_text_memory(len(v)) return s