Files
AiDA_Python/app/service/design_fast/model_process_service.py

34 lines
1.2 KiB
Python
Raw Normal View History

2024-06-24 16:35:34 +08:00
import io
from minio import Minio
from app.core.config import settings
from app.service.utils.new_oss_client import oss_get_image, oss_upload_image
minio_client = Minio(settings.MINIO_URL, access_key=settings.MINIO_ACCESS, secret_key=settings.MINIO_SECRET, secure=settings.MINIO_SECURE)
2024-06-24 16:35:34 +08:00
def model_transpose(image_path):
bucket = image_path.split("/", 1)[0]
object_name = image_path.split("/", 1)[1]
new_object_name = f'{object_name[:object_name.rfind(".")]}.png'
image = oss_get_image(oss_client=minio_client, bucket=bucket, object_name=object_name, data_type="PIL")
2024-06-24 16:35:34 +08:00
image = image.convert("RGBA")
data = image.getdata()
#
new_data = []
for item in data:
2024-06-27 11:13:34 +08:00
if item[0] >= 256 and item[1] >= 256 and item[2] >= 256:
2024-06-24 16:35:34 +08:00
new_data.append((255, 255, 255, 0))
else:
new_data.append(item)
image.putdata(new_data)
image_data = io.BytesIO()
image.save(image_data, format='PNG')
image_data.seek(0)
image_bytes = image_data.read()
oss_upload_image(oss_client=minio_client, bucket=bucket, object_name=new_object_name, image_bytes=image_bytes)
2024-06-24 16:35:34 +08:00
image_path = f"{bucket}/{new_object_name}"
return image_path