Pixel.py 1.5 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>/<path:image_path")
  9. def serve_image(image, image_path):
  10. return send_from_directory(image, image_path)
  11. def crop(k, dir, im, x, y, sx, sy):
  12. if (k == 0):
  13. return
  14. os.makedirs(dir + "0/", exist_ok=True)
  15. q1 = im.crop((0, 0, x/2, y/2))
  16. q1.resize((sx, sy), Image.LANCZOS).save(dir + (dir.replace('/', '_')) + "0.jpg", "JPEG")
  17. os.makedirs(dir + "1/", exist_ok=True)
  18. q2 = im.crop((x/2, 0, x, y/2))
  19. q2.resize((sx, sy), Image.LANCZOS).save(dir + (dir.replace('/', '_')) + "1.jpg", "JPEG")
  20. os.makedirs(dir + "2/", 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.jpg", "JPEG")
  23. os.makedirs(dir + "3/", exist_ok=True)
  24. q4 = im.crop((x/2, y/2, x, y))
  25. q4.resize((sx, sy), Image.LANCZOS).save(dir + (dir.replace('/', '_')) + "3.jpg", "JPEG")
  26. crop(k-1, dir + "0/", q1, q1.width, q1.height, sx, sy)
  27. crop(k-1, dir + "1/", q2, q2.width, q2.height, sx, sy)
  28. crop(k-1, dir + "2/", q3, q3.width, q3.height, sx, sy)
  29. crop(k-1, dir + "3/", 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, "0/", 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()