mirror of
https://github.com/vincentbernat/i3wm-configuration.git
synced 2025-08-09 20:54:35 +02:00
wallpaper: store number of time an image was used to select it less
This commit is contained in:
parent
da10242996
commit
4924483665
1 changed files with 34 additions and 2 deletions
|
@ -23,6 +23,7 @@ import itertools
|
||||||
import logging
|
import logging
|
||||||
import logging.handlers
|
import logging.handlers
|
||||||
import inspect
|
import inspect
|
||||||
|
import xattr
|
||||||
|
|
||||||
from Xlib import display
|
from Xlib import display
|
||||||
from Xlib.ext import randr
|
from Xlib.ext import randr
|
||||||
|
@ -202,11 +203,26 @@ def get_covering_rectangles(outputs: list[Rectangle]) -> set[tuple[Rectangle, ..
|
||||||
def get_random_images(directory: str, number: int) -> list[Image]:
|
def get_random_images(directory: str, number: int) -> list[Image]:
|
||||||
"""Get random images from a directory."""
|
"""Get random images from a directory."""
|
||||||
image_files = []
|
image_files = []
|
||||||
|
weights = []
|
||||||
|
counts = []
|
||||||
for base, _, files in os.walk(os.path.join(directory)):
|
for base, _, files in os.walk(os.path.join(directory)):
|
||||||
for i in files:
|
for i in files:
|
||||||
if os.path.splitext(i)[1].lower() in (".jpg", ".jpeg", ".png", ".webp"):
|
if os.path.splitext(i)[1].lower() in (".jpg", ".jpeg", ".png", ".webp"):
|
||||||
image_files.append(os.path.join(base, i))
|
filename = os.path.join(base, i)
|
||||||
images = [PIL.Image.open(image) for image in random.sample(image_files, number)]
|
image_files.append(filename)
|
||||||
|
if options.count_attribute:
|
||||||
|
try:
|
||||||
|
count = int(
|
||||||
|
xattr.get(filename, options.count_attribute).decode()
|
||||||
|
)
|
||||||
|
except (OSError, ValueError):
|
||||||
|
count = 0
|
||||||
|
counts.append(count)
|
||||||
|
weights = [1 / (count + 1) for count in counts]
|
||||||
|
images = [
|
||||||
|
PIL.Image.open(image)
|
||||||
|
for image in random.choices(image_files, k=number, weights=weights)
|
||||||
|
]
|
||||||
|
|
||||||
for image in images:
|
for image in images:
|
||||||
directory_len = len(directory) + 1
|
directory_len = len(directory) + 1
|
||||||
|
@ -356,6 +372,12 @@ if __name__ == "__main__":
|
||||||
metavar="N",
|
metavar="N",
|
||||||
help="consider N additional images to choose the best combination",
|
help="consider N additional images to choose the best combination",
|
||||||
)
|
)
|
||||||
|
group.add_argument(
|
||||||
|
"--count-attribute",
|
||||||
|
default="user.count",
|
||||||
|
metavar="ATTR",
|
||||||
|
help="store number of times an image was used in the provided attribute",
|
||||||
|
)
|
||||||
params = inspect.signature(get_best_parts).parameters
|
params = inspect.signature(get_best_parts).parameters
|
||||||
group.add_argument(
|
group.add_argument(
|
||||||
"--ratio-score",
|
"--ratio-score",
|
||||||
|
@ -423,6 +445,16 @@ if __name__ == "__main__":
|
||||||
*part.image.size
|
*part.image.size
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
if options.count_attribute:
|
||||||
|
try:
|
||||||
|
count = int(xattr.get(part.image.filename, options.count_attribute))
|
||||||
|
except (OSError, ValueError):
|
||||||
|
count = 0
|
||||||
|
xattr.set(
|
||||||
|
part.image.filename,
|
||||||
|
options.count_attribute,
|
||||||
|
bytes(str(count + 1), "ascii"),
|
||||||
|
)
|
||||||
build(background, wallpaper_parts)
|
build(background, wallpaper_parts)
|
||||||
save(background, options.target, options.compression)
|
save(background, options.target, options.compression)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue