vdogsandman
vdogsandman

Reputation: 5417

Saving thumbnails as fits files

Most of my code takes a .fits file and creates small thumbnail images that are based upon certain parameters (they're images of galaxies, and all this is extraneous information . . .)

Anyways, I managed to figure out a way to save the images as a .pdf, but I don't know how to save them as .fits files instead. The solution needs to be something within the "for" loop, so that it can just save the files en masse, because there are way too many thumbnails to iterate through one by one.

The last two lines are the most relevant ones.

for i in range(0,len(ra_new)):
ra_new2=cat['ra'][z&lmass&ra&dec][i]
dec_new2=cat['dec'][z&lmass&ra&dec][i]
target_pixel_x = ((ra_new2-ra_ref)/(pixel_size_x))+reference_pixel_x     
target_pixel_y = ((dec_new2-dec_ref)/(pixel_size_y))+reference_pixel_y  
value=img[target_pixel_x,target_pixel_y]>0
ra_new3=cat['ra'][z&lmass&ra&dec&value][i]
dec_new_3=cat['dec'][z&lmass&ra&dec&value][i]
new_target_pixel_x = ((ra_new3-ra_ref)/(pixel_size_x))+reference_pixel_x     
new_target_pixel_y = ((dec_new3-dec_ref)/(pixel_size_y))+reference_pixel_y 
fig = plt.figure(figsize=(5.,5.))
plt.imshow(img[new_target_pixel_x-200:new_target_pixel_x+200, new_target_pixel_y-200:new_target_pixel_y+200], vmin=-0.01, vmax=0.1, cmap='Greys')
fig.savefig(image+"PHOTO"+str(i)+'.pdf')

Any ideas SO?

Upvotes: 0

Views: 320

Answers (2)

user3148185
user3148185

Reputation: 524

As noted in a comment, the astropy package (if not yet installed) will be useful: http://astropy.readthedocs.org. You can import the required module at the beginning.

from astropy.io import fits

At the last line, you can save a thumbnail FITS file.

thumb = img[new_target_pixel_x-200:new_target_pixel_x+200,
            new_target_pixel_y-200:new_target_pixel_y+200]
fits.writeto(image+str(i).zfill(3)+'.fits',thumb)

Upvotes: 1

Geert Barentsen
Geert Barentsen

Reputation: 11

For converting FITS images to thumbnails, I recommend using the mJPEG tool from the "Montage" software package, available here: http://montage.ipac.caltech.edu/docs/mJPEG.html

For example, to convert a directory of FITS images to JPEG files, and then resize them to thumbnails, I would use a shell script like this:

#!/bin/bash
for FILE in `ls /path/to/images/*.fits`; do
    mJPEG -gray $FILE 5% 90% log -out $FILE.jpg
    convert $FILE.jpg -resize 64x64 $FILE.thumbnail.jpg
done

You can, of course, call these commands from Python instead of a shell script.

Upvotes: 1

Related Questions