Intro to SciKit Image with Instagram API

Author: Kat Chuang @katychuang on Twitter

This is a mini tutorial demo'd at the NYC Pyladies Meetup on Thurs, Aug 29, 2013 to show how scikit image package can be applied to Instagram images via the API.

  1. First step, get your key/secret information to access the API by signing up at http://instagram.com/developer/
  2. Install the Instagram API wrapper with pip install python-instagram from commandline
  3. Import the packages skimage and instagram and matplotlib to use them in your script

In [35]:
%pylab inline 

from pylab import *
import skimage
from skimage import io
import skimage.io._io as io
from skimage.data import load
from PIL import Image


Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.zmq.pylab.backend_inline].
For more information, type 'help(pylab)'.

Examples of SciKit-Image


In [13]:
from skimage.data import camera

# this loads a stock photo from the scikit-image library!
im_orig = camera()

print 'image shape:', im_orig.shape
print 'image (min,max):', im_orig.min(), im_orig.max()

gray()          # set default colormap to gray scale
imshow(im_orig)


image shape: (512, 512)
image (min,max): 0 255
Out[13]:
<matplotlib.image.AxesImage at 0x1090a7f50>

In [14]:
from skimage import data, segmentation, filter, color
import matplotlib.pyplot as plt

coins = data.coins()
mask = coins > filter.threshold_otsu(coins)
clean_border = segmentation.clear_border(mask)

coins_edges = segmentation.visualize_boundaries(color.gray2rgb(coins),
                            clean_border)

plt.figure(figsize=(8, 3.5))
plt.subplot(121)
plt.imshow(clean_border, cmap='gray')
plt.axis('off')
plt.subplot(122)
plt.imshow(coins_edges)
plt.axis('off')

plt.tight_layout()
plt.show()


Instagram API example

Now let's try connecting to Instagram and editing those images.

You can install wrapper with the following command in your commandline

$ pip install python-instagram


In [44]:
from instagram.client import InstagramAPI

# Get your key/secret from http://instagram.com/developer/
INSTAGRAM_CLIENT_ID = 'API KEY'
INSTAGRAM_CLIENT_SECRET = 'SECRET KEY

# plop those babies in!
api = InstagramAPI(client_id=INSTAGRAM_CLIENT_ID,
                   client_secret=INSTAGRAM_CLIENT_SECRET)

#get popular images feed
popular_media = api.media_popular(count=20)

#extract urls of popular images to a list
photolist = []
for media in popular_media:
    photolist.append(media.images['standard_resolution'].url)

print 'Top photos from Instagram'
html = ''

#show the original image thumbnail
for p in photolist:
    html = html + '<img src=' + p + ' width="150" />'
from IPython.core.display import HTML
HTML(html)


Top photos from Instagram
Out[44]:

In [52]:
from skimage import exposure
from skimage.util import img_as_ubyte
import urllib
import urllib2
import cStringIO

# process images and show a more exposed version of photo
for p in photolist:
    im = Image.open(cStringIO.StringIO(urllib.urlopen(p).read()))
    i = img_as_ubyte(im)  # To convert to uint8 data type
    
    image = exposure.rescale_intensity(i, in_range=(0, 2**7 - 1))
    
    axis('off')
    imshow(image,origin='lower')
    show()



In [49]:
p = 'http://distilleryimage1.s3.amazonaws.com/7780073610cb11e3b23122000a1f98cf_7.jpg'
im = Image.open(cStringIO.StringIO(urllib.urlopen(p).read()))
i = img_as_ubyte(im)

from skimage import exposure

image10 = exposure.rescale_intensity(i, in_range=(0, 2**10 - 1))
axis('off')
imshow(image10,origin='lower',cmap=plt.cm.gray)
show()

image8 = exposure.rescale_intensity(i, in_range=(0, 2**8 - 1))
axis('off')
imshow(image8,origin='lower',cmap=plt.cm.gray)
show()

# High Exposure and Rotated
image5 = exposure.rescale_intensity(i, in_range=(0, 2**5 - 1))
im_r = rotate(image5.astype('float')/255, angle=15, order=2)
axis('off')
imshow(im_r,origin='lower',cmap=plt.cm.gray)
show()


Let's flip the image right side up and change the threshold


In [50]:
from skimage.filter import threshold_otsu

u = 'http://distilleryimage9.s3.amazonaws.com/a1392b5c10d511e39edf22000ae916b0_7.jpg'
im = Image.open(cStringIO.StringIO(urllib.urlopen(u).read()))
iimg = img_as_ubyte(im)

im_r = rotate(iimg.astype('float')/255, angle=15, order=2)
thres = threshold_otsu(im_r)
plt.figure(figsize=(8,4))

plt.subplot(121)
#plt.imshow(im_r)
plt.imshow(im_r > thres)
plt.title("80's image")
plt.axis('off')


Out[50]:
(-0.5, 611.5, 611.5, -0.5)

In [54]:
import matplotlib.pyplot as plt
from skimage.filter import threshold_otsu

print "Bad Printer"

for p in photolist:
    im = Image.open(cStringIO.StringIO(urllib.urlopen(p).read()))
    iimg = img_as_ubyte(im)
    im_r = rotate(iimg.astype('float')/255, angle=5, order=2)
    thres = threshold_otsu(im_r)
    plt.figure(figsize=(8,4))

    plt.axis('off')
    plt.title("80's image")
    plt.imshow(im_r > thres)
    show()


Bad Printer

In [ ]: