Beispiel #1
0
    def save(self, output, format=None, **kwargs):
	"""Save the image to a file.
        
        The `file' parameter can be a file name or a file-like object. If
        the file format cannot be deduced from a file name, you have to
        supply the `format' keyword argument.
	"""
        if hasattr(output, 'write'):
	    if format is None:
		raise ValueError, 'No format specified.'
            output = gd.createStream(output)
            stream = True
        elif isinstance(output, str):
            if format is None:
                basename, ext = os.path.splitext(output)
                format = self._extmap.get(ext, ext)
            fout = file(output, 'wb')
            output = gd.createStream(fout)
            stream = False
        else:
            raise TypeError, 'Expecting a file name or a file like object.'
	if format == 'jpeg' and kwargs.has_key('quality'):
	    args += (kwargs['quality'],)
        else:
            args = ()
        if not stream:
            fout.close()
        self.m_image.save(output, format, *args)
Beispiel #2
0
    def load(cls, input, format=None):
	"""Loads and return an image from a file.
        
        The `file' parameter can be a file name or a file like object. If
        the file format cannot be deduced from a file name, you have to
        supply the `format' keyword argument.  Currently, the formats "png",
        "gif", "jpeg", "gd", "gd2", "wbmp", "xbm" and "xpm" are supported.
	"""
        if hasattr(input, 'read'):
	    input = gd.createStream(input)
            stream = True
        elif type(input) is str:
            fin = file(input, 'rb')
            input = gd.createStream(fin)
            stream = False
        else:
	    raise TypeError, 'Expecting a file name or a file like object.'
        if format is None:
            if stream:
                raise ValueError, 'No format or file name specified.'
            else:
                basename, ext = os.path.splitext(input)
                ext = ext.lower()
                format = cls.extensions.get(ext, ext)
        elif format in cls.content_types:
            format = cls.content_types[format]
        if format not in cls.formats:
            raise DrawError, 'Unsupported format: %s' % format
        try:
            image = gd.loadImage(input, format)
        except ValueError, err:
            raise DrawError, str(err)