我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用PIL.ImageFile._save()。
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 _save_all(im, fp, filename): if not hasattr(im, "n_frames"): return _save(im, fp, filename) cur_idx = im.tell() try: with AppendingTiffWriter(fp) as tf: for idx in range(im.n_frames): im.seek(idx) im.load() _save(im, tf, filename) tf.newFrame() finally: im.seek(cur_idx) # # -------------------------------------------------------------------- # Register
def _save_spider(im, fp, filename): # get the filename extension and register it with Image ext = os.path.splitext(filename)[1] Image.register_extension("SPIDER", ext) _save(im, fp, filename) # --------------------------------------------------------------------
def _save_all(im, fp, filename): _save(im, fp, filename, save_all=True) ## # (Internal) Image save plugin for the PDF format.
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 getchunks(im, **params): """Return a list of PNG chunks representing this image.""" class collector(object): data = [] def write(self, data): pass def append(self, chunk): self.data.append(chunk) def append(fp, cid, *data): data = b"".join(data) hi, lo = Image.core.crc32(data, Image.core.crc32(cid)) crc = o16(hi) + o16(lo) fp.append((cid, data, crc)) fp = collector() try: im.encoderinfo = params _save(im, fp, None, append) finally: del im.encoderinfo return fp.data # -------------------------------------------------------------------- # Registry
def _save(im, fp, filename): if im.mode == "1": rawmode, head = "1;I", b"P4" elif im.mode == "L": rawmode, head = "L", b"P5" elif im.mode == "I": if im.getextrema()[1] < 2**16: rawmode, head = "I;16B", b"P5" else: rawmode, head = "I;32B", b"P5" elif im.mode == "RGB": rawmode, head = "RGB", b"P6" elif im.mode == "RGBA": rawmode, head = "RGB", b"P6" else: raise IOError("cannot write mode %s as PPM" % im.mode) fp.write(head + ("\n%d %d\n" % im.size).encode('ascii')) if head == b"P6": fp.write(b"255\n") if head == b"P5": if rawmode == "L": fp.write(b"255\n") elif rawmode == "I;16B": fp.write(b"65535\n") elif rawmode == "I;32B": fp.write(b"2147483648\n") ImageFile._save(im, fp, [("raw", (0, 0)+im.size, 0, (rawmode, 0, 1))]) # ALTERNATIVE: save via builtin debug function # im._dump(filename) # # --------------------------------------------------------------------
def _save(im, fp, filename): if im.mode != "1": raise IOError("cannot write mode %s as MSP" % im.mode) # create MSP header header = [0] * 16 header[0], header[1] = i16(b"Da"), i16(b"nM") # version 1 header[2], header[3] = im.size header[4], header[5] = 1, 1 header[6], header[7] = 1, 1 header[8], header[9] = im.size checksum = 0 for h in header: checksum = checksum ^ h header[12] = checksum # FIXME: is this the right field? # header for h in header: fp.write(o16(h)) # image body ImageFile._save(im, fp, [("raw", (0, 0)+im.size, 32, ("1", 0, 1))]) # # registry
def _save_spider(im, fp, filename): # get the filename extension and register it with Image ext = os.path.splitext(filename)[1] Image.register_extension(SpiderImageFile.format, ext) _save(im, fp, filename) # --------------------------------------------------------------------
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 fp.writelines(hdr) rawmode = "F;32NF" # 32-bit native floating point ImageFile._save(im, fp, [("raw", (0, 0)+im.size, 0, (rawmode, 0, 1))])