tianyunperfect 1 éve
szülő
commit
609b2df135
3 módosított fájl, 78 hozzáadás és 10 törlés
  1. 35 0
      mrdoc.py
  2. 39 9
      project/compress_dir_img.py
  3. 4 1
      tmp3.py

+ 35 - 0
mrdoc.py

@@ -0,0 +1,35 @@
+# 测试类
+import unittest as test
+
+
+class Test(test.TestCase):
+
+    def testCreateBag(self):
+        # coding:utf-8
+
+        import requests
+
+        base_url = 'https://doc.tianyunperfect.cn'
+        token = '872472c70b11605167ce32c2350673e4576772ff6017ab6c899d20c5'
+
+        # url = base_url + '/api/create_project/?token={}'.format(token)
+        #
+        # strs = 'MySQL、Redis、ES、Mongo、Oracle、Kafka、RocketMQ、K8S、Hadoop、Flink、猫、狗、鸟、海底动物、乐器、花、植物'
+        # for i in strs.split('、'):
+        #     data = {
+        #         'name': i.lower(),
+        #         'desc': i,
+        #         'role': 0
+        #     }
+        #     resp = requests.post(url=url, data=data,verify=False)
+        #     print(resp.json())
+
+    def test_get_list(self):
+        # coding:utf-8
+        import requests
+        base_url = 'https://doc.tianyunperfect.cn'
+        token = '872472c70b11605167ce32c2350673e4576772ff6017ab6c899d20c5'
+        url = base_url + '/api/get_projects/?token={}'.format(token)
+
+        resp = requests.get(url=url, verify=False)
+        print(resp.json())

+ 39 - 9
project/compress_dir_img.py

@@ -1,36 +1,66 @@
 import os
 from PIL import Image
 import argparse
+from pybloom_live import ScalableBloomFilter
 
 
 def convert_to_webp(dir_path):
+    path = "~/bf.bloom"
+    path_expanduser = os.path.expanduser("%s" % path)
+    if os.path.exists(path_expanduser):
+        # 从 ~/bf.bloom 中读取bf
+        bf = ScalableBloomFilter.fromfile(open(path_expanduser, "rb"))
+    else:
+        # 初始化一个布隆过滤器
+        bf = ScalableBloomFilter(initial_capacity=100000, error_rate=0.001)
+
     common_image_extensions = [".jpg", ".jpeg", ".png"]
     for subdir, dirs, files in os.walk(dir_path):
+        print(files)
+
         for file in files:
             filepath = subdir + os.sep + file
+            # bf 校验
+            if filepath in bf:
+                continue
+            # 如果已经转换过了,就不转换
             if filepath.endswith(".webp"):
+                bf.add(filepath)
                 continue
+            # 如果不是图片,就不转换
             if not any(filepath.lower().endswith(ext) for ext in common_image_extensions):
+                bf.add(filepath)
+                continue
+            # 如果图片大小小于500k,就不转换
+            if os.path.getsize(filepath) < 500 * 1024:
+                # 加入过滤器
+                bf.add(filepath)
                 continue
             try:
                 img = Image.open(filepath)
                 if img.format == "WEBP":
+                    bf.add(filepath)
                     continue
                 print("检测到其他格式图片:" + filepath)
                 webp_path = filepath + ".webp"
-                img.save(webp_path, "WEBP")
+                img.save(webp_path, "WEBP", quality=80)
                 os.remove(filepath)
                 os.rename(webp_path, filepath)
+                # 加入过滤器
+                bf.add(filepath)
             except IOError:
                 print(f"Cannot convert {file}")
+    # 保存bf到 ~/bf.bloom
+    bf.tofile(open(path_expanduser, "wb"))
 
 
-if __name__ == "__main__":
-    parser = argparse.ArgumentParser(description='Convert images to webp format.')
-    parser.add_argument('dir_path', type=str, help='Directory path to scan for images')
-
-    args = parser.parse_args()
-
-    convert_to_webp(args.dir_path)
+# if __name__ == "__main__":
+#     parser = argparse.ArgumentParser(description='Convert images to webp format.')
+#     parser.add_argument('dir_path', type=str, help='Directory path to scan for images')
+#
+#     args = parser.parse_args()
+#
+#     convert_to_webp(args.dir_path)
 
-# python compress_dir_img.py /path/to/directory
+## python compress_dir_img.py /path/to/directory
+convert_to_webp("/c/Users/admin")

+ 4 - 1
tmp3.py

@@ -1 +1,4 @@
-123
+import requests
+
+response = requests.post('https://php.tianyunperfect.cn/controller/user.php?action=login', json={'username': 'tianyunperfect', 'password': 'tianyunperfect123'}, verify=False)
+print(response.json())