本文目录导读:

准备工作
首先安装所需库:
pip install Pillow
脚本代码
import os
import argparse
from PIL import Image
def set_image_opacity(image_path, opacity, output_path=None, output_format=None):
"""
修改单张图片的透明度
Args:
image_path: 输入图片路径
opacity: 目标透明度 (0.0 - 1.0)
output_path: 输出路径,None 则覆盖原文件
output_format: 输出格式,None 则使用原格式
"""
try:
img = Image.open(image_path).convert("RGBA")
except Exception as e:
print(f"错误:无法打开图片 {image_path} - {e}")
return False
# 获取像素数据
pixels = img.load()
width, height = img.size
# 修改每个像素的 Alpha 通道
for x in range(width):
for y in range(height):
r, g, b, a = pixels[x, y]
# 新的 alpha = 原始 alpha * 目标透明度
new_alpha = int(a * opacity)
pixels[x, y] = (r, g, b, new_alpha)
# 确定输出路径和格式
if output_path is None:
output_path = image_path
if output_format is None:
output_format = img.format if img.format else "PNG"
# 保存图片
img.save(output_path, format=output_format)
return True
def batch_modify_opacity(input_dir, opacity, output_dir=None, overwrite=False, recursive=False):
"""
批量修改图片透明度
Args:
input_dir: 输入目录
opacity: 目标透明度 (0.0 - 1.0)
output_dir: 输出目录,None 则覆盖原文件
overwrite: 是否覆盖原文件 (当 output_dir 为 None 时)
recursive: 是否递归处理子目录
"""
supported_formats = {'.png', '.webp', '.tiff', '.bmp', '.gif'}
if output_dir and not os.path.exists(output_dir):
os.makedirs(output_dir)
# 统计信息
processed = 0
failed = 0
skipped = 0
# 遍历文件
if recursive:
for root, dirs, files in os.walk(input_dir):
for file in files:
file_ext = os.path.splitext(file)[1].lower()
if file_ext in supported_formats:
file_path = os.path.join(root, file)
# 确定输出路径
if output_dir:
rel_path = os.path.relpath(root, input_dir)
out_path = os.path.join(output_dir, rel_path, file)
os.makedirs(os.path.dirname(out_path), exist_ok=True)
elif overwrite:
out_path = file_path
else:
print(f"跳过:{file_path}(未指定输出目录且未启用覆盖)")
skipped += 1
continue
if set_image_opacity(file_path, opacity, out_path):
processed += 1
print(f"✓ 已处理:{file_path} -> {out_path}")
else:
failed += 1
else:
skipped += 1
else:
# 仅处理当前目录
for file in os.listdir(input_dir):
file_path = os.path.join(input_dir, file)
if os.path.isfile(file_path):
file_ext = os.path.splitext(file)[1].lower()
if file_ext in supported_formats:
if output_dir:
out_path = os.path.join(output_dir, file)
elif overwrite:
out_path = file_path
else:
print(f"跳过:{file_path}")
skipped += 1
continue
if set_image_opacity(file_path, opacity, out_path):
processed += 1
print(f"✓ 已处理:{file_path} -> {out_path}")
else:
failed += 1
else:
skipped += 1
# 输出统计信息
print(f"\n处理完成!")
print(f"成功:{processed} 张")
print(f"失败:{failed} 张")
print(f"跳过:{skipped} 张")
def main():
parser = argparse.ArgumentParser(description="批量修改图片透明度")
parser.add_argument("input_dir", help="输入目录路径")
parser.add_argument("opacity", type=float, help="目标透明度 (0.0 - 1.0)")
parser.add_argument("-o", "--output-dir", help="输出目录(不指定则覆盖原文件)")
parser.add_argument("-f", "--force", action="store_true",
help="强制覆盖原文件(当不指定输出目录时生效)")
parser.add_argument("-r", "--recursive", action="store_true",
help="递归处理子目录")
args = parser.parse_args()
# 验证参数
if not os.path.isdir(args.input_dir):
print(f"错误:目录 '{args.input_dir}' 不存在")
return
if args.opacity < 0.0 or args.opacity > 1.0:
print("错误:透明度值必须在 0.0 到 1.0 之间")
return
# 执行批量处理
batch_modify_opacity(
args.input_dir,
args.opacity,
output_dir=args.output_dir,
overwrite=args.force if args.output_dir is None else True,
recursive=args.recursive
)
if __name__ == "__main__":
main()
使用方法
基本用法
# 批量将透明度设为 50%(覆盖原文件) python opacity_changer.py /path/to/images 0.5 -f # 批量将透明度设为 30%,输出到新目录 python opacity_changer.py /path/to/images 0.3 -o /path/to/output # 递归处理子目录 python opacity_changer.py /path/to/images 0.7 -r -o /path/to/output
参数说明
| 参数 | 说明 |
|---|---|
input_dir |
图片所在目录(必填) |
opacity |
目标透明度值,范围 0.0-1.0(必填) |
-o, --output-dir |
输出目录(可选,不指定则覆盖原文件) |
-f, --force |
覆盖原文件模式(需配合覆盖使用) |
-r, --recursive |
递归处理所有子目录 |
示例
将图片透明度统一设为 50%:
python opacity_changer.py ./images 0.5 -f
设为 80% 并输出到新目录:
python opacity_changer.py ./images 0.8 -o ./images_half_transparent
递归处理包含子目录的文件夹:
python opacity_changer.py ./all_images 0.3 -r -o ./output
只处理 PNG 图片(脚本默认): 脚本会自动跳过不支持透明度的格式(如 JPEG),您也可以手动指定筛选。
注意事项
- 支持的图片格式:PNG、WebP、TIFF、BMP、GIF(含透明通道的格式)
- 不支持的格式:JPEG(不支持透明通道,会被跳过)
- 数据安全:使用
-f参数会直接覆盖原文件,建议先备份 - 性能提示:处理大尺寸图片时可能需要一定时间
使用 Docker(可选)
如果不想安装 Python,也可以使用 Docker:
docker run --rm -v $(pwd):/workspace -w /workspace python:3.9 bash -c " pip install Pillow && python -c ' import sys sys.path.insert(0, \"/workspace\") from opacity_changer import batch_modify_opacity batch_modify_opacity(\"/workspace/input\", 0.5, \"/workspace/output\") ' "
版权声明: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直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播