old_contents: #!/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()
  
  
new_contents: #!/usr/bin/env python3
  # -*- coding: utf-8 -*-
  
  # Documentation: http://docs.astropy.org/en/stable/io/fits/index.html
  
  import argparse
  from astropy.io import fits
  
  import PIL.Image as pil_img # PIL.Image is a module not a class...
  
  
  # SAVE THE IMAGE ##############################################################
  
  def save(img, output_file_path):
      """
      img is the image and it should be a 2D numpy array with values in the range [0,255].
      """
      if img.ndim != 2:
          raise Exception("The input image should be a 2D numpy array.")
  
      # Save
      mode = "L"                           # "L" = grayscale mode
      pil_image = pil_img.new(mode, img.shape)
  
      # WARNING: nested list and 2D numpy arrays are silently rejected!!!
      #          data *must* be a list or a 1D numpy array!
      pil_image.putdata(img.flatten())
      pil_image.save(output_file_path)
  
  
  if __name__ == "__main__":
  
      # 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_index, hdu in enumerate(hdu_list):
          img = hdu.data   # "hdu.data" is a Numpy Array
  
          if img.ndim == 2:
  
              # If there are only one image (img is an 2D array)
              save(img, "out_hdu{}.png".format(hdu_index))
  
          elif img.ndim == 3:
  
              # If there is more than one image (img is an 3D array)
              for img_index, img in enumerate(img):
                  save(img, "out_hdu{}_{}.png".format(hdu_index, img_index))
  
      # Close the FITS file
      hdu_list.close()
  
  
subject: Create an actual conversion of the image in PNG.