WARNING:

When running locally you must cd in the following cell into the root of the cloned repo.

In [22]:
cd /Users/dharness/dev/seam


/Users/dharness/dev/seam

In [34]:
%reload_ext autoreload
%autoreload
import matplotlib.animation as animation
import matplotlib.pyplot as plt
from scipy import misc
from scipy.ndimage.filters import gaussian_filter
import numpy as np
from IPython.display import Image
from seam_carver.seam_carver import  (
    normalize,
    compute_eng_grad,
    compute_eng_color,
    compute_eng,
    remove_seam,
    add_seam,
    find_seams,
    get_best_seam,
    reduce_width,
    reduce_height,
    increase_width,
    increase_height,
    intelligent_resize
)

Original Image


In [9]:
Image(filename='./demo/cat.png')


Out[9]:

Simple Energy Gradient with Sobel Filter


In [10]:
cat_img = misc.imread('./demo/cat.png')
eng = compute_eng_grad(cat_img)
misc.imsave('./demo/cat_eng_grad.png', eng)
Image(filename='./demo/cat_eng_grad.png')


Out[10]:

Simple Color Energy


In [11]:
rgb_weights = [-3, 1, -3]
cat_img = misc.imread('./demo/cat.png')
eng = compute_eng_color(cat_img, rgb_weights)
misc.imsave('./demo/cat_eng_color.png', eng)
Image(filename='./demo/cat_eng_color.png')


Out[11]:

Total Energy


In [12]:
rgb_weights = [-3, 1, -3]
mask_weight = 10
cat_img = misc.imread('./demo/cat.png')
mask = np.zeros(cat_img.shape)
img4 = np.dstack((cat_img, mask))
eng = compute_eng(img4, rgb_weights, mask_weight)
misc.imsave('./demo/cat_eng_total.png', eng)
Image(filename='./demo/cat_eng_total.png')


Out[12]:

Add Seam


In [13]:
%autoreload
rgb_weights = [-3, 1, -3]
mask_weight = 10
cat_img = misc.imread('./demo/cat.png')
dims = (cat_img.shape[0], cat_img.shape[1])

img4 = np.dstack((
    cat_img,
    np.zeros(dims)
))
eng = compute_eng(img4, rgb_weights, mask_weight)
seam, adjusted_img4, cost, adjusted_eng = increase_width(img4, eng)

%matplotlib inline
plt.imshow(adjusted_eng, cmap='gray')


Out[13]:
<matplotlib.image.AxesImage at 0x113613a90>

In [14]:
%autoreload
rgb_weights = [-3, 1, -3]
mask_weight = 10
cat_img = misc.imread('./demo/cat.png')
dims = (cat_img.shape[0], cat_img.shape[1])

img4 = np.dstack((
    cat_img,
    np.zeros(dims)
))
eng = compute_eng(img4, rgb_weights, mask_weight)
seam, adjusted_img4, cost, adjusted_eng = increase_height(img4, eng)

%matplotlib inline
plt.imshow(adjusted_eng, cmap='gray')


Out[14]:
<matplotlib.image.AxesImage at 0x113e79278>

Intelligent Resize


In [19]:
%autoreload
rgb_weights = [-3, 1, -3]
mask_weight = 10
cat_img = misc.imread('./demo/cat.png')
mask = np.zeros(cat_img.shape)

resized_img = intelligent_resize(cat_img, 0, -20, rgb_weights, mask, mask_weight)
misc.imsave('./demo/cat_shrunk.png', resized_img)
Image(filename='./demo/cat_shrunk.png')


Out[19]:

In [20]:
%autoreload
resized_img = intelligent_resize(cat_img, 0, 50, rgb_weights, mask, mask_weight)
misc.imsave('./demo/cat_grown.png', resized_img)
Image(filename='./demo/cat_grown.png')


Out[20]:

In [21]:
%autoreload
castle_img = misc.imread('./demo/castle_small.jpg')
castle_mask = np.zeros((castle_img.shape[0], castle_img.shape[1]))

resized_img = intelligent_resize(castle_img, 0, 100, [0,0,0], castle_mask, mask_weight)
misc.imsave('./demo/castle_small_shrunk.png', resized_img)
Image(filename='./demo/castle_small_shrunk.png')


Out[21]:

Examples


In [36]:
%autoreload
lotr_img = misc.imread('./demo/lotr.jpg')
lotr_mask = np.zeros((lotr_img.shape[0], lotr_img.shape[1]))

resized_img = intelligent_resize(lotr_img, 0, 100, [0,0,0], lotr_mask, mask_weight)
misc.imsave('./demo/lotr_out.png', resized_img)
Image(filename='./demo/lotr_out.png')


Out[36]:

In [39]:
%autoreload
hot_dog_img = misc.imread('./demo/hot_dog.jpg')
hot_dog_mask = np.zeros((hot_dog_img.shape[0], hot_dog_img.shape[1]))

resized_img = intelligent_resize(hot_dog_img, 0, -300, [0,0,0], hot_dog_mask, mask_weight)
misc.imsave('./demo/hot_dog.png', resized_img)
Image(filename='./demo/hot_dog.png')


Out[39]:

In [ ]:
%autoreload
rgb_weights = [-3, 1, -3]
mask_weight = 10
cat_img = misc.imread('./demo/cat.png')
mask = np.zeros(cat_img.shape)

resized_img = intelligent_resize(cat_img, 0, -20, rgb_weights, mask, mask_weight)
misc.imsave('./demo/cat_out.png', resized_img)
Image(filename='./demo/cat_out.png')