general: Split image functions into own module and remove un-needed pathlib imports.

This commit is contained in:
Dylan Araps 2017-06-30 10:07:42 +10:00
parent 615321de94
commit 6c08e44ca4
4 changed files with 55 additions and 49 deletions

View File

@ -9,6 +9,7 @@ import sys
from pywal.settings import CACHE_DIR, __version__
from pywal import export
from pywal import image
from pywal import gen_colors
from pywal import set_colors
from pywal import reload
@ -87,13 +88,13 @@ def process_args(args):
# -i
if args.i:
image = gen_colors.get_image(args.i)
image_file = image.get_image(args.i)
# Create a list of hex colors.
colors_plain = gen_colors.get_colors(image, args.q)
colors_plain = gen_colors.get_colors(image_file, args.q)
if not args.n:
wallpaper.set_wallpaper(image)
wallpaper.set_wallpaper(image_file)
# -f
elif args.f:

View File

@ -1,9 +1,6 @@
"""
Generate a colorscheme.
"""
import os
import pathlib
import random
import re
import shutil
import subprocess
@ -13,45 +10,6 @@ from pywal import set_colors
from pywal import util
def random_img(img_dir):
"""Pick a random image file from a directory."""
current_wall = pathlib.Path(CACHE_DIR / "wal")
if current_wall.is_file():
current_wall = util.read_file(current_wall)
current_wall = os.path.basename(current_wall[0])
# Add all images to a list excluding the current wallpaper.
file_types = (".png", ".jpg", ".jpeg", ".jpe", ".gif")
images = [img for img in os.scandir(img_dir)
if img.name.endswith(file_types) and img.name != current_wall]
# If no images are found, use the current wallpaper.
if not images:
print("image: No new images found (nothing to do), exiting...")
quit(1)
return pathlib.Path(img_dir / random.choice(images).name)
def get_image(img):
"""Validate image input."""
image = pathlib.Path(img)
if image.is_file():
wal_img = image
elif image.is_dir():
wal_img = random_img(image)
else:
print("error: No valid image file found.")
exit(1)
print("image: Using image", wal_img)
return str(wal_img)
def imagemagick(color_count, img):
"""Call Imagemagick to generate a scheme."""
colors = subprocess.Popen(["convert", img, "+dither", "-colors",
@ -96,8 +54,8 @@ def get_colors(img, quiet):
util.save_file(img, CACHE_DIR / "wal")
# Cache the sequences file.
cache_file = pathlib.Path(CACHE_DIR / "schemes" / img.replace("/", "_"))
cache_file = pathlib.Path(cache_file.with_suffix(".json"))
cache_file = CACHE_DIR / "schemes" / img.replace("/", "_")
cache_file = cache_file.with_suffix(".json")
if cache_file.is_file():
colors = util.read_file_json(cache_file)

48
pywal/image.py Normal file
View File

@ -0,0 +1,48 @@
"""
Get the image file.
"""
import os
import pathlib
import random
from pywal.settings import CACHE_DIR
from pywal import util
def get_random_image(img_dir):
"""Pick a random image file from a directory."""
current_wall = CACHE_DIR / "wal"
if current_wall.is_file():
current_wall = util.read_file(current_wall)
current_wall = os.path.basename(current_wall[0])
# Add all images to a list excluding the current wallpaper.
file_types = (".png", ".jpg", ".jpeg", ".jpe", ".gif")
images = [img for img in os.scandir(img_dir)
if img.name.endswith(file_types) and img.name != current_wall]
# If no images are found, use the current wallpaper.
if not images:
print("image: No new images found (nothing to do), exiting...")
quit(1)
return img_dir / random.choice(images).name
def get_image(img):
"""Validate image input."""
image = pathlib.Path(img)
if image.is_file():
wal_img = image
elif image.is_dir():
wal_img = get_random_image(image)
else:
print("error: No valid image file found.")
exit(1)
print("image: Using image", wal_img)
return str(wal_img)

View File

@ -2,7 +2,6 @@
Send sequences to all open terminals.
"""
import os
import pathlib
import re
from pywal.settings import CACHE_DIR
@ -72,7 +71,7 @@ def send_sequences(colors, vte):
def reload_colors(vte):
"""Reload colors."""
sequence_file = pathlib.Path(CACHE_DIR / "sequences")
sequence_file = CACHE_DIR / "sequences"
if sequence_file.is_file():
sequences = "".join(util.read_file(sequence_file))