Modify ratio calculations to get better accuracy

This commit is contained in:
Zeva Rose 2021-06-26 23:25:17 -06:00
parent 94d0594361
commit 2c4cefd1f8

View file

@ -67,8 +67,10 @@ class ImageCog(commands.Cog):
ratio = tgt_size / size - 0.02 ratio = tgt_size / size - 0.02
accuracy = 0.0
# TODO: Optimize to not run multiple times # TODO: Optimize to not run multiple times
while len(file) > tgt_size: while len(file) > tgt_size:
old_file = file
buffer = np.frombuffer(file, dtype=np.uint8) buffer = np.frombuffer(file, dtype=np.uint8)
img = cv2.imdecode(buffer, flags=-1) img = cv2.imdecode(buffer, flags=-1)
@ -78,7 +80,12 @@ class ImageCog(commands.Cog):
new_img = cv2.resize(img, (width, height)) new_img = cv2.resize(img, (width, height))
file = cv2.imencode(".png", new_img)[1].tobytes() file = cv2.imencode(".png", new_img)[1].tobytes()
ratio = tgt_size / len(file) - 0.02 accuracy = (len(file) / tgt_size) * 100
if accuracy <= 0.50:
file = old_file
ratio = ratio + 0.1
else:
ratio = tgt_size / len(file) - 0.02
bufio = BytesIO(file) bufio = BytesIO(file)
accuracy = (len(file) / tgt_size) * 100 accuracy = (len(file) / tgt_size) * 100