In [1]:
# convert image to it's negative - s = L - 1 - r
# where L is max colour resolution

# inline plot magic
%matplotlib inline

In [2]:
# first we import image library
from matplotlib import image
# then plot library
from matplotlib import pyplot
# then numpy
import numpy as np

In [3]:
# read image
img = image.imread('MP.tif')

In [4]:
# lets see how the array looks
img


Out[4]:
array([[[ 0,  0,  0],
        [32,  0,  0],
        [ 0,  0,  0],
        ..., 
        [ 6,  0,  0],
        [ 0,  0,  0],
        [ 0,  0,  0]],

       [[ 6,  0,  0],
        [20,  0,  0],
        [ 6,  0,  0],
        ..., 
        [ 6,  0,  0],
        [ 6,  0,  0],
        [ 0,  0,  0]],

       [[19,  0,  0],
        [13,  0,  0],
        [26,  0,  0],
        ..., 
        [ 6,  0,  0],
        [ 6,  0,  0],
        [ 6,  0,  0]],

       ..., 
       [[ 0,  0,  0],
        [ 0,  0,  0],
        [ 6,  0,  0],
        ..., 
        [ 0,  0,  0],
        [ 0,  0,  0],
        [ 6,  0,  0]],

       [[ 0,  0,  0],
        [ 0,  0,  0],
        [ 0,  0,  0],
        ..., 
        [ 0,  0,  0],
        [13,  0,  0],
        [ 0,  0,  0]],

       [[ 6,  0,  0],
        [ 0,  0,  0],
        [ 6,  0,  0],
        ..., 
        [ 6,  0,  0],
        [ 0,  0,  0],
        [13,  0,  0]]], dtype=uint8)

In [5]:
# ok, we have data in R cell, lets view it
pyplot.imshow(img)


Out[5]:
<matplotlib.image.AxesImage at 0x7f28e86ba490>

In [6]:
# lets find the maximum value
np.max(img)


Out[6]:
255

In [7]:
# and minimum
np.min(img)


Out[7]:
0

In [8]:
# sweet, we have 0-255, a 8 bit image
# lets invert it with
def _invert(cell):
    return 255-cell
# type of out array need to be uint8 or float32
invert = np.vectorize(_invert, otypes=[np.uint8])
inv_img = invert(img)

In [9]:
# did it work?
inv_img


Out[9]:
array([[[255, 255, 255],
        [223, 255, 255],
        [255, 255, 255],
        ..., 
        [249, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[249, 255, 255],
        [235, 255, 255],
        [249, 255, 255],
        ..., 
        [249, 255, 255],
        [249, 255, 255],
        [255, 255, 255]],

       [[236, 255, 255],
        [242, 255, 255],
        [229, 255, 255],
        ..., 
        [249, 255, 255],
        [249, 255, 255],
        [249, 255, 255]],

       ..., 
       [[255, 255, 255],
        [255, 255, 255],
        [249, 255, 255],
        ..., 
        [255, 255, 255],
        [255, 255, 255],
        [249, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ..., 
        [255, 255, 255],
        [242, 255, 255],
        [255, 255, 255]],

       [[249, 255, 255],
        [255, 255, 255],
        [249, 255, 255],
        ..., 
        [249, 255, 255],
        [255, 255, 255],
        [242, 255, 255]]], dtype=uint8)

In [10]:
# seems like it, lets view it
pyplot.imshow(inv_img)


Out[10]:
<matplotlib.image.AxesImage at 0x7f28e6d7d2d0>

In [11]:
# it did work, but it would be sweeter with greyscale
# instead of only red
# lets look at the dimensions
img.shape


Out[11]:
(2048, 2048, 3)

In [14]:
%%time
# ok, we need to change every 2048x2048 pixel
# 2nd+3rd column should be equal to 1st (GB=R in RGB)
def greyscale(row):
    # use numpys max
    m = row.max()
    return np.array([m,m,m], dtype=np.uint8)
grey_img = np.apply_along_axis(greyscale, 2, img)


CPU times: user 1min 24s, sys: 40 ms, total: 1min 24s
Wall time: 1min 24s

In [15]:
pyplot.imshow(grey_img)


Out[15]:
<matplotlib.image.AxesImage at 0x7f28e6bd8250>

In [14]:
# works, but wow, inefficient!
# lets do it by copying
grey_img = np.copy(img)
# to different syntaxes
grey_img[...,1] = grey_img[...,0]
grey_img[:,:,2] = grey_img[:,:,0]

In [15]:
grey_img


Out[15]:
array([[[ 0,  0,  0],
        [32, 32, 32],
        [ 0,  0,  0],
        ..., 
        [ 6,  6,  6],
        [ 0,  0,  0],
        [ 0,  0,  0]],

       [[ 6,  6,  6],
        [20, 20, 20],
        [ 6,  6,  6],
        ..., 
        [ 6,  6,  6],
        [ 6,  6,  6],
        [ 0,  0,  0]],

       [[19, 19, 19],
        [13, 13, 13],
        [26, 26, 26],
        ..., 
        [ 6,  6,  6],
        [ 6,  6,  6],
        [ 6,  6,  6]],

       ..., 
       [[ 0,  0,  0],
        [ 0,  0,  0],
        [ 6,  6,  6],
        ..., 
        [ 0,  0,  0],
        [ 0,  0,  0],
        [ 6,  6,  6]],

       [[ 0,  0,  0],
        [ 0,  0,  0],
        [ 0,  0,  0],
        ..., 
        [ 0,  0,  0],
        [13, 13, 13],
        [ 0,  0,  0]],

       [[ 6,  6,  6],
        [ 0,  0,  0],
        [ 6,  6,  6],
        ..., 
        [ 6,  6,  6],
        [ 0,  0,  0],
        [13, 13, 13]]], dtype=uint8)

In [16]:
# plot it
pyplot.imshow(grey_img)


Out[16]:
<matplotlib.image.AxesImage at 0x11b1a7b38>

In [17]:
# now invert and show
inv_img = invert(grey_img)
pyplot.imshow(inv_img)


Out[17]:
<matplotlib.image.AxesImage at 0x11d1fe780>

In [18]:
# single channel psuedo color
pyplot.imshow(img[...,0])
pyplot.colorbar()


Out[18]:
<matplotlib.colorbar.Colorbar at 0x11f355b00>

In [19]:
# same with custom colormap, see colormaps.png for available cmaps
pyplot.imshow(img[...,0], cmap='gray')
pyplot.colorbar()


Out[19]:
<matplotlib.colorbar.Colorbar at 0x1235a2cc0>

In [20]:
# all of the above, in oneliner - this is the WINNER :-)
pyplot.imshow(255-img[...,0], cmap='gray')


Out[20]:
<matplotlib.image.AxesImage at 0x12563bd68>