Pixel.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/env python3
  2. import sys
  3. import os
  4. from PIL import Image
  5. from math import log2, pow
  6. from flask import Flask, send_from_directory
  7. app = Flask(__name__)
  8. @app.route("/image/<string:image>")
  9. def serve_image(image):
  10. return send_from_directory("00/" + "/".join(a + b for a, b in zip(image[:-6][::2], image[:-6][1::2])) + "/", image)
  11. def crop(k, dir, im, x, y, sx, sy):
  12. if (k == 0):
  13. return
  14. os.makedirs(dir, exist_ok=True)
  15. q1 = im.crop((0, 0, x/2, y/2))
  16. q1.resize((sx, sy), Image.LANCZOS).save(dir + (dir.replace('/', '')[2:]) + "00.jpg", "JPEG")
  17. os.makedirs(dir, exist_ok=True)
  18. q2 = im.crop((x/2, 0, x, y/2))
  19. q2.resize((sx, sy), Image.LANCZOS).save(dir + (dir.replace('/', '')[2:]) + "01.jpg", "JPEG")
  20. os.makedirs(dir, exist_ok=True)
  21. q3 = im.crop((0, y/2, x/2, y))
  22. q3.resize((sx, sy), Image.LANCZOS).save(dir + (dir.replace('/', '')[2:]) + "10.jpg", "JPEG")
  23. os.makedirs(dir, exist_ok=True)
  24. q4 = im.crop((x/2, y/2, x, y))
  25. q4.resize((sx, sy), Image.LANCZOS).save(dir + (dir.replace('/', '')[2:]) + "11.jpg", "JPEG")
  26. crop(k-1, dir + "00/", q1, q1.width, q1.height, sx, sy)
  27. crop(k-1, dir + "01/", q2, q2.width, q2.height, sx, sy)
  28. crop(k-1, dir + "10/", q3, q3.width, q3.height, sx, sy)
  29. crop(k-1, dir + "11/", q4, q4.width, q4.height, sx, sy)
  30. def main():
  31. im = Image.open(sys.argv[1])
  32. target_size = int(sys.argv[2])
  33. k = int(log2(im.width) - log2(target_size))
  34. crop(k, "00/", im, im.width, im.height, int(im.width/pow(2, k)), int(im.height/pow(2, k)))
  35. if __name__ == "__main__":
  36. # main()
  37. app.run()