Ejemplo n.º 1
0
        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")
Ejemplo n.º 2
0
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")
Ejemplo n.º 3
0
		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?
Ejemplo n.º 4
0
            # 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")
Ejemplo n.º 5
0
        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")
Ejemplo n.º 6
0
		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")
Ejemplo n.º 7
0
			 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")
Ejemplo n.º 8
0
		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")
Ejemplo n.º 9
0
	fp.write(o16b(offset))
	fp.write(chr(transparent_index))
	fp.write(chr(compression_type))
	fp.write(o16b(0))   # reserved by Palm

	# now write colormap if necessary

	if colormapsize > 0:
		fp.write(o16b(256))
		for i in range(256):
			fp.write(chr(i))
			if colormapmode == 'RGB':
				fp.write(chr(colormap[3 * i]) + chr(colormap[3 * i + 1]) + chr(colormap[3 * i + 2]))
			elif colormapmode == 'RGBA':
				fp.write(chr(colormap[4 * i]) + chr(colormap[4 * i + 1]) + chr(colormap[4 * i + 2]))

	# now convert data to raw form
	ImageFile._save(im, fp, [("raw", (0,0)+im.size, 0, (rawmode, rowbytes, 1))])

	fp.flush()


#
# --------------------------------------------------------------------

Image.register_save("Palm", _save)

Image.register_extension("Palm", ".palm")

Image.register_mime("Palm", "image/palm")
Ejemplo n.º 10
0
        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")
Ejemplo n.º 11
0
		self.frame = 0

		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")
Ejemplo n.º 12
0
            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")
Ejemplo n.º 13
0
        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")
Ejemplo n.º 14
0
                    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")
Ejemplo n.º 15
0
				 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)
Ejemplo n.º 16
0
        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")
Ejemplo n.º 17
0
        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")
Ejemplo n.º 18
0
			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

	data = None
Ejemplo n.º 19
0
		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")
Ejemplo n.º 20
0
    # now write colormap if necessary

    if colormapsize > 0:
        fp.write(o16b(256))
        for i in range(256):
            fp.write(chr(i))
            if colormapmode == 'RGB':
                fp.write(
                    chr(colormap[3 * i]) + chr(colormap[3 * i + 1]) +
                    chr(colormap[3 * i + 2]))
            elif colormapmode == 'RGBA':
                fp.write(
                    chr(colormap[4 * i]) + chr(colormap[4 * i + 1]) +
                    chr(colormap[4 * i + 2]))

    # now convert data to raw form
    ImageFile._save(im, fp,
                    [("raw", (0, 0) + im.size, 0, (rawmode, rowbytes, 1))])

    fp.flush()


#
# --------------------------------------------------------------------

Image.register_save("Palm", _save)

Image.register_extension("Palm", ".palm")

Image.register_mime("Palm", "image/palm")
Ejemplo n.º 21
0
	# 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")
Ejemplo n.º 22
0
        # 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")
Ejemplo n.º 23
0
		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")
Ejemplo n.º 24
0
				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")
Ejemplo n.º 25
0
    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")
Ejemplo n.º 26
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")
Ejemplo n.º 27
0
                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")
Ejemplo n.º 28
0
        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")
Ejemplo n.º 29
0
# --------------------------------------------------------------------

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")
Ejemplo n.º 30
0
            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")
Ejemplo n.º 31
0
             (int(width * 72.0 / resolution), int(height * 72.0 / resolution)))

    xref[5] = fp.tell()
    _obj(fp, 5, Length=len(op.getvalue()))

    fp.write("stream\n")
    fp.write(op.getvalue())
    fp.write("\nendstream\n")

    _endobj(fp)

    #
    # trailer
    startxref = fp.tell()
    fp.write("xref\n0 %d\n0000000000 65535 f \n" % len(xref))
    for x in xref[1:]:
        fp.write("%010d 00000 n \n" % x)
    fp.write("trailer\n<<\n/Size %d\n/Root 1 0 R\n>>\n" % len(xref))
    fp.write("startxref\n%d\n%%%%EOF\n" % startxref)
    fp.flush()


#
# --------------------------------------------------------------------

Image.register_save("PDF", _save)

Image.register_extension("PDF", ".pdf")

Image.register_mime("PDF", "application/pdf")
Ejemplo n.º 32
0
		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")
Ejemplo n.º 33
0
            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")
Ejemplo n.º 34
0
			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")
Ejemplo n.º 35
0
			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")
Ejemplo n.º 36
0
##
# 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")
Ejemplo n.º 37
0
        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")
Ejemplo n.º 38
0
    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")
Ejemplo n.º 39
0
            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")
Ejemplo n.º 40
0
		#
		# 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")
Ejemplo n.º 41
0
			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")
Ejemplo n.º 42
0
		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")
Ejemplo n.º 43
0
        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")
Ejemplo n.º 44
0
        # 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")
Ejemplo n.º 45
0
	op.write("q %d 0 0 %d 0 0 cm /image Do Q\n" % (int(width * 72.0 / resolution), int(height * 72.0 / resolution)))

	xref[5] = fp.tell()
	_obj(fp, 5, Length = len(op.getvalue()))

	fp.write("stream\n")
	fp.write(op.getvalue())
	fp.write("\nendstream\n")

	_endobj(fp)

	#
	# trailer
	startxref = fp.tell()
	fp.write("xref\n0 %d\n0000000000 65535 f \n" % len(xref))
	for x in xref[1:]:
		fp.write("%010d 00000 n \n" % x)
	fp.write("trailer\n<<\n/Size %d\n/Root 1 0 R\n>>\n" % len(xref))
	fp.write("startxref\n%d\n%%%%EOF\n" % startxref)
	fp.flush()

#
# --------------------------------------------------------------------

Image.register_save("PDF", _save)

Image.register_extension("PDF", ".pdf")

Image.register_mime("PDF", "application/pdf")
Ejemplo n.º 46
0
    # 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")
Ejemplo n.º 47
0
		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")
Ejemplo n.º 48
0
			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")
Ejemplo n.º 49
0
        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")
Ejemplo n.º 50
0
    # 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")
Ejemplo n.º 51
0
		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")
Ejemplo n.º 52
0
			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")
Ejemplo n.º 53
0
			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")
Ejemplo n.º 54
0
        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?