import os, Image
import numpy as np
files = os.listdir('./')
image = Image.open('./' + files[0])
im = np.array(image, dtype=np.float32)
for i in range(1, len(files)):
    currentimage = Image.open('./' + files[i])
im += np.array(currentimage, dtype=np.float32)
im /= len(files) * 0.25
final_image = Image.fromarray(np.uint8(im.clip(0, 255)))
final_image.save('all_averaged.jpg', 'JPEG')
Stacking astronomy images with Python