pass def append(self, chunk): self.data.append(chunk) def append(fp, cid, *data): data = string.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 Image.register_open("PNG", PngImageFile, _accept) Image.register_save("PNG", _save) Image.register_extension("PNG", ".png") Image.register_mime("PNG", "image/png")
# -------------------------------------------------------------------- def _save(im, fp, filename): if im.mode == "1": rawmode, head = "1;I", "P4" elif im.mode == "L": rawmode, head = "L", "P5" elif im.mode == "RGB": rawmode, head = "RGB", "P6" elif im.mode == "RGBA": rawmode, head = "RGB", "P6" else: raise IOError, "cannot write mode %s as PPM" % im.mode fp.write(head + "\n%d %d\n" % im.size) if head != "P4": fp.write("255\n") ImageFile._save(im, fp, [("raw", (0,0)+im.size, 0, (rawmode, 0, 1))]) # ALTERNATIVE: save via builtin debug function # im._dump(filename) # # -------------------------------------------------------------------- Image.register_open("PPM", PpmImageFile, _accept) Image.register_save("PPM", _save) Image.register_extension("PPM", ".pbm") Image.register_extension("PPM", ".pgm") Image.register_extension("PPM", ".ppm")
if width <= 0 or height <= 0 or bytes != 1: raise SyntaxError, "not a GIMP brush" comment = self.fp.read(header_size - 20)[:-1] self.mode = "L" self.size = width, height self.info["comment"] = comment # Since the brush is so small, we read the data immediately self.data = self.fp.read(width * height) def load(self): if not self.data: return # create an image out of the brush data block self.im = Image.core.new(self.mode, self.size) self.im.fromstring(self.data) self.data = "" # # registry Image.register_open("GBR", GbrImageFile, _accept) Image.register_extension("GBR", ".gbr")
def _save(im, fp, filename): if im.mode == "1": rawmode, head = "1;I", "P4" elif im.mode == "L": rawmode, head = "L", "P5" elif im.mode == "RGB": rawmode, head = "RGB", "P6" elif im.mode == "RGBA": rawmode, head = "RGB", "P6" else: raise IOError, "cannot write mode %s as PPM" % im.mode fp.write(head + "\n%d %d\n" % im.size) if head != "P4": fp.write("255\n") ImageFile._save(im, fp, [("raw", (0, 0) + im.size, 0, (rawmode, 0, 1))]) # ALTERNATIVE: save via builtin debug function # im._dump(filename) # # -------------------------------------------------------------------- Image.register_open("PPM", PpmImageFile, _accept) Image.register_save("PPM", _save) Image.register_extension("PPM", ".pbm") Image.register_extension("PPM", ".pgm") Image.register_extension("PPM", ".ppm")
if not m: m = s elif ord(s[0]) > ord(m[0]) and ord(s[1]) > ord(m[1]): m = s #print "width", ord(s[0]) #print "height", ord(s[1]) #print "colors", ord(s[2]) #print "reserved", ord(s[3]) #print "planes", i16(s[4:]) #print "bitcount", i16(s[6:]) #print "bytes", i32(s[8:]) #print "offset", i32(s[12:]) # load as bitmap self._bitmap(i32(m[12:])) # patch up the bitmap height self.size = self.size[0], self.size[1] / 2 d, e, o, a = self.tile[0] self.tile[0] = d, (0, 0) + self.size, o, a return # # -------------------------------------------------------------------- Image.register_open("ICO", IcoImageFile, _accept) Image.register_extension("ICO", ".ico")
s = self.fp.read(4) if i32(s) != MAGIC: raise SyntaxError, "not a DCX file" # Component directory self._offset = [] for i in range(1024): offset = i32(self.fp.read(4)) if not offset: break self._offset.append(offset) self.__fp = self.fp self.seek(0) def seek(self, frame): if frame >= len(self._offset): raise EOFError("attempt to seek outside DCX directory") self.frame = frame self.fp = self.__fp self.fp.seek(self._offset[frame]) PcxImageFile._open(self) def tell(self): return self.frame Image.register_open("DCX", DcxImageFile, _accept) Image.register_extension("DCX", ".dcx")
s = self.fp.read(6) if s != "P7 332": raise SyntaxError, "not an XV thumbnail file" # Skip to beginning of next line self.fp.readline() # skip info comments while 1: s = self.fp.readline() if not s: raise SyntaxError, "Unexpected EOF reading XV thumbnail file" if s[0] != '#': break # parse header line (already read) s = string.split(s.strip()) self.mode = "P" self.size = int(s[0]), int(s[1]) self.palette = ImagePalette.raw("RGB", PALETTE) self.tile = [("raw", (0, 0) + self.size, self.fp.tell(), (self.mode, 0, 1))] # -------------------------------------------------------------------- Image.register_open("XVThumb", XVThumbImageFile)
self.tile = [("xbm", (0, 0)+self.size, m.end(), None)] 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]) fp.write("#define im_height %d\n" % im.size[1]) hotspot = im.encoderinfo.get("hotspot") if hotspot: fp.write("#define im_x_hot %d\n" % hotspot[0]) fp.write("#define im_y_hot %d\n" % hotspot[1]) fp.write("static char im_bits[] = {\n") ImageFile._save(im, fp, [("xbm", (0,0)+im.size, 0, None)]) fp.write("};\n") Image.register_open("XBM", XbmImageFile, _accept) Image.register_save("XBM", _save) Image.register_extension("XBM", ".xbm") Image.register_mime("XBM", "image/xbm")
if not _accept(self.fp.read(8)): raise SyntaxError("Not a GRIB file") self.fp.seek(offset) # make something up self.mode = "F" self.size = 1, 1 loader = self._load() if loader: loader.open(self) def _load(self): return _handler def _save(im, fp, filename): if _handler is None or not hasattr("_handler", "save"): raise IOError("GRIB save handler not installed") _handler.save(im, fp, filename) # -------------------------------------------------------------------- # Registry Image.register_open(GribStubImageFile.format, GribStubImageFile, _accept) Image.register_save(GribStubImageFile.format, _save) Image.register_extension(GribStubImageFile.format, ".grib")
self.mode = 'RGBA' self.size = self.icns.bestsize() self.info['sizes'] = self.icns.itersizes() # Just use this to see if it's loaded or not yet. self.tile = ('',) def load(self): Image.Image.load(self) if not self.tile: return self.load_prepare() # This is likely NOT the best way to do it, but whatever. im = self.icns.getimage(self.size) self.im = im.im self.mode = im.mode self.size = im.size self.fp = None self.icns = None self.tile = () self.load_end() Image.register_open("ICNS", IcnsImageFile, lambda x: x[:4] == 'icns') Image.register_extension("ICNS", '.icns') if __name__ == '__main__': import os, sys im = Image.open(open(sys.argv[1], "rb")) im.save("out.png") os.startfile("out.png")
if not m: m = s elif ord(s[0]) > ord(m[0]) and ord(s[1]) > ord(m[1]): m = s #print "width", ord(s[0]) #print "height", ord(s[1]) #print "colors", ord(s[2]) #print "reserved", ord(s[3]) #print "hotspot x", i16(s[4:]) #print "hotspot y", i16(s[6:]) #print "bytes", i32(s[8:]) #print "offset", i32(s[12:]) # load as bitmap self._bitmap(i32(m[12:]) + offset) # patch up the bitmap height self.size = self.size[0], self.size[1] / 2 d, e, o, a = self.tile[0] self.tile[0] = d, (0, 0) + self.size, o, a return # # -------------------------------------------------------------------- Image.register_open("CUR", CurImageFile, _accept) Image.register_extension("CUR", ".cur")
self.mode = "P" self.palette = ImagePalette.raw("RGB", string.join(palette, "")) self.tile = [("raw", (0, 0) + self.size, self.fp.tell(), ("P", 0, 1))] def load_read(self, bytes): # # load all image data in one chunk xsize, ysize = self.size s = [None] * ysize for i in range(ysize): s[i] = string.ljust(self.fp.readline()[1:xsize + 1], xsize) self.fp = None return string.join(s, "") # # Registry Image.register_open("XPM", XpmImageFile, _accept) Image.register_extension("XPM", ".xpm") Image.register_mime("XPM", "image/xpm")
print cid, bytes, "(unknown)" s = self.fp.read(bytes) self.arg.crc(cid, s) self.fp.read(4) # ship extra CRC def tell(self): return 0 def verify(self): "Verify ARG file" # back up to first chunk self.fp.seek(8) self.arg.verify(self) self.arg.close() self.fp = None # # -------------------------------------------------------------------- Image.register_open("ARG", ArgImageFile, _accept) Image.register_extension("ARG", ".arg") Image.register_mime("ARG", "video/x-arg")
if not _accept(self.fp.read(8)): raise SyntaxError("Not a BUFR file") self.fp.seek(offset) # make something up self.mode = "F" self.size = 1, 1 loader = self._load() if loader: loader.open(self) def _load(self): return _handler def _save(im, fp, filename): if _handler is None or not hasattr("_handler", "save"): raise IOError("BUFR save handler not installed") _handler.save(im, fp, filename) # -------------------------------------------------------------------- # Registry Image.register_open(BufrStubImageFile.format, BufrStubImageFile, _accept) Image.register_save(BufrStubImageFile.format, _save) Image.register_extension(BufrStubImageFile.format, ".bufr")
def append(self, chunk): self.data.append(chunk) def append(fp, cid, *data): data = string.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 Image.register_open("PNG", PngImageFile, _accept) Image.register_save("PNG", _save) Image.register_extension("PNG", ".png") Image.register_mime("PNG", "image/png")
self.tile = [("xbm", (0, 0) + self.size, m.end(), None)] 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]) fp.write("#define im_height %d\n" % im.size[1]) hotspot = im.encoderinfo.get("hotspot") if hotspot: fp.write("#define im_x_hot %d\n" % hotspot[0]) fp.write("#define im_y_hot %d\n" % hotspot[1]) fp.write("static char im_bits[] = {\n") ImageFile._save(im, fp, [("xbm", (0, 0) + im.size, 0, None)]) fp.write("};\n") Image.register_open("XBM", XbmImageFile, _accept) Image.register_save("XBM", _save) Image.register_extension("XBM", ".xbm") Image.register_mime("XBM", "image/xbm")
try: try: # fast self.im = Image.core.open_ppm(outfile) except: # slightly slower im = Image.open(outfile) im.load() self.im = im.im finally: try: os.unlink(outfile) except: pass Image.register_open("IPTC", IptcImageFile) Image.register_extension("IPTC", ".iim") ## # Get IPTC information from TIFF, JPEG, or IPTC file. # # @param im An image containing IPTC data. # @return A dictionary containing IPTC information, or None if # no IPTC information block was found. def getiptcinfo(im): import TiffImagePlugin, JpegImagePlugin import StringIO
else: raise SyntaxError("Unsupported file format") self.mode = "RGB" self.size = size loader = self._load() if loader: loader.open(self) def _load(self): return _handler def _save(im, fp, filename): if _handler is None or not hasattr("_handler", "save"): raise IOError("WMF save handler not installed") _handler.save(im, fp, filename) # # -------------------------------------------------------------------- # Registry stuff Image.register_open(WmfStubImageFile.format, WmfStubImageFile, _accept) Image.register_save(WmfStubImageFile.format, _save) Image.register_extension(WmfStubImageFile.format, ".wmf") Image.register_extension(WmfStubImageFile.format, ".emf")
# include SIMPLE, BITPIX, NAXIS, etc. self.fp.seek(offset) # make something up self.mode = "F" self.size = 1, 1 loader = self._load() if loader: loader.open(self) def _load(self): return _handler def _save(im, fp, filename): if _handler is None or not hasattr("_handler", "save"): raise IOError("FITS save handler not installed") _handler.save(im, fp, filename) # -------------------------------------------------------------------- # Registry Image.register_open(FITSStubImageFile.format, FITSStubImageFile, _accept) Image.register_save(FITSStubImageFile.format, _save) Image.register_extension(FITSStubImageFile.format, ".fit") Image.register_extension(FITSStubImageFile.format, ".fits")
if not m: m = s elif ord(s[0]) > ord(m[0]) and ord(s[1]) > ord(m[1]): m = s #print "width", ord(s[0]) #print "height", ord(s[1]) #print "colors", ord(s[2]) #print "reserved", ord(s[3]) #print "planes", i16(s[4:]) #print "bitcount", i16(s[6:]) #print "bytes", i32(s[8:]) #print "offset", i32(s[12:]) # load as bitmap self._bitmap(i32(m[12:])) # patch up the bitmap height self.size = self.size[0], self.size[1]/2 d, e, o, a = self.tile[0] self.tile[0] = d, (0,0)+self.size, o, a return # # -------------------------------------------------------------------- Image.register_open("ICO", IcoImageFile, _accept) Image.register_extension("ICO", ".ico")
self.info["dpi"] = xdpi, ydpi else: raise SyntaxError("Unsupported file format") self.mode = "RGB" self.size = size loader = self._load() if loader: loader.open(self) def _load(self): return _handler def _save(im, fp, filename): if _handler is None or not hasattr("_handler", "save"): raise IOError("WMF save handler not installed") _handler.save(im, fp, filename) # # -------------------------------------------------------------------- # Registry stuff Image.register_open(WmfStubImageFile.format, WmfStubImageFile, _accept) Image.register_save(WmfStubImageFile.format, _save) Image.register_extension(WmfStubImageFile.format, ".wmf") Image.register_extension(WmfStubImageFile.format, ".emf")
# read key/value pair # FIXME: dangerous, may read whole file s = s + self.fp.readline() if len(s) == 1 or len(s) > 100: break if s[0] == "*": continue # comment m = field.match(s) if not m: break k, v = m.group(1,2) if k == "width": xsize = int(v) self.size = xsize, ysize elif k == "height": ysize = int(v) self.size = xsize, ysize elif k == "pixel" and v == "n8": self.mode = "L" # # -------------------------------------------------------------------- Image.register_open("IMT", ImtImageFile) # # no extension registered (".im" is simply too common)
elif compression == 1: # # packbits compression i = 0 tile = [] bytecount = read(channels * ysize * 2) offset = file.tell() for channel in range(channels): layer = mode[channel] if mode == "CMYK": layer = layer + ";I" tile.append(("packbits", bbox, offset, layer)) for y in range(ysize): offset = offset + i16(bytecount[i:i + 2]) i = i + 2 file.seek(offset) if offset & 1: read(1) # padding return tile # -------------------------------------------------------------------- # registry Image.register_open("PSD", PsdImageFile, _accept) Image.register_extension("PSD", ".psd")
orientation = im.info.get("orientation", -1) if orientation > 0: flags = flags | 0x20 fp.write("\000" + chr(colormaptype) + chr(imagetype) + o16(colormapfirst) + o16(colormaplength) + chr(colormapentry) + o16(0) + o16(0) + o16(im.size[0]) + o16(im.size[1]) + chr(bits) + chr(flags)) if colormaptype: fp.write(im.im.getpalette("RGB", "BGR")) ImageFile._save(im, fp, [("raw", (0,0)+im.size, 0, (rawmode, 0, orientation))]) # # -------------------------------------------------------------------- # Registry Image.register_open("TGA", TgaImageFile, _accept) Image.register_save("TGA", _save) Image.register_extension("TGA", ".tga")
if jpeg_tables: self.tile_prefix = self.jpeg[jpeg_tables] else: raise IOError, "unknown/invalid compression" x = x + xtile if x >= xsize: x, y = 0, y + ytile if y >= ysize: break # isn't really required self.stream = stream self.fp = None def load(self): if not self.fp: self.fp = self.ole.openstream(self.stream[:2] + ["Subimage 0000 Data"]) ImageFile.ImageFile.load(self) # # -------------------------------------------------------------------- Image.register_open("FPX", FpxImageFile, _accept) Image.register_extension("FPX", ".fpx")
if s != "P7 332": raise SyntaxError, "not an XV thumbnail file" # Skip to beginning of next line self.fp.readline() # skip info comments while 1: s = self.fp.readline() if not s: raise SyntaxError, "Unexpected EOF reading XV thumbnail file" if s[0] != '#': break # parse header line (already read) s = string.split(s.strip()) self.mode = "P" self.size = int(s[0]), int(s[1]) self.palette = ImagePalette.raw("RGB", PALETTE) self.tile = [ ("raw", (0, 0)+self.size, self.fp.tell(), (self.mode, 0, 1) )] # -------------------------------------------------------------------- Image.register_open("XVThumb", XVThumbImageFile)
elif layout == (1, 3, 4): self.mode = "RGBA" else: raise SyntaxError("unsupported SGI image mode") # size self.size = i16(s[6:]), i16(s[8:]) # decoder info if compression == 0: offset = 512 pagesize = self.size[0]*self.size[1]*layout[0] self.tile = [] for layer in self.mode: self.tile.append(("raw", (0,0)+self.size, offset, (layer,0,-1))) offset = offset + pagesize elif compression == 1: self.tile = [("sgi_rle", (0,0)+self.size, 512, (self.mode, 0, -1))] # # registry Image.register_open("SGI", SgiImageFile, _accept) Image.register_extension("SGI", ".bw") Image.register_extension("SGI", ".rgb") Image.register_extension("SGI", ".rgba") Image.register_extension("SGI", ".sgi") # really?
if len(self.images) > 1: self.category = Image.CONTAINER self.seek(0) def seek(self, frame): try: filename = self.images[frame] except IndexError: raise EOFError, "no such frame" self.fp = self.ole.openstream(filename) TiffImagePlugin.TiffImageFile._open(self) self.frame = frame def tell(self): return self.frame # # -------------------------------------------------------------------- Image.register_open("MIC", MicImageFile, _accept) Image.register_extension("MIC", ".mic")
def _open(self): # assuming a 4-byte magic label (FIXME: add "_accept" hook) s = self.fp.read(4) if s != "\200\350\000\000": raise SyntaxError, "not a PIXAR file" # read rest of header s = s + self.fp.read(508) self.size = i16(s[418:420]), i16(s[416:418]) # get channel/depth descriptions mode = i16(s[424:426]), i16(s[426:428]) if mode == (14, 2): self.mode = "RGB" # FIXME: to be continued... # create tile descriptor (assuming "dumped") self.tile = [("raw", (0,0)+self.size, 1024, (self.mode, 0, 1))] # # -------------------------------------------------------------------- Image.register_open("PIXAR", PixarImageFile) # # FIXME: what's the standard extension?
raise SyntaxError("Not an HDF file") self.fp.seek(offset) # make something up self.mode = "F" self.size = 1, 1 loader = self._load() if loader: loader.open(self) def _load(self): return _handler def _save(im, fp, filename): if _handler is None or not hasattr("_handler", "save"): raise IOError("HDF5 save handler not installed") _handler.save(im, fp, filename) # -------------------------------------------------------------------- # Registry Image.register_open(HDF5StubImageFile.format, HDF5StubImageFile, _accept) Image.register_save(HDF5StubImageFile.format, _save) Image.register_extension(HDF5StubImageFile.format, ".h5") Image.register_extension(HDF5StubImageFile.format, ".hdf")
o16(offset[0]) + # offset o16(offset[1]) + o16(im.size[0]) + # size o16(im.size[1]) + chr(0) + # flags chr(8)) # bits ImageFile._save(im, fp, [("gif", (0,0)+im.size, 0, RAWMODE[im.mode])]) fp.write("\0") # end of image data finally: del im.encoderinfo return fp.data # -------------------------------------------------------------------- # Registry Image.register_open(GifImageFile.format, GifImageFile, _accept) Image.register_save(GifImageFile.format, _save) Image.register_extension(GifImageFile.format, ".gif") Image.register_mime(GifImageFile.format, "image/gif") # # Uncomment the following line if you wish to use NETPBM/PBMPLUS # instead of the built-in "uncompressed" GIF encoder # Image.register_save(GifImageFile.format, _save_netpbm)
if frame != self.frame + 1: raise ValueError, "cannot seek to frame %d" % frame self.frame = frame # move to next frame self.fp = self.__fp self.fp.seek(self.__offset) s = self.fp.read(4) if not s: raise EOFError framesize = i32(s) self.decodermaxblock = framesize self.tile = [("fli", (0,0)+self.size, self.__offset, None)] self.__offset = self.__offset + framesize def tell(self): return self.frame # # registry Image.register_open("FLI", FliImageFile, _accept) Image.register_extension("FLI", ".fli") Image.register_extension("FLI", ".flc")
o16(bits) + # depth o32(0) + # compression (0=uncompressed) o32(image) + # size of bitmap o32(1) + o32(1) + # resolution o32(colors) + # colors used o32(colors)) # colors important fp.write("\000" * (header - 40)) # padding (for OS/2 format) if im.mode == "1": for i in (0, 255): fp.write(chr(i) * 4) elif im.mode == "L": for i in range(256): fp.write(chr(i) * 4) elif im.mode == "P": fp.write(im.im.getpalette("RGB", "BGRX")) ImageFile._save(im, fp, [("raw", (0, 0) + im.size, 0, (rawmode, stride, -1))]) # # -------------------------------------------------------------------- # Registry Image.register_open(BmpImageFile.format, BmpImageFile, _accept) Image.register_save(BmpImageFile.format, _save) Image.register_extension(BmpImageFile.format, ".bmp")
self.mode, rawmode = "1", "1;I" elif depth == 8: self.mode = rawmode = "L" elif depth == 24: self.mode, rawmode = "RGB", "BGR" else: raise SyntaxError, "unsupported mode" compression = i32(s[20:24]) if i32(s[24:28]) != 0: length = i32(s[28:32]) offset = offset + length self.palette = ImagePalette.raw("RGB;L", self.fp.read(length)) if self.mode == "L": self.mode = rawmode = "P" stride = (((self.size[0] * depth + 7) / 8) + 3) & (~3) if compression == 1: self.tile = [("raw", (0,0)+self.size, offset, (rawmode, stride))] elif compression == 2: self.tile = [("sun_rle", (0,0)+self.size, offset, rawmode)] # # registry Image.register_open("SUN", SunImageFile, _accept) Image.register_extension("SUN", ".ras")
## # Image plugin for MPEG streams. This plugin can identify a stream, # but it cannot read it. class MpegImageFile(ImageFile.ImageFile): format = "MPEG" format_description = "MPEG" def _open(self): s = BitStream(self.fp) if s.read(32) != 0x1B3: raise SyntaxError, "not an MPEG file" self.mode = "RGB" self.size = s.read(12), s.read(12) # -------------------------------------------------------------------- # Registry stuff Image.register_open("MPEG", MpegImageFile) Image.register_extension("MPEG", ".mpg") Image.register_extension("MPEG", ".mpeg") Image.register_mime("MPEG", "video/mpeg")
self.mode = "RGB" self.size = 768, 512 # FIXME: not correct for rotated images! self.tile = [("pcd", (0,0)+self.size, 96*2048, None)] def draft(self, mode, size): if len(self.tile) != 1: return d, e, o, a = self.tile[0] if size: scale = max(self.size[0] / size[0], self.size[1] / size[1]) for s, o in [(4,0*2048), (2,0*2048), (1,96*2048)]: if scale >= s: break # e = e[0], e[1], (e[2]-e[0]+s-1)/s+e[0], (e[3]-e[1]+s-1)/s+e[1] # self.size = ((self.size[0]+s-1)/s, (self.size[1]+s-1)/s) self.tile = [(d, e, o, a)] return self # # registry Image.register_open("PCD", PcdImageFile) Image.register_extension("PCD", ".pcd")
try: frames = im.encoderinfo["frames"] except KeyError: frames = 1 if check: return check fp.write("Image type: %s image\r\n" % type) if filename: fp.write("Name: %s\r\n" % filename) fp.write("Image size (x*y): %d*%d\r\n" % im.size) fp.write("File size (no of images): %d\r\n" % frames) if im.mode == "P": fp.write("Lut: 1\r\n") fp.write("\000" * (511 - fp.tell()) + "\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 Image.register_open("IM", ImImageFile) Image.register_save("IM", _save) Image.register_extension("IM", ".im")
o32(im.size[1]) + # height o16(1) + # planes o16(bits) + # depth o32(0) + # compression (0=uncompressed) o32(image) + # size of bitmap o32(1) + o32(1) + # resolution o32(colors) + # colors used o32(colors)) # colors important fp.write("\000" * (header - 40)) # padding (for OS/2 format) if im.mode == "1": for i in (0, 255): fp.write(chr(i) * 4) elif im.mode == "L": for i in range(256): fp.write(chr(i) * 4) elif im.mode == "P": fp.write(im.im.getpalette("RGB", "BGRX")) ImageFile._save(im, fp, [("raw", (0,0)+im.size, 0, (rawmode, stride, -1))]) # # -------------------------------------------------------------------- # Registry Image.register_open(BmpImageFile.format, BmpImageFile, _accept) Image.register_save(BmpImageFile.format, _save) Image.register_extension(BmpImageFile.format, ".bmp")
# # packbits compression i = 0 tile = [] bytecount = read(channels * ysize * 2) offset = file.tell() for channel in range(channels): layer = mode[channel] if mode == "CMYK": layer = layer + ";I" tile.append( ("packbits", bbox, offset, layer) ) for y in range(ysize): offset = offset + i16(bytecount[i:i+2]) i = i + 2 file.seek(offset) if offset & 1: read(1) # padding return tile # -------------------------------------------------------------------- # registry Image.register_open("PSD", PsdImageFile, _accept) Image.register_extension("PSD", ".psd")
# data orientation stride = len(bits) * ((im.size[0]*bits[0]+7)/8) ifd[ROWSPERSTRIP] = im.size[1] ifd[STRIPBYTECOUNTS] = stride * im.size[1] ifd[STRIPOFFSETS] = 0 # this is adjusted by IFD writer ifd[COMPRESSION] = 1 # no compression offset = ifd.save(fp) ImageFile._save(im, fp, [ ("raw", (0,0)+im.size, offset, (rawmode, stride, 1)) ]) # -- helper for multi-page save -- if im.encoderinfo.has_key("_debug_multipage"): #just to access o32 and o16 (using correct byte order) im._debug_multipage = ifd # # -------------------------------------------------------------------- # Register Image.register_open("TIFF", TiffImageFile, _accept) Image.register_save("TIFF", _save) Image.register_extension("TIFF", ".tif") Image.register_extension("TIFF", ".tiff") Image.register_mime("TIFF", "image/tiff")
bytes = i32(self.fp.read(4)) if width <= 0 or height <= 0 or bytes != 1: raise SyntaxError, "not a GIMP brush" comment = self.fp.read(header_size - 20)[:-1] self.mode = "L" self.size = width, height self.info["comment"] = comment # Since the brush is so small, we read the data immediately self.data = self.fp.read(width * height) def load(self): if not self.data: return # create an image out of the brush data block self.im = Image.core.new(self.mode, self.size) self.im.fromstring(self.data) self.data = "" # # registry Image.register_open("GBR", GbrImageFile, _accept) Image.register_extension("GBR", ".gbr")
# create MSP header header = [0] * 16 header[0], header[1] = i16("Da"), i16("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 sum = 0 for h in header: sum = sum ^ h header[12] = sum # 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 Image.register_open("MSP", MspImageFile, _accept) Image.register_save("MSP", _save) Image.register_extension("MSP", ".msp")
info.get("smooth", 0), info.has_key("optimize"), info.get("streamtype", 0), dpi[0], dpi[1], subsampling, extra, ) ImageFile._save(im, fp, [("jpeg", (0,0)+im.size, 0, rawmode)]) def _save_cjpeg(im, fp, filename): # ALTERNATIVE: handle JPEGs via the IJG command line utilities. import os file = im._dump() os.system("cjpeg %s >%s" % (file, filename)) try: os.unlink(file) except: pass # -------------------------------------------------------------------q- # Registry stuff Image.register_open("JPEG", JpegImageFile, _accept) Image.register_save("JPEG", _save) Image.register_extension("JPEG", ".jfif") Image.register_extension("JPEG", ".jpe") Image.register_extension("JPEG", ".jpg") Image.register_extension("JPEG", ".jpeg") Image.register_mime("JPEG", "image/jpeg")
colormapfirst, colormaplength, colormapentry = 0, 0, 0 if im.mode == "RGBA": flags = 8 else: flags = 0 orientation = im.info.get("orientation", -1) if orientation > 0: flags = flags | 0x20 fp.write("\000" + chr(colormaptype) + chr(imagetype) + o16(colormapfirst) + o16(colormaplength) + chr(colormapentry) + o16(0) + o16(0) + o16(im.size[0]) + o16(im.size[1]) + chr(bits) + chr(flags)) if colormaptype: fp.write(im.im.getpalette("RGB", "BGR")) ImageFile._save(im, fp, [("raw", (0, 0) + im.size, 0, (rawmode, 0, orientation))]) # # -------------------------------------------------------------------- # Registry Image.register_open("TGA", TgaImageFile, _accept) Image.register_save("TGA", _save) Image.register_extension("TGA", ".tga")
if not m: m = s elif ord(s[0]) > ord(m[0]) and ord(s[1]) > ord(m[1]): m = s #print "width", ord(s[0]) #print "height", ord(s[1]) #print "colors", ord(s[2]) #print "reserved", ord(s[3]) #print "hotspot x", i16(s[4:]) #print "hotspot y", i16(s[6:]) #print "bytes", i32(s[8:]) #print "offset", i32(s[12:]) # load as bitmap self._bitmap(i32(m[12:]) + offset) # patch up the bitmap height self.size = self.size[0], self.size[1]/2 d, e, o, a = self.tile[0] self.tile[0] = d, (0,0)+self.size, o, a return # # -------------------------------------------------------------------- Image.register_open("CUR", CurImageFile, _accept) Image.register_extension("CUR", ".cur")
self.mode = "P" self.palette = ImagePalette.raw("RGB", string.join(palette, "")) self.tile = [("raw", (0, 0)+self.size, self.fp.tell(), ("P", 0, 1))] def load_read(self, bytes): # # load all image data in one chunk xsize, ysize = self.size s = [None] * ysize for i in range(ysize): s[i] = string.ljust(self.fp.readline()[1:xsize+1], xsize) self.fp = None return string.join(s, "") # # Registry Image.register_open("XPM", XpmImageFile, _accept) Image.register_extension("XPM", ".xpm") Image.register_mime("XPM", "image/xpm")
# PCX header fp.write( chr(10) + chr(version) + chr(1) + chr(bits) + o16(0) + o16(0) + o16(im.size[0] - 1) + o16(im.size[1] - 1) + o16(dpi[0]) + o16(dpi[1]) + chr(0) * 24 + chr(255) * 24 + chr(0) + chr(planes) + o16(stride) + o16(1) + o16(screen[0]) + o16(screen[1]) + chr(0) * 54) assert fp.tell() == 128 ImageFile._save(im, fp, [("pcx", (0, 0) + im.size, 0, (rawmode, bits * planes))]) if im.mode == "P": # colour palette fp.write(chr(12)) fp.write(im.im.getpalette("RGB", "RGB")) # 768 bytes elif im.mode == "L": # greyscale palette fp.write(chr(12)) for i in range(256): fp.write(chr(i) * 3) # -------------------------------------------------------------------- # registry Image.register_open("PCX", PcxImageFile, _accept) Image.register_save("PCX", _save) Image.register_extension("PCX", ".pcx")
else: raise SyntaxError("unsupported SGI image mode") # size self.size = i16(s[6:]), i16(s[8:]) # decoder info if compression == 0: offset = 512 pagesize = self.size[0] * self.size[1] * layout[0] self.tile = [] for layer in self.mode: self.tile.append( ("raw", (0, 0) + self.size, offset, (layer, 0, -1))) offset = offset + pagesize elif compression == 1: self.tile = [("sgi_rle", (0, 0) + self.size, 512, (self.mode, 0, -1))] # # registry Image.register_open("SGI", SgiImageFile, _accept) Image.register_extension("SGI", ".bw") Image.register_extension("SGI", ".rgb") Image.register_extension("SGI", ".rgba") Image.register_extension("SGI", ".sgi") # really?