我可以使用 pyheic 库在 python 中轻松地将 HEIC 转换为 JPEG。但是文件大小在转换为 JPEG 时会变大。(大约 4 次)。我可以减小保存文件的大小吗?
如何获取 base64 编码的字符串而不是保存 JPEG 图像?
我的代码如下:
# -*- coding: utf-8 -*-
import sys
import os
from PIL import Image # pip3 install pillow
import pyheif # pip3 install pyheif
def call(oriPath, defPath):
try:
# if defPath is webp or heic
fileType = oriPath.split(".")[-1]
if fileType == "heic":
heif_file = pyheif.read(oriPath)
image = Image.frombytes(
heif_file.mode,
heif_file.size,
heif_file.data,
"raw",
heif_file.mode,
heif_file.stride,
)
image.save(defPath, "JPEG")
except:
print(False)
return
print(True)