简体   繁体   中英

Downloading a section of an Image from a website in python?

I am currently able to download an entire image from a URL using

from PIL import Image, ImageTk
import cStringIO
import urllib

url = http://prestigemgmt.us/wp-content/uploads/2013/03/dog.jpg
self.image = Image.open(cStringIO.StringIO(urllib.urlopen(url).read()))

It works fine and gets the whole image from the website. My question is there any way to get, lets say only the right half of the image.

I understand I could edit the image after it is downloaded, but speed is an important aspect, so ideally I would download only what I need.

It is not possible to do this.

The common image file formats like PNG and JPEG are encoded in a manner that you cannot download an arbitrary part of the picture without downloading the full picture.

You need to download the whole picture, decode it and edit it after the download.

For the advanced knowledge you can always study PNG and JPEG file formats.

If you are in the control of the server providing the images you can write a server-side script which edits the image on the server and then sends the edit over the wire.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM