mirror of
https://github.com/Genshin-bots/gsuid_core.git
synced 2025-05-08 04:56:00 +08:00
🎨 支持传递GIF
格式的LINK_IMAGE
This commit is contained in:
parent
d10d5c8acf
commit
59f43ff54c
@ -255,7 +255,11 @@ async def _image_to_local_url(image: Union[bytes, str]) -> List[Message]:
|
|||||||
|
|
||||||
bio = BytesIO(image_bytes)
|
bio = BytesIO(image_bytes)
|
||||||
_image = Image.open(bio)
|
_image = Image.open(bio)
|
||||||
name = f'{uuid.uuid1()}.jpg'
|
if _image.format == 'GIF':
|
||||||
|
suffix = '.gif'
|
||||||
|
else:
|
||||||
|
suffix = '.jpg'
|
||||||
|
name = f'{uuid.uuid1()}.{suffix}'
|
||||||
path = image_res / name
|
path = image_res / name
|
||||||
path.write_bytes(image_bytes)
|
path.write_bytes(image_bytes)
|
||||||
data = f'link://{pic_srv}/genshinuid/image/{name}'
|
data = f'link://{pic_srv}/genshinuid/image/{name}'
|
||||||
|
@ -58,9 +58,18 @@ async def convert_img(
|
|||||||
logger.info('[GsCore] 处理图片中....')
|
logger.info('[GsCore] 处理图片中....')
|
||||||
|
|
||||||
if isinstance(img, Image.Image):
|
if isinstance(img, Image.Image):
|
||||||
img = img.convert('RGB')
|
if img.format == 'GIF':
|
||||||
result_buffer = BytesIO()
|
result_buffer = BytesIO()
|
||||||
img.save(result_buffer, format='JPEG', quality=pic_quality)
|
img.save(result_buffer, format='GIF')
|
||||||
|
else:
|
||||||
|
img = img.convert('RGB')
|
||||||
|
result_buffer = BytesIO()
|
||||||
|
img.save(
|
||||||
|
result_buffer,
|
||||||
|
format='JPEG',
|
||||||
|
quality=pic_quality,
|
||||||
|
)
|
||||||
|
|
||||||
res = result_buffer.getvalue()
|
res = result_buffer.getvalue()
|
||||||
if is_base64:
|
if is_base64:
|
||||||
res = 'base64://' + b64encode(res).decode()
|
res = 'base64://' + b64encode(res).decode()
|
||||||
@ -68,7 +77,7 @@ async def convert_img(
|
|||||||
elif isinstance(img, bytes):
|
elif isinstance(img, bytes):
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
async with aiofiles.open(img, 'rb') as fp:
|
async with aiofiles.open(Path(img), 'rb') as fp:
|
||||||
img = await fp.read()
|
img = await fp.read()
|
||||||
|
|
||||||
logger.success('[GsCore] 图片处理完成!')
|
logger.success('[GsCore] 图片处理完成!')
|
||||||
|
@ -10,7 +10,7 @@ from bs4 import Tag, BeautifulSoup
|
|||||||
from starlette.requests import Request
|
from starlette.requests import Request
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
from fastapi import FastAPI, BackgroundTasks
|
from fastapi import FastAPI, BackgroundTasks
|
||||||
from fastapi.responses import StreamingResponse
|
from fastapi.responses import Response, StreamingResponse
|
||||||
|
|
||||||
from gsuid_core.sv import SL
|
from gsuid_core.sv import SL
|
||||||
from gsuid_core.gss import gss
|
from gsuid_core.gss import gss
|
||||||
@ -413,10 +413,16 @@ app.mount(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.head('/genshinuid/image/{image_id}.jpg')
|
@app.head('/genshinuid/image/{image_id}')
|
||||||
@app.get('/genshinuid/image/{image_id}.jpg')
|
@app.get('/genshinuid/image/{image_id}')
|
||||||
async def get_image(image_id: str, background_tasks: BackgroundTasks):
|
async def get_image(image_id: str, background_tasks: BackgroundTasks):
|
||||||
path = image_res / f'{image_id}.jpg'
|
path = image_res / image_id
|
||||||
|
if not path.exists() and '.' not in image_id:
|
||||||
|
path = image_res / f'{image_id}.jpg'
|
||||||
|
|
||||||
|
if not path.exists():
|
||||||
|
return Response(status_code=404)
|
||||||
|
|
||||||
image = Image.open(path).convert('RGB')
|
image = Image.open(path).convert('RGB')
|
||||||
image_bytes = BytesIO()
|
image_bytes = BytesIO()
|
||||||
image.save(image_bytes, format='JPEG')
|
image.save(image_bytes, format='JPEG')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user