python小工具_图片格式HEIC转JPG

python小工具_图片格式HEIC转JPG的图1
python小工具_图片格式HEIC转JPG的图2
python小工具_图片格式HEIC转JPG的图3
注意pip install 时别挂VPN;
HEIF 包不支持windows, 这个pillow_heif可以。

python3.8没成功,不知什么原因。3.9成功了。

苹果手机拍照选择小尺寸节省容量,就会保存有HEIC格式图片,有的电脑无法直接查看,需要安装额外的软件。我们也可以用python转一下格式。

# -*- coding: utf-8 -*-
from PIL import Image
# import pyheif   #不支持windows系统
import os
import pillow_heif
import whatimage


# 循环读取HEIC格式照片,写入JPG
def recyle_convert( org_path, dst_path):
    # 若是目录,将org_path 改为其下文件path
    if os. path. isdir( org_path):
        file_list = os. listdir( org_path)
        for idx, file in enumerate( file_list):
            sub_path = os. path. join( org_path, file)
            recyle_convert( sub_path, dst_path)
    # 若是文件,转换
    elif os. path. isfile( org_path):
        with open( org_path, 'rb') as f:
            file_data = f. read()
            try:
                # 判断照片格式
                fmt = whatimage.identify_image( file_data)
                if fmt in [ 'heic']:
                    heif_file = pillow_heif.read_heif( org_path)
                    image = Image. frombytes( mode=heif_file.mode, size=heif_file.size, data=heif_file.data)
                    # 将要存储的路径及名称
                    path, filename = os. path. split( org_path)
                    name, ext = os. path. splitext( filename)
                    file_path = os. path. join( dst_path, '%s.jpg' % name)
                    print( file_path)
                    # 保存图片(JPEG格式)
                    image. save( file_path, "JPEG")
            except:
                print( 'except')
    else:
        print( org_path + 'is error format!')
    pass
# 主函数入口
def main():
    # dst path
    dst_path = r"D:\JPG"
    if os. path. exists( dst_path) is False:
        os. makedirs( dst_path)
        pass
    # org path
    org_path = r"D:\HEIC"
    # convert
    recyle_convert( org_path, dst_path)
    pass



if __name__ == '__main__':
    main()
登录后免费查看全文
立即登录
App下载
技术邻APP
工程师必备
  • 项目客服
  • 培训客服
  • 平台客服

TOP

1
1
3