fix  minio and s3
This commit is contained in:
zhouchengrong
2024-06-21 17:13:39 +08:00
parent d0597f4b4c
commit 2df1518a99
11 changed files with 200 additions and 146 deletions

View File

@@ -1,19 +1,16 @@
import random
from io import BytesIO
# import boto3
import cv2
import numpy as np
from PIL import Image
from minio import Minio
from app.core.config import *
from app.service.utils.oss_client import oss_get_image
from ..builder import PIPELINES
minio_client = Minio(
MINIO_URL,
access_key=MINIO_ACCESS,
secret_key=MINIO_SECRET,
secure=MINIO_SECURE)
# minio_client = Minio(MINIO_URL, access_key=MINIO_ACCESS, secret_key=MINIO_SECRET, secure=MINIO_SECURE)
# s3 = boto3.client('s3', aws_access_key_id=S3_ACCESS_KEY, aws_secret_access_key=S3_AWS_SECRET_ACCESS_KEY, region_name=S3_REGION_NAME)
@@ -56,17 +53,18 @@ class Painting(object):
@staticmethod
def get_gradient(bucket_name, object_name):
image_data = minio_client.get_object(bucket_name, object_name)
# image_data = minio_client.get_object(bucket_name, object_name)
# image_data = s3.get_object(Bucket=bucket_name, Key=object_name)['Body']
# 从数据流中读取图像
image_bytes = image_data.read()
# image_bytes = image_data.read()
# 将图像数据转换为numpy数组
image_array = np.asarray(bytearray(image_bytes), dtype=np.uint8)
# image_array = np.asarray(bytearray(image_bytes), dtype=np.uint8)
# 使用OpenCV解码图像数组
image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
# image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
image = oss_get_image(bucket=bucket_name, object_name=object_name, data_type="cv2")
return image
@staticmethod
@@ -494,16 +492,20 @@ class PrintPainting(object):
if not 'IfSingle' in print_dict.keys():
print_dict['IfSingle'] = False
data = minio_client.get_object(print_dict['print_path_list'][0].split("/", 1)[0], print_dict['print_path_list'][0].split("/", 1)[1])
# data = s3.get_object(Bucket=print_dict['print_path_list'][0].split("/", 1)[0], Key=print_dict['print_path_list'][0].split("/", 1)[1])['Body']
# data = minio_client.get_object(print_dict['print_path_list'][0].split("/", 1)[0], print_dict['print_path_list'][0].split("/", 1)[1])
# data_bytes = BytesIO(data.read())
# image = Image.open(data_bytes)
# image_mode = image.mode
data_bytes = BytesIO(data.read())
image = Image.open(data_bytes)
image_mode = image.mode
bucket_name = print_dict['print_path_list'][0].split("/", 1)[0]
object_name = print_dict['print_path_list'][0].split("/", 1)[1]
image = oss_get_image(bucket=bucket_name, object_name=object_name, data_type="cv2")
# 判断图片格式如果是RGBA 则贴在一张纯白图片上 防止透明转黑
if image_mode == "RGBA":
new_background = Image.new('RGB', image.size, (255, 255, 255))
new_background.paste(image, mask=image.split()[3])
if image.shape[2] == 4:
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGRA2RGBA)
image_pil = Image.fromarray(image_rgb)
new_background = Image.new('RGB', image_pil.size, (255, 255, 255))
new_background.paste(image_pil, mask=image.split()[3])
image = new_background
print_dict['image'] = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR)
@@ -577,21 +579,30 @@ class PrintPainting(object):
@staticmethod
def read_image(image_url):
data = minio_client.get_object(image_url.split("/", 1)[0], image_url.split("/", 1)[1])
# data = s3.get_object(Bucket=image_url.split("/", 1)[0], Key=image_url.split("/", 1)[1])['Body']
data_bytes = BytesIO(data.read())
image = Image.open(data_bytes)
image_mode = image.mode
# 判断图片格式如果是RGBA 则贴在一张纯白图片上 防止透明转黑
if image_mode == "RGBA":
# new_background = Image.new('RGB', image.size, (255, 255, 255))
# new_background.paste(image, mask=image.split()[3])
# image = new_background
return image, image_mode
image = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR)
image = oss_get_image(bucket=image_url.split("/", 1)[0], object_name=image_url.split("/", 1)[1], data_type="cv2")
if image.shape[2] == 4:
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGRA2RGBA)
image = Image.fromarray(image_rgb)
image_mode = "RGBA"
else:
image_mode = "RGB"
return image, image_mode
# data = minio_client.get_object(image_url.split("/", 1)[0], image_url.split("/", 1)[1])
# # data = s3.get_object(Bucket=image_url.split("/", 1)[0], Key=image_url.split("/", 1)[1])['Body']
#
# data_bytes = BytesIO(data.read())
# image = Image.open(data_bytes)
# image_mode = image.mode
# # 判断图片格式如果是RGBA 则贴在一张纯白图片上 防止透明转黑
# if image_mode == "RGBA":
# # new_background = Image.new('RGB', image.size, (255, 255, 255))
# # new_background.paste(image, mask=image.split()[3])
# # image = new_background
# return image, image_mode
# image = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR)
# return image, "RGB"
# @staticmethod
# def read_image(image_url):
# response = requests.get(image_url)