#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Documentation: http://docs.astropy.org/en/stable/io/fits/index.html

import os.path

import argparse
from astropy.io import fits

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm


# PARSE OPTIONS ###############################################################

parser = argparse.ArgumentParser(description="Convert FITS files to PNG images")

parser.add_argument("filearg", nargs=1, metavar="FILE", help="the FITS file to convert")

args = parser.parse_args()

file_path = args.filearg[0]


# READ DATA ###################################################################

# Open the FITS file
hdu_list = fits.open(file_path)

for hdu_id, hdu in enumerate(hdu_list):
    data = hdu.data   # "hdu.data" is a Numpy Array

    if data.ndim == 2:
        # If there are only one image (data is an 2D array)
        fig = plt.figure(figsize=(8.0, 8.0))
        ax = fig.add_subplot(111)
        ax.imshow(data, interpolation='nearest', cmap=cm.gray)
        plt.savefig("{}_hdu{}.png".format(os.path.splitext(file_path)[0], hdu_id))
    elif data.ndim == 3:
        # If there is more than one image (data is an 3D array)
        for img_index, img in enumerate(data):
            fig = plt.figure(figsize=(8.0, 8.0))
            ax = fig.add_subplot(111)
            ax.imshow(img, interpolation='nearest', cmap=cm.gray)
            plt.savefig("HDU{}_{}.png".format(hdu_id, img_index))

# Close the FITS file
hdu_list.close()


@@ -116,24 +116,8 @@
 ml%0A%0A
-import os.path%0A%0A
 impo
@@ -168,80 +168,742 @@
 ort 
-numpy as np%0Aimport matplotlib.pyplot as plt%0Afrom matplotlib import cm%0A%0A%0A
+PIL.Image as pil_img # PIL.Image is a module not a class...%0A%0A%0A# SAVE THE IMAGE ##############################################################%0A%0Adef save(img, output_file_path):%0A    %22%22%22%0A    img is the image and it should be a 2D numpy array with values in the range %5B0,255%5D.%0A    %22%22%22%0A    if img.ndim != 2:%0A        raise Exception(%22The input image should be a 2D numpy array.%22)%0A%0A    # Save%0A    mode = %22L%22                           # %22L%22 = grayscale mode%0A    pil_image = pil_img.new(mode, img.shape)%0A%0A    # WARNING: nested list and 2D numpy arrays are silently rejected!!!%0A    #          data *must* be a list or a 1D numpy array!%0A    pil_image.putdata(img.flatten())%0A    pil_image.save(output_file_path)%0A%0A%0Aif __name__ == %22__main__%22:%0A%0A    
 # PA
@@ -973,22 +973,22 @@
 ########
-####%0A%0A
+%0A%0A    
 parser =
@@ -1060,17 +1060,20 @@
 mages%22)%0A
-%0A
+    
 parser.a
@@ -1153,17 +1153,20 @@
 nvert%22)%0A
-%0A
+    
 args = p
@@ -1184,17 +1184,20 @@
 _args()%0A
-%0A
+    
 file_pat
@@ -1217,17 +1217,20 @@
 arg%5B0%5D%0A%0A
-%0A
+    
 # READ D
@@ -1296,22 +1296,22 @@
 ########
-####%0A%0A
+%0A%0A    
 # Open t
@@ -1315,32 +1315,36 @@
 n the FITS file%0A
+    
 hdu_list = fits.
@@ -1360,16 +1360,20 @@
 _path)%0A%0A
+    
 for hdu_
@@ -1373,17 +1373,20 @@
 or hdu_i
-d
+ndex
 , hdu in
@@ -1411,20 +1411,23 @@
 t):%0A    
-data
+    img
  = hdu.d
@@ -1467,23 +1467,26 @@
 ay%0A%0A    
-if data
+    if img
 .ndim ==
@@ -1489,24 +1489,27 @@
 m == 2:%0A
+%0A  
         
 # If the
@@ -1492,32 +1492,34 @@
 = 2:%0A%0A          
+  
 # If there are o
@@ -1529,28 +1529,27 @@
  one image (
-data
+img
  is an 2D ar
@@ -1565,238 +1565,74 @@
     
-fig = plt.figure(figsize=(8.0, 8.0))%0A        ax = fig.add_subplot(111)%0A        ax.imshow(data, interpolation='nearest', cmap=cm.gray)%0A        plt.savefig(%22%7B%7D_hdu%7B%7D.png%22.format(os.path.splitext(file_path)%5B0%5D, hdu_id))%0A
+    save(img, %22out_hdu%7B%7D.png%22.format(hdu_index))%0A%0A   
     
+ 
 elif 
-data
+img
 .ndi
@@ -1639,16 +1639,21 @@
 m == 3:%0A
+%0A    
         
@@ -1687,20 +1687,19 @@
  image (
-data
+img
  is an 3
@@ -1711,24 +1711,28 @@
 ay)%0A        
+  
+  
 for img_inde
@@ -1755,196 +1755,48 @@
 ate(
-data):%0A            fig = plt.figure(figsize=(8.0, 8.0))%0A            ax = fig.add_subplot(111)%0A            ax.imshow(img, interpolation='nearest', cmap=cm.gray)%0A            plt.savefig(%22HDU
+img):%0A                save(img, %22out_hdu
 %7B%7D_%7B
@@ -1814,17 +1814,20 @@
 at(hdu_i
-d
+ndex
 , img_in
@@ -1833,16 +1833,20 @@
 ndex))%0A%0A
+    
 # Close 
@@ -1859,16 +1859,20 @@
 TS file%0A
+    
 hdu_list

Create an actual conversion of the image in PNG.