苹果相册.heic格式转成.jpg格式

heic格式转成.jpg格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import os
from PIL import Image
import pillow_heif

def convert_heic_to_jpg(input_path):
"""
将 HEIC 文件转换为 JPG
"""
try:
heif_file = pillow_heif.read_heif(input_path)
img = Image.frombytes(
heif_file.mode,
heif_file.size,
heif_file.data,
"raw"
)

output_path = os.path.splitext(input_path)[0] + ".jpg"
img.save(output_path, "JPEG", quality=90)
print(f"✔ 转换成功: {input_path} → {output_path}")

except Exception as e:
print(f"✘ 转换失败: {input_path}")
print("原因:", e)


def convert_folder(folder):
"""
批量转换指定文件夹内的所有 HEIC 文件
"""
for file in os.listdir(folder):
if file.lower().endswith(".heic"):
full_path = os.path.join(folder, file)
convert_heic_to_jpg(full_path)


if __name__ == "__main__":
folder = input("请输入要转换的文件夹路径(留空 = 当前目录): ").strip()

if folder == "":
folder = "."

if not os.path.isdir(folder):
print("❌ 路径不存在:", folder)
else:
print("开始转换 HEIC → JPG ...")
convert_folder(folder)
print("全部完成!")