# Program to convert a sequence of colour images into a FITS data cube.
# User has to supply the prefix for each image, the pixel size and number of images in the
# sequence.

import numpy
from astropy.io import fits as pyfits
import matplotlib
import matplotlib.image as mpimg
import os
import PIL
from PIL import Image

abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)

# Image file names should be of the format prefix_N.png
prefix = 'imagespr2017cbnrao17cb15nrao17cb15d_1701096716589_'

# Give the number of images, their pixel dimensions, and the image to start form
nmax = 34
n = 1

x = 424
y = 424
z = nmax

# Name of the output file to produce
fitsfile = 'LLPegasiGif.fits'


image = numpy.zeros((z,y,x))

for i in range(n,nmax):
	print(i)
	#img = mpimg.imread(prefix+str(i)+'.png')
	img = numpy.asarray(Image.open(prefix+str(i)+'.png').convert('L'))
	
	#for j in range(0,x):
	#	for k in range(0,y):
	#		image[i,j,k] = img[k,j,0]
						# Greyscale	#RGB data
	image[i,:,:] = img[:,:] 	#img[:,:,0]	# 


pyfits.writeto(fitsfile,image, clobber=True)
