#!/usr/bin/env python # coding: utf-8 # ## Arrays: Working with Images # In[2]: import matplotlib.image as mpimg import matplotlib.pyplot as plt import urllib2 #from urllib.request import urlopen # in Python3 import numpy as np get_ipython().run_line_magic('matplotlib', 'inline') url = "http://wiki.nurserylive.com/uploads/default/original/2X/b/bd24518a6fb6b8dbe9e55bb793a94df7adbe9bc1.png" # In[3]: try: data = urllib2.urlopen(url) #PNG #data = urlopen(url) #in Python3 except Exception: from io import BytesIO data = BytesIO(urllib2.urlopen(url).read()) #JPG # data = BytesIO(urlopen(url).read()) # in Python3 # First, load the image image = mpimg.imread(data) print(type(image)) # Print out its shape print(image.shape) plt.imshow(image) plt.show() # ## Geometric Manipulations with Tensorflow # In[4]: import tensorflow as tf print tf.__version__ # In[6]: # Create a TensorFlow Variable x = tf.Variable(image, name='x') x = tf.transpose(x, perm=[1, 0, 2]) with tf.Session() as session: model = tf.global_variables_initializer() session.run(model) result = session.run(x) print(image.shape) print(type(result)) print(result.shape) plt.figure(figsize=(10, 5)) plt.subplot(1,2,1) plt.imshow(image) plt.subplot(1,2,2) plt.imshow(result) plt.show() # In[13]: height, width, depth = image.shape # Create a TensorFlow Variable x = tf.Variable(image, name='x') x = tf.reverse_sequence(x, [width] * height, seq_dim=1, batch_dim=0) #x = tf.reverse_sequence(x, np.ones((height,)) * width, seq_dim=1, batch_dim=0) with tf.Session() as session: model = tf.global_variables_initializer() session.run(model) result = session.run(x) print image.shape print result.shape plt.figure(figsize=(10, 5)) plt.subplot(1,2,1) plt.imshow(image) plt.subplot(1,2,2) plt.imshow(result) plt.show() # In[47]: print [width] * height print np.ones(height) print np.ones((height,)) print np.ones((height,)) * width # ## Combine the transposing code with the flip code to rotate clock wise. # In[73]: # Create a TensorFlow Variable x = tf.Variable(image, name='x') x1 = tf.transpose(x, perm=[1, 0, 2]) x2 = tf.reverse_sequence(x, [width] * height, seq_dim=1, batch_dim=0) x3 = tf.transpose(x2, perm=[1, 0, 2]) with tf.Session() as session: model = tf.global_variables_initializer() session.run(model) result90 = session.run(x1) result180 = session.run(x2) result270 = session.run(x3) plt.figure(figsize=(10, 10)) plt.subplot(2,2,1) plt.imshow(image) plt.subplot(2,2,2) plt.imshow(result90) plt.subplot(2,2,3) plt.imshow(result180) plt.subplot(2,2,4) plt.imshow(result270) plt.show() # ## Perform a “flipud”, which flips the image top-to-bottom. # In[14]: height, width, depth = image.shape # Create a TensorFlow Variable x = tf.Variable(image, name='x') x = tf.reverse_sequence(x, [height] * width, seq_dim=0, batch_dim=1) with tf.Session() as session: model = tf.global_variables_initializer() session.run(model) result = session.run(x) print image.shape print result.shape plt.figure(figsize=(10, 5)) plt.subplot(1,2,1) plt.imshow(image) plt.subplot(1,2,2) plt.imshow(result) plt.show() # ## Compute a “mirror”, where the first half of the image is copied, flipped (l-r) and then copied into the second half. # In[15]: height, width, depth = image.shape # Create a TensorFlow Variable x = tf.Variable(image, name='x') x1_half = tf.slice(x, [0, 0, 0], [height, width / 2, 3]) x = tf.Variable(image, name='x') x2 = tf.reverse_sequence(x, [width / 2] * height, seq_dim=1, batch_dim=0) x2_half = tf.slice(x2, [0, 0, 0], [height, width / 2, 3]) x3 = tf.concat(concat_dim=1, values=[x1_half, x2_half]) with tf.Session() as session: model = tf.global_variables_initializer() session.run(model) result = session.run(x3) plt.figure(figsize=(10, 5)) plt.subplot(1,2,1) plt.imshow(image) plt.subplot(1,2,2) plt.imshow(result) plt.show()