mirror of
https://github.com/Kakune55/ComiPy.git
synced 2025-05-06 18:29:26 +08:00
feat:将图像处理库从PIL改为OpenCV以优化性能
This commit is contained in:
parent
e44fa7fb8e
commit
007645e94d
40
file.py
40
file.py
@ -1,9 +1,10 @@
|
|||||||
import shutil, os, zipfile, io
|
import shutil, os, zipfile, io, cv2, numpy as np
|
||||||
|
|
||||||
import db.file, app_conf
|
import db.file, app_conf
|
||||||
from PIL import Image
|
|
||||||
|
|
||||||
app_conf = app_conf.conf()
|
app_conf = app_conf.conf()
|
||||||
|
|
||||||
|
|
||||||
def init():
|
def init():
|
||||||
paths = ("inputdir", "storedir", "tmpdir")
|
paths = ("inputdir", "storedir", "tmpdir")
|
||||||
for path in paths:
|
for path in paths:
|
||||||
@ -66,27 +67,14 @@ def raedZip(bookid: str, index: int):
|
|||||||
return str(e), ""
|
return str(e), ""
|
||||||
|
|
||||||
|
|
||||||
def thumbnail(input,size=(420,600)):
|
def thumbnail(input, minSize: int = 600):
|
||||||
im = Image.open(io.BytesIO(input))
|
img = cv2.imdecode(np.frombuffer(input, np.uint8), cv2.IMREAD_COLOR)
|
||||||
del input
|
height = img.shape[0] # 图片高度
|
||||||
newimg = im.convert('RGB')
|
width = img.shape[1] # 图片宽度
|
||||||
im.close()
|
if height < width:
|
||||||
newimg.thumbnail(size)
|
newshape = (int(minSize / width * height), minSize)
|
||||||
output_io = io.BytesIO()
|
else:
|
||||||
newimg.save(output_io,format='WEBP')
|
newshape = (minSize, int(minSize / width * height))
|
||||||
newimg.close()
|
img = cv2.resize(img, newshape)
|
||||||
output_io.seek(0)
|
success, encoded_image = cv2.imencode(".webp", img, [cv2.IMWRITE_WEBP_QUALITY, 75])
|
||||||
return output_io
|
return encoded_image.tobytes()
|
||||||
|
|
||||||
def imageToWebP(input,size=(2100,3000)):
|
|
||||||
with Image.open(io.BytesIO(input)) as img:
|
|
||||||
newimg = img.convert('RGB')
|
|
||||||
img.close()
|
|
||||||
output_io = io.BytesIO()
|
|
||||||
newimg.thumbnail(size)
|
|
||||||
newimg.save(output_io,format='WEBP')
|
|
||||||
newimg.close()
|
|
||||||
output_io.seek(0)
|
|
||||||
return output_io
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
shortuuid
|
shortuuid
|
||||||
flask
|
flask
|
||||||
Pillow
|
opencv-python
|
@ -18,7 +18,7 @@ def img(bookid, index): # 图片接口
|
|||||||
if request.args.get("mini") == "yes":
|
if request.args.get("mini") == "yes":
|
||||||
data = file.thumbnail(data)
|
data = file.thumbnail(data)
|
||||||
else:
|
else:
|
||||||
data = file.imageToWebP(data)
|
data = file.thumbnail(data,2400)
|
||||||
response = make_response(data) # 读取文件
|
response = make_response(data) # 读取文件
|
||||||
del data
|
del data
|
||||||
response.headers.set("Content-Type", "image/Webp")
|
response.headers.set("Content-Type", "image/Webp")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user