Last active
March 17, 2019 08:21
-
-
Save Saren-Arterius/1543dee2a6e76dcb9c6740f1b3a1c15c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env python3 | |
from multiprocessing import Pool | |
from math import floor | |
from PIL import Image | |
from statistics import median_low | |
from os import listdir | |
from time import time | |
WIDTH = 3840 | |
HEIGHT = 1634 | |
ETA = {} | |
def print_progress(i, total, word, id=None): | |
length = 80 | |
bar_count = length * (i / total) | |
f_bar_count = floor(bar_count) | |
remainder = bar_count - f_bar_count | |
cursor = '|' if remainder == 0 else ['/', '-', '\\'][int(remainder // (1 / 3))] | |
bars, dots = '|' * f_bar_count, '.' * (length - f_bar_count) | |
eta = '' | |
if id is not None: | |
if id not in ETA: | |
ETA[id] = time() | |
try: | |
avg = (i / total) / (time() - ETA[id]) | |
secs = ((total - i) / total) / avg | |
mins = int(secs // 60) | |
secs = int(secs % 60) | |
eta = f'| ETA {mins:02d}:{secs:02d}' | |
except: | |
pass | |
print(f'{word} [{bars}{cursor}{dots}] {(i / total) * 100:.2f}% {eta}', end="\r") | |
pngs = list(filter(lambda f: f.endswith('.png') and not f.startswith('out'), listdir('.'))) | |
images = [Image.open(p) for p in pngs] | |
for i, im in enumerate(images): | |
im.getpixel((0, 0)) | |
print_progress(i + 1, len(images), 'Loading', 'preload') | |
print() | |
def handle_col(y): | |
col = [] | |
for x in range(WIDTH): | |
pixels = [im.getpixel((x, y)) for im in images] | |
px = median_low(pixels) | |
col.append(px) | |
return y, col | |
if __name__ == '__main__': | |
newimg = Image.new('RGB', (WIDTH, HEIGHT)) | |
print_progress(0, HEIGHT, 'Processing') | |
with Pool() as pool: | |
for y, col in pool.imap(handle_col, range(HEIGHT)): | |
for x, px in enumerate(col): | |
newimg.putpixel((x, y), px) | |
print_progress(y + 1, HEIGHT, 'Processing', 'process') | |
print() | |
print('Saving as out.png') | |
newimg.save('out.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment