import
os
import
sys
from
PIL
import
Image
def
convert_webp_to_jpg(input_path):
if
os.path.isfile(input_path):
process_file(input_path)
elif
os.path.isdir(input_path):
for
root, _, files
in
os.walk(input_path):
for
file
in
files:
if
file
.lower().endswith(
'.webp'
):
process_file(os.path.join(root,
file
))
else
:
print
(f
"路径无效:{input_path}"
)
def
process_file(file_path):
if
file_path.lower().endswith(
'.webp'
):
try
:
with Image.
open
(file_path) as img:
output_path
=
os.path.splitext(file_path)[
0
]
+
".jpg"
rgb_image
=
img.convert(
"RGB"
)
rgb_image.save(output_path,
"JPEG"
)
print
(f
"已转换: {file_path} -> {output_path}"
)
except
Exception as e:
print
(f
"转换失败: {file_path}, 错误: {e}"
)
if
__name__
=
=
"__main__"
:
if
len
(sys.argv) >
1
:
for
path
in
sys.argv[
1
:]:
convert_webp_to_jpg(path)
else
:
print
(
"请将文件或文件夹拖放到此程序上执行转换。"
)