Using Our Image Module

In Pycharm IDE we had written some functions for downloading an image from the web and creating a PIL image object. Here we import the function pil_image_from_url which is a function we wrote in our image.py module which is within the image folder.

from image.image import pil_image_from_url
# pick any image_url from the web you want to use
image_url = 'https://cdn.britannica.com/82/212182-050-50D9F3CE/basketball-LeBron-James-Cleveland-Cavaliers-2018.jpg'
img = pil_image_from_url(image_url)
type(img)
PIL.JpegImagePlugin.JpegImageFile
img
../../_images/using_image_udemy_6_0.png
img.size
(1120, 1600)
img.rotate(45)
../../_images/using_image_udemy_8_0.png
img.rotate(90)
../../_images/using_image_udemy_9_0.png
(left, upper, right, lower) = (500, 400, 800, 600)
img_crop = img.crop((left, upper, right, lower))
img_crop
../../_images/using_image_udemy_11_0.png

Downloading Images

We also wrote a simple function to download some images from the web.

from image.image import download_images
image_urls = ['https://upload.wikimedia.org/wikipedia/commons/1/18/Dog_Breeds.jpg',
              'https://static.im-a-puzzle.com/gallery/Animals/Dogs/Dog_in_the_snow.jpg',
              'https://upload.wikimedia.org/wikipedia/commons/4/43/Cute_dog.jpg'
              ]
# change the folder_path on your machine
images = download_images(image_urls,
                         folder_path='/Users/guestadmin/junk/downloaded_images')
images
[{'image_url': 'https://upload.wikimedia.org/wikipedia/commons/1/18/Dog_Breeds.jpg',
  'width': 1100,
  'height': 825,
  'format': 'JPEG',
  'file_name': '/Users/guestadmin/junk/downloaded_images/0.jpg'},
 {'image_url': 'https://static.im-a-puzzle.com/gallery/Animals/Dogs/Dog_in_the_snow.jpg',
  'width': 1350,
  'height': 900,
  'format': 'JPEG',
  'file_name': '/Users/guestadmin/junk/downloaded_images/1.jpg'},
 {'image_url': 'https://upload.wikimedia.org/wikipedia/commons/4/43/Cute_dog.jpg',
  'width': 3176,
  'height': 2117,
  'format': 'JPEG',
  'file_name': '/Users/guestadmin/junk/downloaded_images/2.jpg'}]