Error converting image: Invalid type for Profile | Python

使用ImageCms.ImageCmsProfile加载文件时出现如下错误

1
2
3
4
5
        with open(profile_path, 'rb') as f:
profile_content = f.read()
profile = ImageCms.ImageCmsProfile(profile_content)

# Error converting image: Invalid type for Profile

错误原因:读取文件时,返回的是一个bytes对象,而ImageCms.ImageCmsProfile需要一个文件路径或者一个BytesIO对象
解决方法:使用BytesIO对象

1
2
3
with open(profile_path, 'rb') as f:
profile_content = f.read()
profile = ImageCms.ImageCmsProfile(io.BytesIO(profile_content))