Update bot to better handle image optimization.

This commit is contained in:
dotnet 2023-10-13 13:06:07 -04:00
parent b5c4e7bae6
commit 982cc9c42a
1 changed files with 20 additions and 12 deletions

View File

@ -90,10 +90,13 @@ class BotInstance:
if image_request.status_code != 200: if image_request.status_code != 200:
print("Error: " + image_request.json()["error"]["message"], file=log_file) print("Error: " + image_request.json()["error"]["message"], file=log_file)
return False return False
#Save image_request.content, based on image_url extension
with open("image_original" + os.path.splitext(image_url)[-1], "wb") as image_file:
image_file.write(image_request.content)
# Optimise the image by reducing it to max width of 2048px # Optimise the image by reducing it to max width of 2048px
image = Image.open(BytesIO(image_request.content)) image = Image.open(BytesIO(image_request.content))
#Save a copy of the original image
image.save("image_original.jpg")
if image.width > 2048: if image.width > 2048:
image = image.resize((2048, int(image.height * (2048 / image.width))), Image.Resampling.LANCZOS) image = image.resize((2048, int(image.height * (2048 / image.width))), Image.Resampling.LANCZOS)
# Apply JPEG compression # Apply JPEG compression
@ -101,17 +104,22 @@ class BotInstance:
image.save("image.jpg", optimize=True, quality=90) image.save("image.jpg", optimize=True, quality=90)
# If the image is larger than the original, use the original # If the image is larger than the original, use the original
if os.path.getsize("image.jpg") > os.path.getsize("image_original.jpg"): if os.path.getsize("image.jpg") > os.path.getsize("image_original" + os.path.splitext(image_url)[-1]):
os.remove("image.jpg") upload_from_url_request = requests.post(self.misskey_url + "drive/files/upload-from-url", json = {"url": image_url, "isSensitive": image_rating != 'general', "i": self.misskey_token})
os.rename("image_original.jpg", "image.jpg") # If error, print error and exit
if upload_from_url_request.status_code != 204 and upload_from_url_request.status_code != 200:
print("Error: " + upload_from_url_request.json()["error"]["message"], file=log_file)
return False
else:
# Submit a /drive/files/create request to Misskey
create_file_request = requests.post(self.misskey_url + "drive/files/create", data = {"name": os.path.split(image_url)[-1], "i": self.misskey_token, "isSensitive": str(image_rating != 'general').lower()}, files = {"file": open("image.jpg", "rb")})
# If error, print error and exit
if create_file_request.status_code != 200:
print("Error: " + create_file_request.json()["error"]["message"], file=log_file)
return False
# Submit a /drive/files/create request to Misskey os.remove("image.jpg")
create_file_request = requests.post(self.misskey_url + "drive/files/create", data = {"name": os.path.split(image_url)[-1], "i": self.misskey_token, "isSensitive": str(image_rating != 'general').lower()}, files = {"file": open("image.jpg", "rb")}) os.remove("image_original" + os.path.splitext(image_url)[-1])
# If error, print error and exit
if create_file_request.status_code != 200:
print("Error: " + create_file_request.json()["error"]["message"], file=log_file)
return False
else: else:
upload_from_url_request = requests.post(self.misskey_url + "drive/files/upload-from-url", json = {"url": image_url, "isSensitive": image_rating != 'general', "i": self.misskey_token}) upload_from_url_request = requests.post(self.misskey_url + "drive/files/upload-from-url", json = {"url": image_url, "isSensitive": image_rating != 'general', "i": self.misskey_token})
# If error, print error and exit # If error, print error and exit