Resize images consistently

This commit is contained in:
kramo
2023-04-17 14:21:37 +02:00
parent a6523aee16
commit 838460ce2a
5 changed files with 56 additions and 71 deletions

View File

@@ -27,6 +27,7 @@ import requests
from gi.repository import GdkPixbuf, Gio from gi.repository import GdkPixbuf, Gio
from .check_install import check_install from .check_install import check_install
from .save_cover import resize_cover
def get_game(task, current_time, win, row): def get_game(task, current_time, win, row):
@@ -106,7 +107,10 @@ def get_games_async(win, rows, importer):
def update_games(_task, result): def update_games(_task, result):
final_values = result.propagate_value()[1] final_values = result.propagate_value()[1]
# No need for an if statement as final_value would be None for games we don't want to save # No need for an if statement as final_value would be None for games we don't want to save
importer.save_game(final_values[0], pixbuf=final_values[1]) importer.save_game(
final_values[0],
resize_cover(win, pixbuf=final_values[1]) if final_values[1] else None,
)
for row in rows: for row in rows:
task = Gio.Task.new(None, None, update_games) task = Gio.Task.new(None, None, update_games)

View File

@@ -27,7 +27,7 @@ from PIL import Image
from .create_dialog import create_dialog from .create_dialog import create_dialog
from .game_cover import GameCover from .game_cover import GameCover
from .save_cover import img2tiff, resize_animation, save_cover from .save_cover import resize_cover, save_cover
from .save_game import save_game from .save_game import save_game
from .steamgriddb import SGDBSave from .steamgriddb import SGDBSave
@@ -233,13 +233,7 @@ def create_details_window(win, game_id=None):
cover_button_delete_revealer.set_reveal_child(True) cover_button_delete_revealer.set_reveal_child(True)
cover_changed = True cover_changed = True
with Image.open(path) as image: game_cover.new_pixbuf(resize_cover(win, path))
if getattr(image, "is_animated", False):
path = resize_animation(path)
else:
path = img2tiff(win, path)
game_cover.new_pixbuf(path)
def close_window(_widget, _callback=None): def close_window(_widget, _callback=None):
window.close() window.close()
@@ -326,9 +320,7 @@ def create_details_window(win, game_id=None):
save_cover( save_cover(
win, win,
game_id, game_id,
None, game_cover.path,
game_cover.get_pixbuf(),
game_cover.get_animation(),
) )
path = win.games_dir / f"{game_id}.json" path = win.games_dir / f"{game_id}.json"

View File

@@ -22,7 +22,7 @@ from pathlib import Path
from gi.repository import Adw, Gtk from gi.repository import Adw, Gtk
from .create_dialog import create_dialog from .create_dialog import create_dialog
from .save_cover import save_cover from .save_cover import resize_cover, save_cover
from .save_game import save_game from .save_game import save_game
from .steamgriddb import SGDBSave from .steamgriddb import SGDBSave
@@ -54,12 +54,14 @@ class Importer:
self.import_dialog.present() self.import_dialog.present()
def save_game(self, values=None, cover_path=None, pixbuf=None): def save_game(self, values=None, cover_path=None):
if values: if values:
save_game(self.win, values) save_game(self.win, values)
if cover_path or pixbuf: if cover_path:
save_cover(self.win, values["game_id"], cover_path, pixbuf) save_cover(
self.win, values["game_id"], resize_cover(self.win, cover_path)
)
self.games.add((values["game_id"], values["name"])) self.games.add((values["game_id"], values["name"]))

View File

@@ -21,66 +21,58 @@
from pathlib import Path from pathlib import Path
from shutil import copyfile from shutil import copyfile
from gi.repository import GdkPixbuf, Gio from gi.repository import Gio
from PIL import Image, ImageSequence from PIL import Image, ImageSequence
def img2tiff(win, cover_path): def resize_cover(win, cover_path=None, pixbuf=None):
tmp_path = Path(Gio.File.new_tmp("XXXXXX.tiff")[0].get_path()) if pixbuf:
cover_path = Path(Gio.File.new_tmp("XXXXXX.tiff")[0].get_path())
pixbuf.savev(str(cover_path), "tiff", ["compression"], ["1"])
with Image.open(cover_path) as image: with Image.open(cover_path) as image:
image.resize(win.image_size).save(tmp_path) if getattr(image, "is_animated", False):
frames = tuple(
frame.copy().resize((200, 300))
for frame in ImageSequence.Iterator(image)
)
tmp_path = Path(Gio.File.new_tmp("XXXXXX.gif")[0].get_path())
frames[0].save(
tmp_path,
save_all=True,
append_images=frames[1:],
)
else:
tmp_path = Path(Gio.File.new_tmp("XXXXXX.tiff")[0].get_path())
image.resize(win.image_size).save(
tmp_path,
compression="tiff_adobe_deflate"
if win.schema.get_boolean("high-quality-images")
else "jpeg",
)
return tmp_path return tmp_path
def resize_animation(cover_path): def save_cover(win, game_id, cover_path):
with Image.open(cover_path) as image:
frames = tuple(
frame.copy().resize((200, 300)) for frame in ImageSequence.Iterator(image)
)
tmp_path = Path(Gio.File.new_tmp("XXXXXX.gif")[0].get_path())
frames[0].save(
tmp_path,
save_all=True,
append_images=frames[1:],
)
return tmp_path
def save_cover(
win,
game_id,
cover_path=None,
pixbuf=None,
animation_path=None,
):
win.covers_dir.mkdir(parents=True, exist_ok=True) win.covers_dir.mkdir(parents=True, exist_ok=True)
animated_path = win.covers_dir / f"{game_id}.gif"
static_path = win.covers_dir / f"{game_id}.tiff"
# Remove previous covers # Remove previous covers
(win.covers_dir / f"{game_id}.tiff").unlink(missing_ok=True) (animated_path).unlink(missing_ok=True)
(win.covers_dir / f"{game_id}.gif").unlink(missing_ok=True) (static_path).unlink(missing_ok=True)
if animation_path: copyfile(
copyfile(animation_path, win.covers_dir / f"{game_id}.gif") cover_path,
if game_id in win.game_covers: animated_path if animated_path.is_file() else static_path,
win.game_covers[game_id].new_pixbuf(animation_path)
return
if not pixbuf:
if not cover_path:
return
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(
str(cover_path), *win.image_size, False
)
pixbuf.savev(
str(win.covers_dir / f"{game_id}.tiff"),
"tiff",
["compression"],
["8"] if win.schema.get_boolean("high-quality-images") else ["7"],
) )
if game_id in win.game_covers: if game_id in win.game_covers:
win.game_covers[game_id].new_pixbuf(win.covers_dir / f"{game_id}.tiff") win.game_covers[game_id].new_pixbuf(
animated_path if animated_path.is_file() else static_path
)
return

View File

@@ -4,7 +4,7 @@ import requests
from gi.repository import Gio from gi.repository import Gio
from .create_dialog import create_dialog from .create_dialog import create_dialog
from .save_cover import save_cover, resize_animation from .save_cover import save_cover, resize_cover
class SGDBSave: class SGDBSave:
@@ -59,7 +59,6 @@ class SGDBSave:
task.return_value(game[0]) task.return_value(game[0])
return return
animated_grid = False
response = None response = None
try: try:
@@ -73,7 +72,6 @@ class SGDBSave:
response = requests.get( response = requests.get(
grid.json()["data"][0]["url"], timeout=5 grid.json()["data"][0]["url"], timeout=5
) )
animated_grid = True
except IndexError: except IndexError:
pass pass
if not response: if not response:
@@ -93,10 +91,7 @@ class SGDBSave:
save_cover( save_cover(
self.win, self.win,
game[0], game[0],
tmp_file.get_path(), resize_cover(self.win, tmp_file.get_path()),
animation_path=resize_animation(tmp_file.get_path())
if animated_grid
else None,
) )
task.return_value(game[0]) task.return_value(game[0])