Skip to content

Commit

Permalink
Implementing caching
Browse files Browse the repository at this point in the history
  • Loading branch information
anufrievroman committed Aug 20, 2023
1 parent 5b886d4 commit 09bdfaa
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 33 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ GUI wallpaper setter for both Wayland and X11 window managers that works as a fr
- GUI wallpaper selection
- Works on both Wayland (with `swaybg` or `swww`) and X11 (with `feh`)
- Restores wallpaper at launch of your WM
- Caching for fast loading

## Installation

Expand All @@ -19,17 +20,15 @@ You need to install at least one of the backends and Waypaper, which works as a

Install a preferred backend from your package manager: [swaybg](https://github.com/swaywm/swaybg) or [swww](https://github.com/Horus645/swww) on Wayland or [feh](https://github.com/derf/feh) on x11. You can also install and test all of them.

- [swaybg](https://github.com/swaywm/swaybg) - the wayland backend that supports only static images.
- [swww](https://github.com/Horus645/swww) - the wayland backend that also supports animated GIFs.
- [feh](https://github.com/derf/feh) - the x11 backend that supports static images.
### 2. Install Waypaper

### 2. Install Waypaper (from PyPi)
#### From PyPi

`pipx install waypaper`

If `pipx` is not found, you first need to install `pipx` from your package manager, it's sometimes called `python-pipx`.

### 2. Install Waypaper (from AUR)
#### From AUR

[waypaper-git](https://aur.archlinux.org/packages/waypaper-git) package is available in AUR, thanks to *metak*. So, on arch-based system, you can install it as:

Expand Down
Empty file added temp.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion waypaper/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from waypaper.arguments import args


__version__ = "1.5.1"
__version__ = "1.6"


def run():
Expand Down
66 changes: 39 additions & 27 deletions waypaper/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import os
import subprocess
import configparser

import distutils.spawn

gi.require_version("Gtk", "3.0")
Expand All @@ -16,6 +15,33 @@
from waypaper.options import FILL_OPTIONS, BACKEND_OPTIONS


def get_image_paths(root_folder, include_subfolders=False, depth=None):
"""Get a list of file paths depending of weather we include subfolders and how deep we scan"""
image_paths = []
for root, directories, files in os.walk(root_folder):
if not include_subfolders and root != root_folder:
continue
if depth is not None and root != root_folder:
current_depth = root.count(os.path.sep) - root_folder.count(os.path.sep)
if current_depth > depth:
continue
for filename in files:
if filename.endswith(".jpg") or filename.endswith(".png") or filename.endswith(".gif"):
image_paths.append(os.path.join(root, filename))
return image_paths


def cache_image(image_path):
"""Resize and cache images using gtk library"""
pixbuf = GdkPixbuf.Pixbuf.new_from_file(image_path)
aspect_ratio = pixbuf.get_width() / pixbuf.get_height()
scaled_width = 240
scaled_height = int(scaled_width / aspect_ratio)
scaled_pixbuf = pixbuf.scale_simple(scaled_width, scaled_height, GdkPixbuf.InterpType.BILINEAR)
output_file = f"{cf.config_folder}/.cache/{os.path.basename(image_path)}"
scaled_pixbuf.savev(output_file, "jpeg", [], [])


class App(Gtk.Window):
"""Main application class that controls GUI"""

Expand Down Expand Up @@ -154,28 +180,13 @@ def show_no_backend_message(self, message):
dialog.destroy()


def get_image_paths(self, root_folder, include_subfolders=False, depth=None):
"""Get a list of file paths depending of weather we include subfolders and how deep we scan"""
self.image_paths = []
for root, directories, files in os.walk(root_folder):
if not include_subfolders and root != root_folder:
continue
if depth is not None and root != root_folder:
current_depth = root.count(os.path.sep) - root_folder.count(os.path.sep)
if current_depth > depth:
continue
for filename in files:
if filename.endswith(".jpg") or filename.endswith(".png") or filename.endswith(".gif"):
self.image_paths.append(os.path.join(root, filename))


def process_images(self):
"""Load images from the selected folder, resize them, and arrange into a grid"""

self.get_image_paths(cf.image_folder, cf.include_subfolders, depth=1)
self.image_paths = get_image_paths(cf.image_folder, cf.include_subfolders, depth=1)

# Show loading label:
self.loading_label = Gtk.Label(label=f"Loading {len(self.image_paths)} wallpapers...")
# Show caching label:
self.loading_label = Gtk.Label(label=f"Caching {len(self.image_paths)} wallpapers...")
self.bottom_loading_box.add(self.loading_label)
self.show_all()

Expand All @@ -184,16 +195,17 @@ def process_images(self):

for image_path in self.image_paths:

# Load and scale the image:
pixbuf = GdkPixbuf.Pixbuf.new_from_file(image_path)
aspect_ratio = pixbuf.get_width() / pixbuf.get_height()
scaled_width = 240
scaled_height = int(scaled_width / aspect_ratio)
scaled_pixbuf = pixbuf.scale_simple(scaled_width, scaled_height, GdkPixbuf.InterpType.BILINEAR)
self.thumbnails.append(scaled_pixbuf)
# If this image is not cached yet, resize and cache it:
if not os.path.exists(f"{cf.config_folder}/.cache/{os.path.basename(image_path)}"):
cache_image(image_path)

# Load cached thumbnail:
cached_image_path = f"{cf.config_folder}/.cache/{os.path.basename(image_path)}"
thumbnail = GdkPixbuf.Pixbuf.new_from_file(cached_image_path)
self.thumbnails.append(thumbnail)
self.image_names.append(os.path.basename(image_path))

# When image processing is done, remove loading label and display the images:
# When image processing is done, remove caching label and display the images:
self.bottom_loading_box.remove(self.loading_label)
GLib.idle_add(self.load_image_grid)

Expand Down
4 changes: 4 additions & 0 deletions waypaper/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ def read_parameters_from_user_arguments(self):
if not os.path.exists(cf.config_folder):
os.makedirs(cf.config_folder)

# Create cache folder:
if not os.path.exists(f"{cf.config_folder}/.cache"):
os.makedirs(f"{cf.config_folder}/.cache")

# Create config file:
if not os.path.exists(cf.config_file):
cf.create()
Expand Down

0 comments on commit 09bdfaa

Please sign in to comment.