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.
pip install python-instagram from commandline
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
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)
Out[13]:
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()
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)
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()
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]:
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()
In [ ]: