Pixel.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python3
  2. import sys
  3. import os
  4. from PIL import Image, ImageFont, ImageDraw
  5. from math import log2, pow
  6. from flask import Flask, send_file
  7. app = Flask(__name__)
  8. @app.route("/image/<path:image>")
  9. def serve_image(image):
  10. return send_file(image)
  11. def crop(k, dir, im, x, y, sx, sy):
  12. if (k == 0):
  13. return
  14. font = ImageFont.truetype("JackInput.ttf", 50)
  15. os.makedirs(dir, exist_ok=True)
  16. q1 = im.crop((0, 0, x/2, y/2))
  17. rq1 = q1.resize((sx, sy), Image.LANCZOS)
  18. dq1 = ImageDraw.Draw(rq1)
  19. dq1.text((0, 0), str(int((dir.replace('/', '')[2:]) + "00", 2)), (255, 0, 0), font=font)
  20. rq1.save(dir + (dir.replace('/', '')[2:]) + "00.jpg", "JPEG")
  21. os.makedirs(dir, exist_ok=True)
  22. q2 = im.crop((x/2, 0, x, y/2))
  23. rq2 = q2.resize((sx, sy), Image.LANCZOS)
  24. dq2 = ImageDraw.Draw(rq2)
  25. dq2.text((0, 0), str(int((dir.replace('/', '')[2:]) + "01", 2)), (255, 0, 0), font=font)
  26. rq2.save(dir + (dir.replace('/', '')[2:]) + "01.jpg", "JPEG")
  27. os.makedirs(dir, exist_ok=True)
  28. q3 = im.crop((0, y/2, x/2, y))
  29. rq3 = q3.resize((sx, sy), Image.LANCZOS)
  30. dq3 = ImageDraw.Draw(rq3)
  31. dq3.text((0, 0), str(int((dir.replace('/', '')[2:]) + "10", 2)), (255, 0, 0), font=font)
  32. rq3.save(dir + (dir.replace('/', '')[2:]) + "10.jpg", "JPEG")
  33. os.makedirs(dir, exist_ok=True)
  34. q4 = im.crop((x/2, y/2, x, y))
  35. rq4 = q4.resize((sx, sy), Image.LANCZOS)
  36. dq4 = ImageDraw.Draw(rq4)
  37. dq4.text((0, 0), str(int((dir.replace('/', '')[2:]) + "11", 2)), (255, 0, 0), font=font)
  38. rq4.save(dir + (dir.replace('/', '')[2:]) + "11.jpg", "JPEG")
  39. crop(k-1, dir + "00/", q1, q1.width, q1.height, sx, sy)
  40. crop(k-1, dir + "01/", q2, q2.width, q2.height, sx, sy)
  41. crop(k-1, dir + "10/", q3, q3.width, q3.height, sx, sy)
  42. crop(k-1, dir + "11/", q4, q4.width, q4.height, sx, sy)
  43. def main():
  44. im = Image.open(sys.argv[1])
  45. target_size = int(sys.argv[2])
  46. k = int(log2(im.width) - log2(target_size))
  47. crop(k, "00/", im, im.width, im.height, int(im.width/pow(2, k)), int(im.height/pow(2, k)))
  48. if __name__ == "__main__":
  49. # main()
  50. app.run()