How to read an image from url in python 3 and get the height and width

To read an image directly from url and then reads its size. you can use urllib.request and PIL aka Pillow.
If you don’t have pillow installed. you can use:

  1.  python -m pip install Pillow

and before that you might need to upgrade pip, especially if you get an error

  1.  python -m pip install --upgrade pip

Here is how to read an image from url.

  1.  import urllib.request
  2.  from PIL import Image
  3.  url = 'http://www.example.com/my_image_is_not_your_image.png'
  4.  image = Image.open(urllib.request.urlopen(url))
  5.  width, height = image.size
  6.  print (width,height)
  7.  

If you followed a tutorial that uses urllib2 you can change that to urllib.request in python 3.

‘cp932’ codec can’t encode character ‘\xa1’ in position 62: illegal multibyte sequence

When using print in python with some multibyte character or when decoding from another encoding you might encounter this fatal error:

  1.  Traceback (most recent call last):
  2.   File "skynet.py", line 28, in
  3.   print(skyname_wakeup_word)
  4.  UnicodeEncodeError: 'cp932' codec can't encode character '\xa1' in position 62: illegal multibyte sequence

To sort of fix it, you can use

  1.  skyname_wakeup_word.encode('cp932',"ignore")

This occurs because python can’t convert that character to the traditional windows ansi used in windows terminal (cmd). cp932 is code page 932 the Japanese version of shift-JIS.
Other encodings: cp874, cp936, cp949, cp950, cp1251, cp1252, cp1253, cp1254, cp1255, cp1256, cp1257, cp1258. Source