-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheic2jpg.py
51 lines (42 loc) · 1.43 KB
/
heic2jpg.py
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
import subprocess
import os
import io
import whatimage
import pyheif
import traceback
from PIL import Image
from shutil import copyfile
def decodeImage(bytesIo):
try:
fmt = whatimage.identify_image(bytesIo)
if fmt in ['heic']:
i = pyheif.read_heif(bytesIo)
img_data = Image.frombytes(mode=i.mode, size=i.size, data=i.data)
# img_data.save('test.jpg', format="jpeg")
except:
traceback.print_exc()
return img_data
def read_image_file_rb(file_path):
with open(file_path, 'rb') as f:
file_data = f.read()
return file_data
if __name__ == "__main__":
new_folder = 'converted_dataset1'
folder_pth = 'dataset1'
subfolder_list = os.listdir(folder_pth)
img_data = None
for sub in subfolder_list:
new_sub_pth = os.path.join(new_folder, str(sub))
sub_pth = os.path.join(folder_pth, str(sub))
img_in_folder = os.listdir(sub_pth)
for img in img_in_folder:
img_pth = os.path.join(sub_pth, str(img))
new_img_pth = os.path.join(new_sub_pth, str(img))
if img.find('HEIC') != -1:
# print(img_pth)
new_path = new_img_pth.replace('.HEIC', '.jpg')
data = read_image_file_rb(img_pth)
img_data = decodeImage(data)
img_data.save(new_path, format = "jpeg")
os.remove(new_img_pth)
print("Done!")