How to get image size from url

Today I needed to get image size (width and height) from image urls. I am sharing my python code here.

import requests
from PIL import Image
from io import BytesIO


def get_image_size(url):
    data = requests.get(url).content
    im = Image.open(BytesIO(data))    
    return im.size


if __name__ == "__main__":
    url = "https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgQiI_ipTLsmwY5rfnlGkMdp0Bq2bwLmlhtXRuQMJjCYxojb1bxaZ-LRGdiMlP0XiBWlgG0HCF3GbHQAlrMVi16isP7lwWhBySdTtipnnuWk6UrYiztsKtwStN_0-pyR_mlnMbt9_5rCx14/s1600/sbn_pic_2.jpg"
    width, height = get_image_size(url)
    print width, height

I used pillow - which is a PIL fork.

Feel free to comment if you have any suggestion to improve my code.

Comments

Popular posts from this blog

Python all any built-in function

lambda magic to find prime numbers

Accept-Encoding 'gzip' to make your cralwer faster