比如说图片格式转JPG
from PIL import Image
import os
import io
def convert_to_jpg(input_path, output_path, min_size_kb=30):
"""
将任意图片转换为 JPG,自动调整质量与分辨率以确保文件≥min_size_kb KB
"""
if not os.path.exists(input_path):
raise FileNotFoundError(f"文件不存在: {input_path}")
with Image.open(input_path) as img:
rgb_img = img.convert("RGB")
# 尝试不同缩放倍率直到达到目标大小
scale = 1.0
while True:
width = int(rgb_img.width * scale)
height = int(rgb_img.height * scale)
resized = rgb_img.resize((width, height), Image.LANCZOS)
buffer = io.BytesIO()
resized.save(buffer, format="JPEG", quality=98)
size_kb = buffer.tell() / 1024
if size_kb >= min_size_kb or scale > 2.5:
with open(output_path, "wb") as f:
f.write(buffer.getvalue())
print(f"已成功转换: {input_path} → {output_path}")
print(f"最终文件大小: {size_kb:.2f} KB, 缩放倍率: {scale:.2f}x")
break
scale += 0.1 # 每次放大10%
# 示例路径
input_file =
output_file =
convert_to_jpg(input_file, output_file, min_size_kb=30)
比如说adguardhome规则去重
# 去重并保持原顺序
input_file =
output_file =
seen = set()
with open(input_file, "r", encoding="utf-8") as fin, open(output_file, "w", encoding="utf-8") as fout:
for line in fin:
line_stripped = line.strip()
if not line_stripped:
continue # 跳过空行
if line_stripped not in seen:
seen.add(line_stripped)
fout.write(line_stripped + "\n")
print(f"去重完成,共保留 {len(seen)} 条规则,已写入 {output_file}")
华为的云函数,也有呢
我一般是直接改后缀名
ai确实方便