Skip to content

Instantly share code, notes, and snippets.

@eloyz
Last active August 29, 2015 14:05
Show Gist options
  • Save eloyz/d4571687b50591d1d076 to your computer and use it in GitHub Desktop.
Save eloyz/d4571687b50591d1d076 to your computer and use it in GitHub Desktop.
A script that downloads images. Script was design with the intent of teaching basic Python programming skills
import os
import StringIO
# import the request module
import requests
from PIL import Image
# Storing the image URL in a constant variable
IMAGE_URL = 'http://www.distractify.netdna-cdn.com/wp-content/uploads/2014/08/slugsolos-1-620x.jpg'
# Create a list of image URL's
IMAGE_URLS = [IMAGE_URL for i in xrange(3)]
SIZE = 128, 128
def get_image(binary):
"""Create an image object using
the binary code.
"""
return Image.open(StringIO.StringIO(binary))
def resize_image(image, size=(128,128)):
"""Resizes image
"""
image.thumbnail(size, Image.ANTIALIAS)
return image
def rotate_image(image, degrees):
"""Rotate an image by so-many degrees
"""
return image.rotate(degrees)
def save_thumbnail(binary, filename):
"""Saves a thumbnail version
of an image.
"""
image = get_image(binary)
image = resize_image(image)
image.save(filename, 'JPEG')
def make_dir(directory):
"""Make directory if it does not exist
"""
if not os.path.exists(directory):
os.makedirs(directory)
def download_images(directory):
"""This function downloads images
"""
# loop through images
for index, image_url in enumerate(IMAGE_URLS, 1):
image_url = image_url.replace('slugsolos-1', 'slugsolos-{}'.format(index))
# Geting a request object
r = requests.get(image_url)
# im = get_image(r.content)
# im = rotate_image(im, 180)
# im.show()
# im.thumbnail(SIZE, Image.ANTIALIAS)
# im.save('thumbnail.jpg', 'JPEG')
# Open image.jpg file
# Write content to image.jpg file
# Close image.jpg file
file_name = 'image-{}.jpg'.format(index)
# file_name = os.path.basename(image_url)
save_thumbnail(r.content, os.path.join('thumbnails', file_name))
file_name = os.path.join(directory, file_name)
with open(file_name, 'wb') as image_file:
image_file.write(r.content)
if __name__ == '__main__':
make_dir('images')
make_dir('thumbnails')
download_images('images')

Downloading an image to learn Python

1. Making a virtual environment

Virtual environments create sandboxes for your code. So that other Python packages that are installed for other programs do not interfere with your current project.

virtualenv venv
source venv/bin/activate

2. Install python packages using pip

PIP is your package manager. It allows you to easily install and uninstall versions of python packages from the Python Package Index https://pypi.python.org/pypi.

pip install requests
pip uninstall requests
pip freeze
pip freeze > requirements.txt

Creating a requirements.txt file is a convention that allows you to more easily install multiple python packages.

pip install -r requirements.txt 

3. Print statements, functions, docstrings, comments, constant variables, lists, loops, writing to a file, creating directories, classes/instances,

Summary of what we'll be covering and what code we'll be creating in order to cover some of these subjects.

  • print statements
  • functions
  • docstrings
  • comments
  • variables
  • lists
  • conditionals
  • loops
  • files
  • directories
  • classes and instances

Building the code

  • Calling a function (calling script, functions, variables, docstring)
  • Download an image (importing, writing a file)
  • Download multiple times (lists, looping)
  • Download multiple images (string.format)
  • Making directories (making, detecting, and creating paths)
  • Manipulating images (binary data, classes, instances, and methods)
@Alazilla
Copy link

@kojoidrissa
Copy link

Alazilla,

I just got a new Windows machine last week. My first personal Windows box in ~7 years. I'm going to use the MESS out of that link. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment