Brett
www.Paufler.net

Lightning Talk
Image Cracking with Python


Coolness: The Final Effect [2, 6, 30]
alcatraz original
Coolness: The Final Effect [2, 3, 5]

Why Images?


Motivates Learning
Fun - Easy - Accessible

Basic Data Type
MRI - Xray
Microscopes - Telescopes

Signal Processing
Data Compression
(Entropy & Information Theory)
Computer Vision -> openCV

numpy -> Python's version of Fortran
skimage -< Scientific Python Library


Python Just One Tool in the Box

Irfanview
Inkscape

Base Picture
Alcatraz Picture
Watermark Used
Watermark Portion
Collection of Watermarks
Watermark Collection

IrfanView - Resize, Add Watermarks
Inkscape - Make Watermarks & Graphics

Run Irfanview as a subprocess to batch resize, etc.
I find it more convenient than PIL
(Python Image Library).



Eclipse with PyDev

The most important tool in the chain.


Eclipse IDE
Eclipse IDE




Eclipse IDE throwing Error
No foo() defined.
Eclipse throwing an error.

Python Image Cracking Code


Fastest way to get to the innards of an image in Python.

from skimage.io import imread, imsave

img = imread('./input/alcatraz.png')
img = img     #conversion code here
imsave('./output/alcatraz.png', img)



Batch Code With Bells and Whistles

from os import listdir
from skimage.io import imread, imsave

for image in listdir('./input'):

    image = './input/' + image
    print image
	
    img = imread(image)
    print img
	
    sN = image.replace('input', 'output')
    print sN
    imsave(sN, img)
	

image = path to image file
img = numpy img array
sN = save name


Print Output

Output of Print statement
Three stacked arrays.
Range of integers from 0-255.



Round Trip similiar to 'Hello World'.
  Proof setup is working.
  Scrapes meta-data.

Is It Working?

Base Image - alcatraz
Input
Output equals Input - alcatraz
Output

Dissecting the Numpy img array.

from skimage.io import imread, imsave

#NOTE: img is a numpy array
img = imread('./input/alcatraz.png')

#Numpy Slicing Syntax
r = img[:,:,0]
g = img[:,:,1]
b = img[:,:,2]

imsave('./output/red_as_grey.png', r)
imsave('./output/green_as_grey.png', g)
imsave('./output/blue_as_grey.png', b)


Grayscale Color Layers

Greyscale of red layer of alcatraz
Greyscale Red
Greyscale of green layer of alcatraz
Greyscale Green
Greyscale of blue layer of alcatraz
Greyscale Blue



Red as Red, etc.


from skimage.io import imread, imsave
import numpy as np

#NOTE: img is a numpy array
img = imread('./input/alcatraz.png')

#Numpy Slicing Syntax
r = img[:,:,0]
g = img[:,:,1]
b = img[:,:,2]

#Create a matching array of zeroes
z = np.zeros_like(r)

#Reconstructing a three layer (R,G,B) img
red = np.dstack((r, z, z))
green = np.dstack((z, g, z))
blue = np.dstack((z, z, b))

imsave('./output/red_layer.png', red)
imsave('./output/green_layer.png', green)
imsave('./output/blue_layer.png', blue)

print r == g
print img.shape


Color Layers

Red Layer Alcatraz
Red Layer
Green Layer Alcatraz
Green Layer
Blue Layer Alcatraz
Blue Layer


Output of:
print r == g
print img.shape

Numpy r==g output


And Now for Something Completely Different

In which the power of Numpy
is more completely demonstrated.
And hopefully something cool is outputted.


from skimage.io import imread, imsave
import numpy as np

img = imread('./input/alcatraz.png', as_grey=True) 
print 'Grayscale'
print img

#Equal to * 1 below
img = np.array(255 * img, dtype=int)

img_list = []
for n in [1, 2, 3, 5]:
    img = img * n
    print '\n\nimg * %d' % n
    print img
    #create the color img array
    z = np.zeros_like(img)
    g = np.dstack((z, img, z))
    img_list.append(img)
    imsave('./output/green_squared_%d.png' % n, g)

coolness = np.dstack(img_list[1:])
imsave('./output/coolness.png', coolness)


Bug/Error in Above
As I should have assigned the multiplied image to a new variable.


Grayscale printed feedback

grayscale output


Filter Effect Output

Green Effect img *1
img * 1
* 1 in Total
Green Effect img *2
img * 2
* 2 in Total
Green Effect img *3
img * 3
* 6 in Total
Green Effect img *5
img * 5
* 30 in Total

Print Feedback img * 1
Print Feedback img * 2
Print Feedback img * 3
Print Feedback img * 5



Tying It All Together

Coolness!

Coolness: The Final Effect [2, 6, 30]
Original
[2, 6, 30]
Coolness: The Final Effect [2, 3, 5]
Refactored
[2, 3, 5]

Refactored Code

I may be wordy,
but code shouldn't be...



from skimage.io import imread, imsave
import numpy as np

def image_crack_demo_grey(image='./input/alcatraz.png',
                     sN='./output/alcatraz.png',
                     factors=(2,3,5)):
    '''Grayscale 'coolness' effect applied to passed image file.
        factors are amount (r,g,b) color layers are multiplied by'''
    img = np.array(255 * imread(image, as_grey=True), dtype=int) 
    r = img * factors[0]
    g = img * factors[1]
    b = img * factors[2]
    imsave(sN, np.dstack((r,g,b)))

def image_crack_demo(image='./input/alcatraz.png',
                     sN='./output/alcatraz.png',
                     factors=(2,3,5)):
    '''Color Layer 'coolness' effect applied to passed image file.
        factors are amount (r,g,b) color layers are multiplied by'''
    img = imread(image)
    img[:,:,0] *= factors[0]
    img[:,:,1] *= factors[1]
    img[:,:,2] *= factors[2]
    imsave(sN, img)

def run_image_demo():
    '''Demo run of grayscale and color image_crack_demo
    used to populate grand finale of webpage.'''
    for factors in [(2,3,5), (5,3,2), (2,6,30), (30,6,2)]:
        grey_sN = './output/alcatraz_%d_%d_%d_grey.png' % factors
        image_crack_demo_grey(sN=grey_sN, factors=factors)
        color_sN = './output/alcatraz_%d_%d_%d_color.png' % factors
        image_crack_demo(sN=color_sN, factors=factors)

run_image_demo()

Color Layer Effect
Color Layer Effect
Color Layer Effect
Color Layer Effect
Color Layer Effect
grey
color
[2,3,5]
Color Layer Effect
grey
color
[5,3,2]
Color Layer Effect
grey
color
[2,6,30]
Color Layer Effect
grey
color
[30,6,2]

One Liners

After bulk imports:

from skimage.io import imread, imsave
from skimage.filter.rank.generic import entropy
from skimage.filter import sobel
from skimage.morphology import disk

Replacing one liners for img = img

Entropy Filter Example
img = entropy(img, disk(10))
THreshold Filter Example
img[img<=0.64] = 0
Sobel Filter Example
img = sobel(img)

Final Thoughts?
How About a Sound File...

Images are signals.
And so is most everything else...

Cracking a sound file is just as easy.

from scipy.signal import resample
from scipy.io.wavfile import read, write

r, d = read(wav_file_in)



More Code Related Stuff

Brett@Paufler.net

Terms of Service
© Copyright 2015 Brett Paufler