Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. from multiprocessing import Pool
  3. from math import floor
  4. from PIL import Image
  5. from statistics import median_low
  6. from os import listdir
  7. from time import time
  8.  
  9. WIDTH = 3840
  10. HEIGHT = 1634
  11. ETA = {}
  12.  
  13. def print_progress(i, total, word, id=None):
  14. length = 80
  15. bar_count = length * (i / total)
  16. f_bar_count = floor(bar_count)
  17. remainder = bar_count - f_bar_count
  18. cursor = '|' if remainder == 0 else ['/', '-', '\\'][int(remainder // (1 / 3))]
  19. bars, dots = '|' * f_bar_count, '.' * (length - f_bar_count)
  20. eta = ''
  21. if id is not None:
  22. if id not in ETA:
  23. ETA[id] = time()
  24. try:
  25. avg = (i / total) / (time() - ETA[id])
  26. secs = ((total - i) / total) / avg
  27. mins = int(secs // 60)
  28. secs = int(secs % 60)
  29. eta = f'| ETA {mins:02d}:{secs:02d}'
  30. except:
  31. pass
  32. print(f'{word} [{bars}{cursor}{dots}] {(i / total) * 100:.2f}% {eta}', end="\r")
  33.  
  34. pngs = list(filter(lambda f: f.endswith('.png') and not f.startswith('out'), listdir('.')))
  35. images = [Image.open(p) for p in pngs]
  36. for i, im in enumerate(images):
  37. im.getpixel((0, 0))
  38. print_progress(i + 1, len(images), 'Loading', 'preload')
  39.  
  40. print()
  41.  
  42. def handle_col(y):
  43. col = []
  44. for x in range(WIDTH):
  45. pixels = [im.getpixel((x, y)) for im in images]
  46. px = median_low(pixels)
  47. col.append(px)
  48. return y, col
  49.  
  50. if __name__ == '__main__':
  51. newimg = Image.new('RGB', (WIDTH, HEIGHT))
  52. print_progress(0, HEIGHT, 'Processing')
  53. with Pool() as pool:
  54. for y, col in pool.imap(handle_col, range(HEIGHT)):
  55. for x, px in enumerate(col):
  56. newimg.putpixel((x, y), px)
  57. print_progress(y + 1, HEIGHT, 'Processing', 'process')
  58. print()
  59. print('Saving as out.png')
  60. newimg.save('out.png')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement