scripts/cronjob/media/heic_to_jpeg.py

42 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from pymongo import UpdateOne
from lib.all import *
import oss2
import os
import pyheif
from PIL import Image
service_name = 'compress'
log_dir = '/app/log'
logger = Logger(service_name, log_dir=log_dir)
access_key_id = "LTAI5tAdu5LRvZwm4LJa21Fo"
access_key_secret = "WGvSQsDralTfFAAxhEqLBOgbXqflHo"
endpoint_internal = "https://oss-cn-hangzhou-internal.aliyuncs.com"
bucket_name = "wishpal-ironfan-media"
class S:
def __init__(self):
self.bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint_internal, bucket_name)
self.col_image = MongoDB(
host="mongodb://root:Wishpal2024@dds-bp1a72ede37c55d4242490.mongodb.rds.aliyuncs.com:3717/admin",
port=3717,
db="media",
collection="image"
)
print(self.col_image)
def __del__(self):
self.col_image.close()
def proc_oen(self):
form = "jpeg" # 设置输出图片格式需要转换成png时引号内换成png即可
img = pyheif.read("734f-684a-46e1-8f45-5ee57091f75c.heic") # 读取HEIC图片文件
img_bytes = Image.frombytes(mode=img.mode, size=img.size, data=img.data) # 读取图片参数
img_bytes.save("734f-684a-46e1-8f45-5ee57091f75c.jpeg", format=form) # 保存图片为JPEG
s = S()
s.proc_oen()