Spaces:
Running
Running
import concurrent.futures | |
import io | |
import os | |
import numpy as np | |
import oss2 | |
import requests | |
from PIL import Image, ImageDraw, ImageFont | |
from .log import logger | |
# oss | |
access_key_id = os.getenv("ACCESS_KEY_ID") | |
access_key_secret = os.getenv("ACCESS_KEY_SECRET") | |
bucket_name = os.getenv("BUCKET_NAME") | |
endpoint = os.getenv("ENDPOINT") | |
oss_path = "wangmeng.xwm/MemeMaster" | |
oss_path_img_gallery = "wangmeng.xwm/MemeMaster_img_gallery" | |
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name) | |
def download_img_pil(index, img_url): | |
# print(img_url) | |
r = requests.get(img_url, stream=True) | |
if r.status_code == 200: | |
img = Image.open(io.BytesIO(r.content)) | |
return (index, img) | |
else: | |
logger.error(f"Fail to download: {img_url}") | |
def download_images(img_urls, batch_size): | |
imgs_pil = [None] * batch_size | |
# worker_results = [] | |
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor: | |
to_do = [] | |
for i, url in enumerate(img_urls): | |
future = executor.submit(download_img_pil, i, url) | |
to_do.append(future) | |
for future in concurrent.futures.as_completed(to_do): | |
ret = future.result() | |
# worker_results.append(ret) | |
index, img_pil = ret | |
imgs_pil[index] = img_pil # 按顺序排列url,后续下载关联的图片或者svg需要使用 | |
return imgs_pil | |
def upload_np_2_oss(input_image, name="cache.png", gallery=False): | |
imgByteArr = io.BytesIO() | |
Image.fromarray(input_image).save(imgByteArr, format="PNG") | |
imgByteArr = imgByteArr.getvalue() | |
if gallery: | |
path = oss_path_img_gallery | |
else: | |
path = oss_path | |
bucket.put_object(path + "/" + name, imgByteArr) # data为数据,可以是图片 | |
ret = bucket.sign_url('GET', path + "/" + name, 60 * 60 * 24) # 返回值为链接,参数依次为,方法/oss上文件路径/过期时间(s) | |
del imgByteArr | |
return ret | |