process-images.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import time
  2. import concurrent.futures
  3. from PIL import Image, ImageFilter
  4. img_names = [
  5. 'photo-1516117172878-fd2c41f4a759.jpg',
  6. 'photo-1532009324734-20a7a5813719.jpg',
  7. 'photo-1524429656589-6633a470097c.jpg',
  8. 'photo-1530224264768-7ff8c1789d79.jpg',
  9. 'photo-1564135624576-c5c88640f235.jpg',
  10. 'photo-1541698444083-023c97d3f4b6.jpg',
  11. 'photo-1522364723953-452d3431c267.jpg',
  12. 'photo-1513938709626-033611b8cc03.jpg',
  13. 'photo-1507143550189-fed454f93097.jpg',
  14. 'photo-1493976040374-85c8e12f0c0e.jpg',
  15. 'photo-1504198453319-5ce911bafcde.jpg',
  16. 'photo-1530122037265-a5f1f91d3b99.jpg',
  17. 'photo-1516972810927-80185027ca84.jpg',
  18. 'photo-1550439062-609e1531270e.jpg',
  19. 'photo-1549692520-acc6669e2f0c.jpg'
  20. ]
  21. t1 = time.perf_counter()
  22. size = (1200, 1200)
  23. def process_image(img_name):
  24. img = Image.open(img_name)
  25. img = img.filter(ImageFilter.GaussianBlur(15))
  26. img.thumbnail(size)
  27. img.save(f'processed/{img_name}')
  28. print(f'{img_name} was processed...')
  29. with concurrent.futures.ProcessPoolExecutor() as executor:
  30. executor.map(process_image, img_names)
  31. t2 = time.perf_counter()
  32. print(f'Finished in {t2-t1} seconds')