ImageMagic Examples

The ImageMagic routines can be very useful. Here are some basic examples using the sub.fits image. I like to use this really simple ImageMagic tutorial. A more detailed site is the ImageMagic homepage. Another long list of Detailed examples in here.


Transform the image file format.
 
% convert sub.fits sub.png
% convert sub.fits sub.jpeg

Get basic info and view the image.
 
% identify sub.fits
sub.fits FITS 600x400 600x400+0+0 32-bit DirectClass 968KB 0.000u 0:00.000
% identify sub.png 
sub.png PNG 600x400 600x400+0+0 16-bit PseudoClass 65536c 392KB 0.000u 0:00.000
% display sub.png 
Below is the type of image we see with the display tool.
The image we see when we use "display" to view the sub.ng file. Not much to look at! Moving the cursor into the image area and hitting the MIDDLE button brings up awindow that shows that part ofthe images as well as the X,Y position of the pixel you are centered on. The left and right buttons bring up semi-useful menus. Experimenting with this image and the display tool, you learn that the UPPER-LEFT corner of the image is the X,Y=1,1 position (not the LOWER-LEFT like in ds9). The last line of text in this little window tells you the gray scale values of the cenetered pixel. In this example, the bright galaxy parts of the image are all staurated at I=255, and the darkest background pixels are around I=80. We'd like to have the background near 0 and only the very brightest parts of the galaxies (the nuclei) at I=255 or so.


Cut (Crop) the image. We may want to cut or tri the image. Here is how we can cut a 200x250 sub-image starting at X,Y=100,80.
 
% convert -crop 200x250+100+80 sub.png test.png 
% identify test.png  
test.png PNG 200x250 600x400+100+80 16-bit .....

Change the grey-scale level in the image. When we made the png image version of sub.fits, we got something that looked very bad. One useful repoitory of scripts using ImageMagic was Fred's ImageMagic Scripts". I found a script there called "autolevel", which I pulled over and made executabel with chmod. I ran it on sub.png, and then got something that I could see with display (unclear how it works, but a step in the right direction!).
 
% cp ~sco/Downloads/autolevel . 
% chmod 777 autolevel
% autolevel sub.png test.png

Here is the image I made with the autolevel script. I would scale the image differently, but at least I can see the stars and galaxies now!

Another thing that gave better results was uing the "normalize" option. Unfortunately, most of the ImageMagic docs I have seen thus far are lack solid descriptions of what is being performed. Note in the example below that I operate onthe sub.fits file directly:
 
% convert sub.fits -normalize a.png
Now my background is closer to 0 (around 25) and the galaxies and stars (white parts) are around 255. Obviously some use of global pixel stats is being made, but exactly what is being done is not described (blerg!). I used the sub.fits image because I could use ds9 on this image to learn that my background pixels have values of around I=200 and the brightest parts of my galaxy images are around I=10000. So, what I want to do is something like:
 
I_new = (I-200)*alpha    where alpha = 255/(10000-200) = 0.0260

How does "-fx" work? It might be useful.
It seems from a lot of web ImageMagic web pages that the "-fx" option in convert should be able to do perform the sort of linear transformation described above. However, nothing remotely like a clear description of how to use "-fx" can be found! Simply put: The problem: I want to transform image pixels so the two levels, for instance I=200 to 9000, are converted to pixels in a new image that scale in the range I_new = 0 to 255.

In the end, I have concluded that the ImageMagic package has an enormous number of tasks, and some of them are useful. As for simple, intuitive pixel transformations, One thing is clear, it is easy to use convert to transform a FITS image to a PNG format. The PNG format is more easily ingested for making nice python-based plots and images. I am thus far unable to make heads or tails out of much of the web documentation. I record below some of my last notes on this topic.

 

Trying to figure out the fx operator in ImageMagic

I use my tim_stripes.sh code to make a simple 
gradient image where values run 1 to 255:
% tim_stripes.s 1 255 a.fits 

Now I alter a.fits:
%  convert a.fits -fx 'u*1.5' at.fits
I would expect the top pixel values to be 255*1.5 or 382, but 
this is not the case. I get huge numbers (according to "display") 
with values like I=3221209087. What the fuck? 

When I convert to a png image, I get wild (but different)
pixel values: 
% convert at.fits at.png

There are some vague references to the fx result being in 
a range 0 to 1, but it unclear what the fuck they mean. 

% tim_stripes.sh 1 1000 a.fits     (I get pixels 1 to 1000) 
When I make a png:
% convert a.fits a.png
% display a.png   ---->  Here the values run from 0 to 255, and I see 
                         a smooth gradient across the full range of X. 

===========================================================
The -level operator is described at:
http://www.imagemagick.org/Usage/color_mods/#level

Again, there is some 0-1 bullshit going on that they do not explain. 

% tim_stripes.sh 1 255 a.fits
% convert a.fits -level 25%,75% aa.fits
When I use ds9 to view the image, I see the two ends (faint and 
bright) are indeed clipped, and I have a smooth gradient in 
between. However, the pixel values are now huge numbers (ie. 5 x 10^9).

When I convert to png:
% convert aa.fits aa.png   ---> I see the same thing, huge numbers! 

Maybe all of this assumes that my original working pixel range is 0-255. 

% tim_stripes.sh 1 1000 a.fits
% convert a.fits a.png
% display a.png
====>  a.fits has values o 1-1000, but a.png has values 1-255. 

Here is one thing that may be important: 
I can transform to an 8-bit image with:
% convert sub.fits -depth 8 s8.fits
  **** The s8.fits image has my 0-255 range, but now all of my 
       high pixel values (I>3000) are burned out. I have lost 
       all of the dynamic range of my image! 

I guess this depth transformation is done for the range of pixels 
Imin to Imax, where Imax will be some vary large value, and here 
is where I loose my dynamic range. 

Maybe later I can revisit this topic (i.e. how to use convert with "-fx" to transofrm pixel values"), but for now I'll move on.




Back to calling page