Created
August 15, 2016 11:01
-
-
Save Lukse/681d02db3b10092d231cdbf45b20a389 to your computer and use it in GitHub Desktop.
Stack images by summing frames
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import fnmatch | |
import numpy as np | |
from PIL import Image | |
from tqdm import trange | |
images_in = "img" | |
brighness_divide = 5.0 | |
def find(directory, pattern): | |
file_list = [] | |
for root, dirs, files in os.walk(directory): | |
for basename in files: | |
if fnmatch.fnmatch(basename, pattern): | |
filename = os.path.join(root, basename) | |
file_list.append(filename) | |
return file_list | |
# Find pictures in specified directory | |
imlist = find(images_in, '*.jpg') | |
# take first picture as a refference | |
w, h = Image.open(imlist[0]).size | |
arr = np.zeros((h, w, 3), np.float) | |
for j in trange(len(imlist), desc='Frames'): | |
imgin = Image.open(imlist[j]) | |
imarr = np.array(imgin, dtype=np.float) | |
arr = arr + imarr | |
arr = arr / brighness_divide | |
# brute force clipping - slow | |
height, width, colors = arr.shape | |
for w in trange(width, desc="Clipping "): | |
for h in range(height): | |
a, b, c = arr[h, w] | |
if a > 255: | |
a = 255.0 | |
if b >= 255: | |
b = 255.0 | |
if c > 255: | |
c = 255.0 | |
arr[h, w] = a, b, c | |
arr = np.array(np.round(arr), dtype=np.uint8) | |
out = Image.fromarray(arr, mode="RGB") | |
out.save("sum_"+images_in+".jpg") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment