analyze_images.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import warnings
  2. warnings.filterwarnings('ignore')
  3. from PIL import Image
  4. import os, json
  5. os.makedirs('examples/how/features', exist_ok=True)
  6. results = []
  7. for i in range(1, 10):
  8. path = f'examples/how/input_local_archive/{i}.jpeg'
  9. img = Image.open(path)
  10. img_rgb = img.convert('RGB')
  11. # Save thumbnail
  12. thumb = img_rgb.resize((360, 480))
  13. thumb.save(f'examples/how/features/thumb_{i}.jpg', 'JPEG', quality=85)
  14. # Get color info
  15. small = img_rgb.resize((50, 50))
  16. pixels = list(small.getdata())
  17. r = sum(p[0] for p in pixels) // len(pixels)
  18. g = sum(p[1] for p in pixels) // len(pixels)
  19. b = sum(p[2] for p in pixels) // len(pixels)
  20. # Get quadrant colors (top/bottom/left/right)
  21. w, h = img_rgb.size
  22. top = img_rgb.crop((0, 0, w, h//3)).resize((10,10))
  23. mid = img_rgb.crop((0, h//3, w, 2*h//3)).resize((10,10))
  24. bot = img_rgb.crop((0, 2*h//3, w, h)).resize((10,10))
  25. def avg_color(region):
  26. px = list(region.getdata())
  27. return (sum(p[0] for p in px)//len(px), sum(p[1] for p in px)//len(px), sum(p[2] for p in px)//len(px))
  28. results.append({
  29. 'index': i,
  30. 'size': img.size,
  31. 'format': img.format,
  32. 'avg_rgb': (r, g, b),
  33. 'top_rgb': avg_color(top),
  34. 'mid_rgb': avg_color(mid),
  35. 'bot_rgb': avg_color(bot),
  36. })
  37. print(f'{i}.jpeg: size={img.size}, avg=({r},{g},{b}), top={avg_color(top)}, mid={avg_color(mid)}, bot={avg_color(bot)}')
  38. print('\nDone! Thumbnails saved to examples/how/features/')