This commit is contained in:
2024-11-11 17:49:16 +08:00
parent 3259443c6d
commit 7d153debdf

View File

@@ -42,16 +42,16 @@ async def rewrite_image(image_id: int):
with pool.connection() as conn: with pool.connection() as conn:
with conn.cursor() as cursor: with conn.cursor() as cursor:
print('START', image_id, '重建向量') print('START', image_id, '重建向量')
cursor.execute(f"SELECT * FROM `web_images` WHERE id={image_id}") cursor.execute(f"SELECT content FROM `web_images` WHERE id={image_id}")
img = cursor.fetchone() img = cursor.fetchone()
if img is None: if img is None:
print('mysql中原始图片不存在:', image_id) print('mysql中原始图片不存在:', image_id)
return Response('图片不存在', status_code=404) return Response('图片不存在', status_code=404)
img_path = os.path.join(UPLOAD_PATH, os.path.basename(img['content'])) img_path = os.path.join(UPLOAD_PATH, os.path.basename(img[0]))
print('img_path', img_path) print('img_path', img_path)
image = download_image(img['content']) image = download_image(img[0])
if image is None: if image is None:
print('图片下载失败:', img['content']) print('图片下载失败:', img[0])
return Response('图片下载失败', status_code=404) return Response('图片下载失败', status_code=404)
image.save(img_path, 'png', save_all=True) image.save(img_path, 'png', save_all=True)
with Image.open(img_path) as imgx: with Image.open(img_path) as imgx:
@@ -73,15 +73,15 @@ async def similar_images(image_id: int, page: int = 1, pageSize: int = 20):
# 如果没有结果, 则重新生成记录 # 如果没有结果, 则重新生成记录
if len(result) == 0: if len(result) == 0:
with conn.cursor() as cursor: with conn.cursor() as cursor:
cursor.execute(f"SELECT * FROM `web_images` WHERE id={image_id}") cursor.execute(f"SELECT content FROM `web_images` WHERE id={image_id}")
img = cursor.fetchone() img = cursor.fetchone()
if img is None: if img is None:
print('mysql 中图片不存在:', image_id) print('mysql 中图片不存在:', image_id)
return Response('图片不存在', status_code=404) return Response('图片不存在', status_code=404)
img_path = os.path.join(UPLOAD_PATH, os.path.basename(img['content'])) img_path = os.path.join(UPLOAD_PATH, os.path.basename(img[0]))
image = download_image(img['content']) image = download_image(img[0])
if image is None: if image is None:
print('图片下载失败:', img['content']) print('图片下载失败:', img[0])
return Response('图片下载失败', status_code=404) return Response('图片下载失败', status_code=404)
image.save(img_path, 'png', save_all=True) image.save(img_path, 'png', save_all=True)
with Image.open(img_path) as imgx: with Image.open(img_path) as imgx: