import io

from app.service.utils.oss_client import oss_get_image, oss_upload_image


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(bucket=bucket, object_name=object_name, data_type="PIL")
    image = image.convert("RGBA")
    data = image.getdata()
    #
    new_data = []
    for item in data:
        if item[0] >= 256 and item[1] >= 256 and item[2] >= 256:
            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(bucket=bucket, object_name=new_object_name, image_bytes=image_bytes)
    image_path = f"{bucket}/{new_object_name}"
    return image_path
