处理图片的缩放

This commit is contained in:
xsl
2025-10-31 15:16:27 +08:00
parent 2555047190
commit 263a061d27
58 changed files with 47 additions and 0 deletions
+47
View File
@@ -0,0 +1,47 @@
import os
from PIL import Image
def resize_png_files_simple(folder_path):
"""
简化版本:直接指定文件夹路径
"""
# 处理根目录
process_folder(folder_path)
# 处理一层子目录
for item in os.listdir(folder_path):
item_path = os.path.join(folder_path, item)
if os.path.isdir(item_path):
process_folder(item_path)
def process_folder(folder):
"""处理单个文件夹中的PNG文件"""
for filename in os.listdir(folder):
if filename.lower().endswith('.png'):
file_path = os.path.join(folder, filename)
try:
# 打开图片
with Image.open(file_path) as img:
# 确保图片是RGBA模式(32位带透明通道)
if img.mode != 'RGBA':
img = img.convert('RGBA')
# 直接缩放为512x512
resized_img = img.resize((512, 512), Image.Resampling.LANCZOS)
# 保存为32位PNG,保持透明通道
resized_img.save(file_path, 'PNG', optimize=True)
print(f"✓ 已处理: {filename} ({img.width}x{img.height} -> 512x512)")
except Exception as e:
print(f"✗ 处理文件 '{filename}' 时出错: {str(e)}")
# 使用方法:直接修改下面的路径为你需要的路径
if __name__ == "__main__":
target_folder = '../example/src/main/assets/pic/action24'
if os.path.exists(target_folder):
resize_png_files_simple(target_folder)
print("所有PNG文件已缩放为512x512")
else:
print("文件夹不存在!")