0

I am using pyfits to open, edit, and save fits images. The saved images will not open into Maxim DL, but they will open into SAO DS9.

So my question is, is there some formatting change the the images are undergoing when i open, and save them and if so what is it?

The code i use to open and save images:

import pyfits

data = pyfits.open("image1.fits")[0].data
hdr = pyfits.open("image1.fits")[0].header


hdu = pyfits.PrimaryHDU(data,hdr)
hdulist = pyfits.HDUList([hdu])
hdulist.writeto("image2.fits")

doing this creates a duplicate of the image with one difference, the image2 won't open into Maxim DL (image1 still does).

i would really appreciate any input anyone might have!!!

Iguananaut
  • 15,675
  • 4
  • 43
  • 50
user3282375
  • 95
  • 1
  • 1
  • 7
  • Your second question is too broad and unrelated to the first so I removed it for now. As for your first question it's really hard to say what's going on. What type of image is it? What happens when you try to open it? I also noticed that you're opening the file twice: Once to read the header and a second time to read the data. This is unnecessary and inefficient as it still has to reparse the file each time. Just call pyfits.open once and access the header and data attributes through a reference to the HDU. – Iguananaut Feb 08 '14 at 06:08
  • Furthermore it's unclear what exactly you mean to do. If all you want is to copy the primary HDU to a new file just do `hdul = pyfits.open(...); hdul[0].writeto(...)`. There is no need to manually construct new PrimaryHDU or HDUList objects. – Iguananaut Feb 08 '14 at 06:08
  • What i am doing is taking in 1024x1024 unprocessed images and background subtracting them with a collective medium. These new images are what i need to save. Also the image format is FITS. what i think is happening is that it is being saved with a different format that is incompatible with MAXIM DL for whatever reason. I have noticed that the file size doubles after opening and resaving the images, even without editing them. – user3282375 Feb 09 '14 at 17:16
  • They're probably scaled images: http://pyfits.readthedocs.org/en/v3.2.0/appendix/faq.html#why-is-an-image-containing-integer-data-being-converted-unexpectedly-to-floats The way you're mixing the header from the unscaled image with the scaled data could be corrupting the data. – Iguananaut Feb 09 '14 at 18:11
  • i ran one of the images through NASA's fits tester and it seems that i need to update the BITPIX information in the header. This might fix it, but i won't know until i run it through MAXIM DL tomorrow. As for creating a video with the images, any ideas? – user3282375 Feb 09 '14 at 21:42
  • You shouldn't have to manually mess with the BITPIX keyword, and in fact it's probably better not to. Please post more of your code so we can see if there are any suggestions. – Iguananaut Feb 10 '14 at 02:46
  • The latest images still don't open into Maxim DL. i can post more of the code, but i don't see any need to. The problem arises from the lines i have above. It has something to do with the way it gets opened and saved. I read that pyfits automatically scales the image when it opens the image. I don't understand why it will open into DS9 with no problems, but Maxim rejects it. If i knew the root of the error that would be fantastic – user3282375 Feb 11 '14 at 00:05
  • I don't know, you might try asking the Maxim DL folks: http://www.cyanogen.com/support.php Posting more of your code might help too--I've already pointed out that the code sample you gave is a bit problematic. Though on further thought I agree it should be producing at least a valid image (evidenced by the fact that it works in ds9). So it's possible the problem's on Maxim DL's end. – Iguananaut Feb 11 '14 at 22:20
  • In case anyone else is having this same issue with Maxim DL, it will only open 2 dimensional arrays (ie. data.shape=(n,m)) and the data must be set to either dtype='int32' or dtype='int64'. be careful when making this change because all decimal precision will be lost – user3282375 Aug 11 '14 at 20:04

0 Answers0