I was sorting through photos recently and found that HEIC photos taken with mobile phones are not supported by computers; the files are too large, and photos taken casually do not require high resolutions.
Find a HEIC converter available for win https://imagemagick.org/script/convert.php, and add a compression code
import os
import time
import math
from PIL import Image
def compress_jpg(file, min_size=1920):
sImg = Image.open(file)
h, w = sImg.size
max_ori, min_ori = max(h, w), min(h, w)
if min_ori <= min_size: return
size = (math.floor(max_ori / min_ori * min_size), min_size) # 横板
if h < w: size = (size[1], size[0]) # 竖版
dImg = sImg.resize(size, Image.ANTIALIAS)
dImg.save(file)
def heic2jpg(file):
assert os.path.exists(file)
img = file[:-4] + 'jpg'
os.system(f'/path/to/convert {file} {img}')
time.sleep(0.2)
assert os.path.exists(img)
return img
def main(root_dir, keep=False):
for root, dirs, files in os.walk(root_dir):
for file in files:
file = os.path.join(root, file)
if file.lower().endswith('.heic'):
print(file)
compress_jpg(heic2jpg(file))
if not keep:
os.remove(file)
if __name__ == "__main__":
main_with_accelerate('A:\\pictures')