mirror of
				https://github.com/Kakune55/ComiPy.git
				synced 2025-11-04 14:34:40 +08:00 
			
		
		
		
	fix:提升缩略图处理速度
This commit is contained in:
		
							
								
								
									
										18
									
								
								file.py
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								file.py
									
									
									
									
									
								
							@@ -67,14 +67,20 @@ def raedZip(bookid: str, index: int):
 | 
				
			|||||||
        return str(e), ""
 | 
					        return str(e), ""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def thumbnail(input, minSize: int = 600):
 | 
					def thumbnail(input, minSize: int = 600, encode:str="webp"):
 | 
				
			||||||
    img = cv2.imdecode(np.frombuffer(input, np.uint8), cv2.IMREAD_COLOR)
 | 
					    img = cv2.imdecode(np.frombuffer(input, np.uint8), cv2.IMREAD_COLOR)
 | 
				
			||||||
    height = img.shape[0]  # 图片高度
 | 
					    height = img.shape[0]  # 图片高度
 | 
				
			||||||
    width = img.shape[1]  # 图片宽度
 | 
					    width = img.shape[1]  # 图片宽度
 | 
				
			||||||
    if height > width:
 | 
					    if minSize < np.amin((height,width)):
 | 
				
			||||||
        newshape = (minSize, int(minSize / width * height))
 | 
					        if height > width:
 | 
				
			||||||
 | 
					            newshape = (minSize, int(minSize / width * height))
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
 | 
					            newshape = (int(minSize / height * width), minSize)
 | 
				
			||||||
 | 
					        img = cv2.resize(img, newshape)
 | 
				
			||||||
 | 
					    if encode == "webp":
 | 
				
			||||||
 | 
					        success, encoded_image = cv2.imencode(".webp", img, [cv2.IMWRITE_WEBP_QUALITY, 75])
 | 
				
			||||||
 | 
					    elif encode == "jpg":
 | 
				
			||||||
 | 
					        success, encoded_image = cv2.imencode(".jpg", img, [cv2.IMWRITE_JPEG_QUALITY, 75])
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        newshape = (int(minSize / height * width), minSize)
 | 
					        return input
 | 
				
			||||||
    img = cv2.resize(img, newshape)
 | 
					 | 
				
			||||||
    success, encoded_image = cv2.imencode(".webp", img, [cv2.IMWRITE_WEBP_QUALITY, 75])
 | 
					 | 
				
			||||||
    return encoded_image.tobytes()
 | 
					    return encoded_image.tobytes()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -16,9 +16,9 @@ def img(bookid, index):  # 图片接口
 | 
				
			|||||||
    if isinstance(data, str):
 | 
					    if isinstance(data, str):
 | 
				
			||||||
        abort(404)
 | 
					        abort(404)
 | 
				
			||||||
    if request.args.get("mini") == "yes":
 | 
					    if request.args.get("mini") == "yes":
 | 
				
			||||||
        data = file.thumbnail(data)
 | 
					        data = file.thumbnail(data,encode="jpg")
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        data = file.thumbnail(data,2400)
 | 
					        data = file.thumbnail(data,1500,)
 | 
				
			||||||
    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")
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user