Downscale poster images before upload

This commit is contained in:
Phin 2024-02-10 12:42:58 +05:30
parent 69eef9e94a
commit aa6088ad23
7 changed files with 16 additions and 3 deletions

8
.github/release-notes/v2.5.0.md vendored Normal file
View file

@ -0,0 +1,8 @@
### Release Notes
* Poster images will now be downscaled to a maximum size of 256x256 before being uploaded (#78)
### Installation Instructions
* [Regular](https://github.com/phin05/discord-rich-presence-plex/blob/v2.5.0/README.md#installation)
* [Docker](https://github.com/phin05/discord-rich-presence-plex/blob/v2.5.0/README.md#run-with-docker)

View file

@ -41,6 +41,7 @@ The config file is stored in a directory named `data`.
* `posters` * `posters`
* `enabled` (boolean, default: `false`) - Displays media posters if enabled. Requires `imgurClientID`. * `enabled` (boolean, default: `false`) - Displays media posters if enabled. Requires `imgurClientID`.
* `imgurClientID` (string, default: `""`) - [Obtention Instructions](#obtaining-an-imgur-client-id) * `imgurClientID` (string, default: `""`) - [Obtention Instructions](#obtaining-an-imgur-client-id)
* `maxSize` (int, default: `256`)
* `buttons` (list) - [Information](#buttons) * `buttons` (list) - [Information](#buttons)
* `label` (string) - The label to be displayed on the button. * `label` (string) - The label to be displayed on the button.
* `url` (string) - A web address or a [dynamic URL placeholder](#dynamic-button-urls). * `url` (string) - A web address or a [dynamic URL placeholder](#dynamic-button-urls).

View file

@ -2,7 +2,7 @@ import os
import sys import sys
name = "Discord Rich Presence for Plex" name = "Discord Rich Presence for Plex"
version = "2.4.5" version = "2.5.0"
plexClientID = "discord-rich-presence-plex" plexClientID = "discord-rich-presence-plex"
discordClientID = "413407336082833418" discordClientID = "413407336082833418"

View file

@ -18,6 +18,7 @@ config: models.config.Config = {
"posters": { "posters": {
"enabled": False, "enabled": False,
"imgurClientID": "", "imgurClientID": "",
"maxSize": 256,
}, },
"buttons": [], "buttons": [],
}, },

View file

@ -6,12 +6,14 @@ import io
import models.imgur import models.imgur
import requests import requests
def uploadToImgur(url: str) -> Optional[str]: def uploadToImgur(url: str, maxSize: int = 0) -> Optional[str]:
try: try:
originalImageBytesIO = io.BytesIO(requests.get(url).content) originalImageBytesIO = io.BytesIO(requests.get(url).content)
originalImage = Image.open(originalImageBytesIO) originalImage = Image.open(originalImageBytesIO)
newImage = Image.new("RGB", originalImage.size) newImage = Image.new("RGB", originalImage.size)
newImage.putdata(originalImage.getdata()) # pyright: ignore[reportUnknownMemberType,reportUnknownArgumentType] newImage.putdata(originalImage.getdata()) # pyright: ignore[reportUnknownMemberType,reportUnknownArgumentType]
if maxSize:
newImage.thumbnail((maxSize, maxSize))
newImageBytesIO = io.BytesIO() newImageBytesIO = io.BytesIO()
newImage.save(newImageBytesIO, subsampling = 0, quality = 90, format = "JPEG") newImage.save(newImageBytesIO, subsampling = 0, quality = 90, format = "JPEG")
data: models.imgur.UploadResponse = requests.post( data: models.imgur.UploadResponse = requests.post(

View file

@ -229,7 +229,7 @@ class PlexAlertListener(threading.Thread):
thumbUrl = getCacheKey(thumb) thumbUrl = getCacheKey(thumb)
if not thumbUrl: if not thumbUrl:
self.logger.debug("Uploading image to Imgur") self.logger.debug("Uploading image to Imgur")
thumbUrl = uploadToImgur(self.server.url(thumb, True)) thumbUrl = uploadToImgur(self.server.url(thumb, True), config["display"]["posters"]["maxSize"])
setCacheKey(thumb, thumbUrl) setCacheKey(thumb, thumbUrl)
activity: models.discord.Activity = { activity: models.discord.Activity = {
"details": title[:128], "details": title[:128],

View file

@ -7,6 +7,7 @@ class Logging(TypedDict):
class Posters(TypedDict): class Posters(TypedDict):
enabled: bool enabled: bool
imgurClientID: str imgurClientID: str
maxSize: int
class Button(TypedDict): class Button(TypedDict):
label: str label: str