Image rendering using python: matplotlib and other things.

Here I explore some image display and computation methods using python.

  1. The stinkbug.
  2. Starting with the matplotlib tutorial.
  3. Adding things and filling in gaps.



The stinkbug.

The peculiar name of this section derives from the title of a sample image (stinkbug.png) that I found in a matplotlib tutorial on how to use the imshow() task to make image plot. I downloaded the PNG file stinkbug.png and stored it in my TDATA location. Because the file is very small (unlike my FITS files), I decided to store it in the run diretory:

 
% got
% cd T_runs/Image_Render/ex1_stinker/S
% ls
stinkbug.png
I can do experiments in the upper directory, but important notes and scripts I'll keep in the save directory (S) with stinkbug.png. The first thing I do, is use some of the stuff I have already been playing with to learn about stinkbug.png:
 
% gs.py stinkbug.png
% display gs.png
% identify stinkbug.png
stinkbug.png PNG 500x375 500x375+0+0 8-bit DirectClass 108KB 0.000u 0:00.000
% display stinkbug.png
I used gs.py (my little baby image-rendering code) tp make a plot of the imag with some axes and some overplotting. This looks fine. I use the ImageMagic "identify" routine to learn that the image has a size of 500x375 pixels. I use the ImageMagic "display" routine to view stinkbug.png directly. I see that the dark parts of the image have pixel values <50 and the bright parts have pixel values >175, and hence I confirm that I have an 8-bit image with pixel data numbers in the expexted range of 0-256 (0 to 2^8).

Finally, I since stinkbug is small, I decided to put a copy of it in my python code directory. This is where I began the simple gs.py code in python/implots (IMage PLOTS). I will leave gs.py alone, but I'll start building plot codes with stinkbug, and then work up to making image plots with the other image file there named n3379_B.png (the I made manually using ds9). Some other interesting points I learned as I read the page presenting stinkbug.png is:

  1. Color PNG images are usually referrrd to as RGB or RGBA (where the A referes to a transparency channel).
  2. grey scale images are just one channel
  3. the PNG is called 24-bit becasue it uses THREE 8-bit images: one each for R, G, B.
As a first exercise with stinkbug.png, I move implots/gs.py into the subdir stink1/stink1.py and I just hack out some of the overploting parts. I just want to insure that all of that stuff works properl, then I'll do the same job using the methods used in the matplotlib tutorial. When I did this I could run the program as:
 
% stink1.py stinkbug.png
Input file: stinkbug.png
Type for image is type 'instance'
Type for arr is type 'numpy.ndarray'

Use to view your plot:
display stink.png

And when I use the display line I see a good looking plot of the stinkbug image with axes that tell me the pixel positions in X,Y. Notice that I did add a copule new things to the code: statements that tell me the type of the variables I created named "image" and "arr'. I see that image is an instance (???), and arr is a numpy data array (numpy.ndarray).




Starting with the matplotlib tutorial.

I followed the matplotlib tutorial on the imshow module to build the code on codes/python/implots/stink2/stink2.py. I added a few lines to make some axis labels and create an output plot file.

The image made with stinkbug.png using stink2.py. The code is simple, but I show it here as a record of something that worked.

#!/usr/bin/python
#
# Here I implement things from matplotlib tutorial at
# http://matplotlib.org/users/image_tutorial.html
# 
# To run:   stink2.py stinkbug.png 

from sys import argv
script_name, pngname = argv

import matplotlib.pyplot as plt 
import matplotlib.image as mpimg 
import numpy as np

# read the image file 
img=mpimg.imread(pngname)

# Make the image plot 
imgplot = plt.imshow(img)

# label the axes
label_main = "Image with stink2.py" 
label_xaxis = "X (pixels)"
label_yaxis = "Y (pixels)"
plt.title(label_main)
plt.xlabel(label_xaxis)
plt.ylabel(label_yaxis)
plt.savefig('stink.png')
print '\nUse to view your plot:\ndisplay stink.png\n'





Adding things and filling in gaps.

Here we start to fill in more things. I create a single-channel numpy array and plot it with a color lookup table. After some searching around, I found the names of the available lookup tables. As shown below, I make a new color table, but also I add a colorbar for that table. Perhaps the most important thing to notice is that (somehow) my pixel values are now in the range 0 to 1.

The image made with stinkbug.png using stink3.py. The important thing to know here is that I changed the pixel scale to 0-1, and the code that did this is:
 
# read the image file
img=mpimg.imread(pngname)
# Now build a numpy array
lum_img = img[:,:,0]
print "Type for lum_img = %s" % (type(lum_img))
# lum_img is a single channel image (the B part of RGB?)
As usual, this important step is glossed over in the tutorial, but this appears to have involved something called "array slicing".




back to calling page