本文目录导读:

是的,完全可以通过脚本自动生成图片缩略图,以下是几种不同场景下的实用脚本方案:
Python脚本(最灵活,推荐)
安装依赖:
pip install pillow
批量生成缩略图脚本:
import os
from PIL import Image
import glob
def create_thumbnails(input_dir, output_dir, size=(150, 150)):
"""批量生成缩略图"""
# 创建输出目录
os.makedirs(output_dir, exist_ok=True)
# 支持的图片格式
extensions = ['*.jpg', '*.jpeg', '*.png', '*.gif', '*.bmp']
for ext in extensions:
for filepath in glob.glob(os.path.join(input_dir, ext)):
filename = os.path.basename(filepath)
try:
# 打开图片
with Image.open(filepath) as img:
# 生成缩略图(保持宽高比)
img.thumbnail(size, Image.LANCZOS)
# 保存缩略图
output_path = os.path.join(output_dir, f"thumb_{filename}")
img.save(output_path, optimize=True)
print(f"已生成: {output_path}")
except Exception as e:
print(f"处理 {filepath} 失败: {e}")
# 使用示例
create_thumbnails("./images", "./thumbnails", (200, 200))
Shell脚本(Linux/macOS)
使用ImageMagick(需要先安装):
#!/bin/bash
# 安装ImageMagick: brew install imagemagick (macOS)
# sudo apt-get install imagemagick (Ubuntu)
input_dir="./images"
output_dir="./thumbnails"
size="200x200"
mkdir -p "$output_dir"
for img in "$input_dir"/*.{jpg,jpeg,png,gif}; do
if [ -f "$img" ]; then
filename=$(basename "$img")
convert "$img" -thumbnail "$size" -quality 85 "$output_dir/thumb_$filename"
echo "已生成: $output_dir/thumb_$filename"
fi
done
Node.js脚本(Web项目常用)
const sharp = require('sharp');
const fs = require('fs');
const path = require('path');
// 安装: npm install sharp
async function generateThumbnails(inputDir, outputDir, width = 200) {
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
const files = fs.readdirSync(inputDir);
for (const file of files) {
const ext = path.extname(file).toLowerCase();
if (['.jpg', '.jpeg', '.png', '.webp'].includes(ext)) {
const inputPath = path.join(inputDir, file);
const outputPath = path.join(outputDir, `thumb_${file}`);
await sharp(inputPath)
.resize(width)
.toFile(outputPath);
console.log(`已生成: ${outputPath}`);
}
}
}
// 使用
generateThumbnails('./images', './thumbnails', 200);
Windows批处理脚本
@echo off
setlocal enabledelayedexpansion
set input_dir=.\images
set output_dir=.\thumbnails
set size=200
if not exist %output_dir% mkdir %output_dir%
for %%f in (%input_dir%\*.jpg %input_dir%\*.png %input_dir%\*.jpeg) do (
echo 处理中: %%~nxf
rem 使用ImageMagick的convert命令(需要安装)
convert "%%f" -thumbnail %size%x%size% -quality 85 "%output_dir%\thumb_%%~nxf"
)
echo 完成!
pause
一键运行脚本(跨平台)
# quick_thumb.py - 简单易用的缩略图生成器
import os
from PIL import Image
def quick_thumb():
print("=== 图片缩略图生成器 ===\n")
input_dir = input("输入源文件夹路径(默认 ./images): ") or "./images"
size = input("输入缩略图尺寸(如 200,200): ") or "200,200"
size = tuple(int(x) for x in size.split(','))
# 检查文件夹
if not os.path.exists(input_dir):
print(f"错误:文件夹 {input_dir} 不存在")
return
output_dir = f"{input_dir}_thumbs"
os.makedirs(output_dir, exist_ok=True)
count = 0
for file in os.listdir(input_dir):
if file.lower().endswith(('.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp')):
try:
img_path = os.path.join(input_dir, file)
with Image.open(img_path) as img:
img.thumbnail(size, Image.LANCZOS)
img.save(os.path.join(output_dir, f"thumb_{file}"), optimize=True)
count += 1
print(f"✓ {file} → thumb_{file}")
except Exception as e:
print(f"✗ {file} 处理失败: {e}")
print(f"\n完成!共生成 {count} 个缩略图")
print(f"输出目录: {output_dir}")
if __name__ == "__main__":
quick_thumb()
使用建议
| 场景 | 推荐方案 | 理由 |
|---|---|---|
| 简单批量处理 | Python + Pillow | 免费、跨平台、易修改 |
| 网站图片处理 | Node.js + Sharp | 速度快、支持格式多 |
| Linux服务器 | Shell + ImageMagick | 无需额外安装 |
| 微信小程序/Web | JavaScript | 前端直接处理 |
| 大量图片(1000+) | Python多线程版 | 并行处理效率高 |
重要提示: 如果需要处理大量图片(尤其是需要保持原始目录结构),推荐使用专业工具如 imagemagick 的 mogrify 命令,或在Python脚本中添加目录遍历功能。
需要我提供特定场景(如网站自动生成、批量改尺寸)的优化版本吗?
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播