I have a small problem. I know there are some similar questions but I do not know that I am doing wrong because they are not working for me, I would appreciate any help. I want to change some pixel values in a fits file. They are basically empty spots and I want to fill them with ~the mean pixels value of the image. I do like this:
from __future__ import division
import pyfits as fits
import numpy as np
obj1 = fits.open(raw_input('Name of the image to be improved? '))
data_obj1 = obj1[0].data
meanpix = np.mean(data_obj1)
noise = np.linspace(-meanpix,meanpix,100000)
shape = data_obj1.shape
result = np.zeros(shape)
for x in range(0,shape[0]):
for y in range(0,shape[1]):
if data_obj1[x,y] > -5.48e-14 and data_obj1[x,y] < -5.46e-14:
random_noise = np.random.choice(noise,1)
result[x,y] = random_noise
else:
result[x,y] = data_obj1[x,y]
out = obj1
out[0].data = result
out.writeto(raw_input('Name of the output file? '), clobber=True)
I know it is doing the operation I want to do, because if I print result[x,y] it is how it is supposed to be. Nevertheless, when I open the generated fits file, it is exactly the same as it was at the beginning. So probably I do not understand i) how to properly save the fits file or ii) how to build my new image correctly. Can someone help me?