diff --git a/src/importers/itch_importer.py b/src/importers/itch_importer.py index 6aae018..1cba187 100644 --- a/src/importers/itch_importer.py +++ b/src/importers/itch_importer.py @@ -27,6 +27,7 @@ import requests from gi.repository import GdkPixbuf, Gio from .check_install import check_install +from .save_cover import resize_cover def get_game(task, current_time, win, row): @@ -106,7 +107,10 @@ def get_games_async(win, rows, importer): def update_games(_task, result): 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 - 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: task = Gio.Task.new(None, None, update_games) diff --git a/src/utils/create_details_window.py b/src/utils/create_details_window.py index 5d0909f..8e83cbb 100644 --- a/src/utils/create_details_window.py +++ b/src/utils/create_details_window.py @@ -27,7 +27,7 @@ from PIL import Image from .create_dialog import create_dialog 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 .steamgriddb import SGDBSave @@ -233,13 +233,7 @@ def create_details_window(win, game_id=None): cover_button_delete_revealer.set_reveal_child(True) cover_changed = True - with Image.open(path) as image: - if getattr(image, "is_animated", False): - path = resize_animation(path) - else: - path = img2tiff(win, path) - - game_cover.new_pixbuf(path) + game_cover.new_pixbuf(resize_cover(win, path)) def close_window(_widget, _callback=None): window.close() @@ -326,9 +320,7 @@ def create_details_window(win, game_id=None): save_cover( win, game_id, - None, - game_cover.get_pixbuf(), - game_cover.get_animation(), + game_cover.path, ) path = win.games_dir / f"{game_id}.json" diff --git a/src/utils/importer.py b/src/utils/importer.py index 4e5899a..b2b0d80 100644 --- a/src/utils/importer.py +++ b/src/utils/importer.py @@ -22,7 +22,7 @@ from pathlib import Path from gi.repository import Adw, Gtk 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 .steamgriddb import SGDBSave @@ -54,12 +54,14 @@ class Importer: 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: save_game(self.win, values) - if cover_path or pixbuf: - save_cover(self.win, values["game_id"], cover_path, pixbuf) + if cover_path: + save_cover( + self.win, values["game_id"], resize_cover(self.win, cover_path) + ) self.games.add((values["game_id"], values["name"])) diff --git a/src/utils/save_cover.py b/src/utils/save_cover.py index 41f1227..f373390 100644 --- a/src/utils/save_cover.py +++ b/src/utils/save_cover.py @@ -21,66 +21,58 @@ from pathlib import Path from shutil import copyfile -from gi.repository import GdkPixbuf, Gio +from gi.repository import Gio from PIL import Image, ImageSequence -def img2tiff(win, cover_path): - tmp_path = Path(Gio.File.new_tmp("XXXXXX.tiff")[0].get_path()) +def resize_cover(win, cover_path=None, pixbuf=None): + 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: - 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 -def resize_animation(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, -): +def save_cover(win, game_id, cover_path): 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 - (win.covers_dir / f"{game_id}.tiff").unlink(missing_ok=True) - (win.covers_dir / f"{game_id}.gif").unlink(missing_ok=True) + (animated_path).unlink(missing_ok=True) + (static_path).unlink(missing_ok=True) - if animation_path: - copyfile(animation_path, win.covers_dir / f"{game_id}.gif") - if game_id in win.game_covers: - 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"], + copyfile( + cover_path, + animated_path if animated_path.is_file() else static_path, ) 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 diff --git a/src/utils/steamgriddb.py b/src/utils/steamgriddb.py index a000ed2..fc2bdac 100644 --- a/src/utils/steamgriddb.py +++ b/src/utils/steamgriddb.py @@ -4,7 +4,7 @@ import requests from gi.repository import Gio from .create_dialog import create_dialog -from .save_cover import save_cover, resize_animation +from .save_cover import save_cover, resize_cover class SGDBSave: @@ -59,7 +59,6 @@ class SGDBSave: task.return_value(game[0]) return - animated_grid = False response = None try: @@ -73,7 +72,6 @@ class SGDBSave: response = requests.get( grid.json()["data"][0]["url"], timeout=5 ) - animated_grid = True except IndexError: pass if not response: @@ -93,10 +91,7 @@ class SGDBSave: save_cover( self.win, game[0], - tmp_file.get_path(), - animation_path=resize_animation(tmp_file.get_path()) - if animated_grid - else None, + resize_cover(self.win, tmp_file.get_path()), ) task.return_value(game[0])