疑点一:对于HEIC图片,Magic提供的返回值为JPEG。有人可以帮我解释一下吗?疑问 2:当支持的文件格式 .HEIC 不存在时,为什么 cv2.imread 会读取图像?另外,如果支持 cv2.imread,为什么不支持 cv2.imwrite?
import cv2
import os
from pathlib import Path
import magic
path_of_directory = str(Path(os.getcwd()).parent) + '/images'
images = os.listdir(path_of_directory)
for image in images:
try:
path = str(Path(os.getcwd()).parent) + '/images/' + image
write_path = str(Path(os.getcwd()).parent) + '/image_write/' + image
print("image is {}".format(image))
image_mat = cv2.imread(path)
print("shape: {}".format(image_mat.shape))
mime = magic.Magic(mime=True)
mime_type = mime.from_file(path)
print("mime_type: {}".format(mime_type))
cv2.imwrite(write_path, image_mat)
return_value = cv2.haveImageReader(path)
print("opencv will read the image: {}".format(return_value))
except Exception as e:
print("Exception occurred: {}".format(e))