فهرست منبع

Adapting quadrants to use unique binary identifiers

Patrick Augusto 3 سال پیش
والد
کامیت
0ec7c397fa
2فایلهای تغییر یافته به همراه19 افزوده شده و 17 حذف شده
  1. 3 1
      Pixel.md
  2. 16 16
      Pixel.py

+ 3 - 1
Pixel.md

@@ -1,5 +1,7 @@
 A única dependência externa do python é a biblioteca livre, de código aberto, "Pillow",
-é possível instalá-la utilizando `pip3 install --upgrade Pillow` (ou apenas `pip`)
+é possível instalá-la utilizando `pip3 install --upgrade Pillow` (ou apenas `pip`).
+Também usamos a biblioteca "Flask", mas apenas para simular o ambiente de produção, ela
+pode ser obtida da mesma maneira que a Pillow.
 
 ```bash
 chmod +x Pixel.py

+ 16 - 16
Pixel.py

@@ -10,34 +10,34 @@ from flask import Flask, send_from_directory
 
 app = Flask(__name__)
 
-@app.route("/image/<string:image>/<path:image_path")
-def serve_image(image, image_path):
-    return send_from_directory(image, image_path)
+@app.route("/image/<string:image>")
+def serve_image(image):
+    return send_from_directory("00/" + "/".join(a + b for a, b in zip(image[:-6][::2], image[:-6][1::2])) + "/", image)
 
 def crop(k, dir, im, x, y, sx, sy):
     if (k == 0):
         return
 
-    os.makedirs(dir + "0/", exist_ok=True)
+    os.makedirs(dir, exist_ok=True)
     q1 = im.crop((0, 0, x/2, y/2))
-    q1.resize((sx, sy), Image.LANCZOS).save(dir + (dir.replace('/', '_')) + "0.jpg", "JPEG")
+    q1.resize((sx, sy), Image.LANCZOS).save(dir + (dir.replace('/', '')[2:]) + "00.jpg", "JPEG")
     
-    os.makedirs(dir + "1/", exist_ok=True)
+    os.makedirs(dir, exist_ok=True)
     q2 = im.crop((x/2, 0, x, y/2))
-    q2.resize((sx, sy), Image.LANCZOS).save(dir + (dir.replace('/', '_')) + "1.jpg", "JPEG")
+    q2.resize((sx, sy), Image.LANCZOS).save(dir + (dir.replace('/', '')[2:]) + "01.jpg", "JPEG")
 
-    os.makedirs(dir + "2/", exist_ok=True)
+    os.makedirs(dir, exist_ok=True)
     q3 = im.crop((0, y/2, x/2, y))
-    q3.resize((sx, sy), Image.LANCZOS).save(dir + (dir.replace('/', '_')) + "2.jpg", "JPEG")
+    q3.resize((sx, sy), Image.LANCZOS).save(dir + (dir.replace('/', '')[2:]) + "10.jpg", "JPEG")
 
-    os.makedirs(dir + "3/", exist_ok=True)
+    os.makedirs(dir, exist_ok=True)
     q4 = im.crop((x/2, y/2, x, y))
-    q4.resize((sx, sy), Image.LANCZOS).save(dir + (dir.replace('/', '_')) + "3.jpg", "JPEG")
+    q4.resize((sx, sy), Image.LANCZOS).save(dir + (dir.replace('/', '')[2:]) + "11.jpg", "JPEG")
     
-    crop(k-1, dir + "0/", q1, q1.width, q1.height, sx, sy)
-    crop(k-1, dir + "1/", q2, q2.width, q2.height, sx, sy)
-    crop(k-1, dir + "2/", q3, q3.width, q3.height, sx, sy)
-    crop(k-1, dir + "3/", q4, q4.width, q4.height, sx, sy)
+    crop(k-1, dir + "00/", q1, q1.width, q1.height, sx, sy)
+    crop(k-1, dir + "01/", q2, q2.width, q2.height, sx, sy)
+    crop(k-1, dir + "10/", q3, q3.width, q3.height, sx, sy)
+    crop(k-1, dir + "11/", q4, q4.width, q4.height, sx, sy)
 
 def main():
     im = Image.open(sys.argv[1])
@@ -45,7 +45,7 @@ def main():
 
     k = int(log2(im.width) - log2(target_size))
 
-    crop(k, "0/", im, im.width, im.height, int(im.width/pow(2, k)), int(im.height/pow(2, k)))
+    crop(k, "00/", im, im.width, im.height, int(im.width/pow(2, k)), int(im.height/pow(2, k)))
 
 if __name__ == "__main__":
     # main()