Search code examples
google-app-enginegoogle-cloud-storagegoogle-app-engine-python

How to get width and height of non JPG image with fetching 26 bytes in Google App Engine?


I want to get width and height of image stored in Google cloud storage without loading it in memory.

I found answer on stackoverflow here stating that if image type is not JPEG or TIFF then we can get width and height of image by only loading 26 bytes in memory. but I'm unable to figured out way of doing that.

I didn't find any code snippets or article related that.

also I was wondering that if same can achieve by using PNG image which will be created by get_serving_url() trasformation on the fly. and loading 26 bytes of that image.


Solution

  • OK, I'll take a stab. Try this:

    from google.appengine.api import images
    import urllib2
    
    image_url   = "http://....."
    
    req         = urllib2.Request(image_url, headers={'Range': 'bytes=0-30'})
    resp        = urllib2.urlopen(req)
    image_bytes = resp.read()
    image       = images.Image(image_bytes)
    
    print image.width 
    print image.height
    
    resp.close()