[Image-SIG] reading part of an image

Fredrik Lundh fredrik at pythonware.com
Mon Dec 17 21:00:11 CET 2007


Xiao wrote:

> Hi, is it possible to read only a part of an image (png or gif) by PIL?
> 
> For example, an image is 20000x20000 in pixels. Is it possible for 
> python to read only 100x100 of the picture (not loading the whole 
> picture) and save the small part to a new png file?

PNG and GIF files are encoded streams, so there's no way to "seek" in 
the actual image data.

For other formats, you can check the "tile" attribute after you've 
opened the image, but before you've loaded it; if it contains a list of 
descriptors, or if the type for the descriptors is "raw", you can 
manipulate the "tile" list so it contains only the part you want to 
read, and manipulate the "size" accordingly.

For an example, see:

    http://effbot.org/zone/pil-large-images.htm

</F>



More information about the Image-SIG mailing list