scripts/create_wallpaper.py

158 lines
5.7 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# make_wallpaper.py
# Scales picture to fit wallpaper and adds matching background
#
# License: GPLv3
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY# without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# To view a copy of the GNU General Public License
# visit: http://www.gnu.org/licenses/gpl.html
#
2020-04-03 00:38:14 +00:00
#
# ------------
#| Change Log |
# ------------
# Rel 1: Initial release.
import os
from gimpfu import *
def make_wallpaper(res_x, res_y, source_path, target_path):
# create new image in resolution size
res_x = int(res_x)
res_y = int(res_y)
2020-04-03 00:38:14 +00:00
img = gimp.Image(res_x, res_y)
bg = img.new_layer("bg", res_x, res_y)
pic = pdb.gimp_file_load_layer(img, source_path)
img.add_layer(pic, 0)
# scale picture down if it's too big for resolution
if pic.width > res_x or pic.height > res_y:
scale = 1.0 * res_x / pic.width
if scale is not 1.0 * res_y / pic.height:
scale_x = (res_x - 50.0) / pic.width
scale_y = (res_y - 50.0) / pic.height
scale = min(scale_x, scale_y)
new_x = int(pic.width * scale)
new_y = int(pic.height * scale)
pic.scale(new_x, new_y)
# get border average of image for 20px, 4 colors
if not(pic.width == res_x and pic.height == res_y):
pic.set_offsets((res_x - pic.width) // 2, (res_y - pic.height) // 2)
color, _ = pdb.plug_in_borderaverage(img, pic, 20, 4)
pdb.gimp_palette_set_foreground(color)
# fill bg layer with calculated average
bg.fill(FOREGROUND_FILL)
# desature for certain bgcolors
color_hsv = color.to_hsv()
if color_hsv[1] > 50 and color_hsv[2] > 50:
2020-04-03 00:38:14 +00:00
merged = img.merge_visible_layers(FALSE)
img.add_layer(pdb.gimp_layer_copy(merged, FALSE))
multi = img.layers[0]
multi.name = "multi"
pdb.gimp_desaturate(multi)
multi.opacity = 15.0
multi.mode = MULTIPLY_MODE
img.add_layer(pdb.gimp_layer_copy(multi, FALSE))
sat = img.layers[0]
sat.name = "sat"
sat.mode = SATURATION_MODE
2020-04-03 00:38:14 +00:00
final = img.merge_visible_layers(FALSE)
# save
target_file = target_path + ".jpg"
pdb.gimp_file_save(img, final, target_file, target_file)
def make_wallpaper_from_file(res_x, res_y, source_path, target_dir, target_filename):
if not target_dir or not target_filename:
source_parts = source_path.split("/")
if not target_dir:
target_dir = "/".join(source_parts[:len(source_parts) - 1])
if not target_filename:
target_filename = source_parts[len(source_parts) - 1]
target_path = target_dir + "/" + target_filename
make_wallpaper(res_x, res_y, source_path, target_path)
def make_wallpaper_batch(res_x, res_y, source_path, recursive, target_path, target_file_prefix, target_file_prefix_subdir, target_pad_number):
file_no = 0
for root, dirs, files in os.walk(source_path):
# set mask length with enough padding zeroes
if target_pad_number:
mask_len = len(str(len(files))) + 1
else:
mask_len = 0
prefix = target_file_prefix
if recursive and target_file_prefix_subdir:
prefix += str.replace(str.replace(root, " ", "_"), "/", "_") + "_"
# setup file names according to preferences
for name in files:
file_no += 1
file_str = prefix + ("0" * (max(0, mask_len - len(str(file_no))))) + str(file_no)
make_wallpaper(res_x, res_y, root + "/" + name, target_path + "/" + file_str)
if not recursive:
break
register(
"python_fu_make_wallpaper_batch",
"Iterate through given folder, scale each picture and add background for wallpaper",
"Iterate through given folder, scale each picture and add background for wallpaper",
"Sarah Ziesel",
"Sarah Ziesel",
"April 2020",
"<Toolbox>/Python-Fu/Wallpaper/Create Wallpapers From Folder...",
"",
[
(PF_SLIDER, "res_x", "Screen width", 2560, (1, 9999, 1)),
(PF_SLIDER, "res_y", "Screen height", 1440, (1, 9999, 1)),
(PF_DIRNAME, "source_path", "Source folder", "Pictures"),
(PF_BOOL, "recursive", "Go through subfolders recursively", FALSE),
(PF_DIRNAME, "target_path", "Desired target path", "Pictures"),
(PF_STRING, "target_file_prefix", "Naming prefix", ""),
(PF_BOOL, "target_file_prefix_subdir", "Prefix with sub directory name", TRUE),
(PF_BOOL, "target_pad_number", "Pad with leading zeroes", TRUE),
],
[],
make_wallpaper_batch)
register(
"python_fu_make_wallpaper_from_file",
"Scale picture and add background for wallpaper",
"Scales picture to fit wallpaper and adds matching background",
"Sarah Ziesel",
"Sarah Ziesel",
"April 2020",
"<Toolbox>/Python-Fu/Wallpaper/Create Wallpaper From File...",
2020-04-03 00:38:14 +00:00
"",
[
(PF_SLIDER, "res_x", "Screen width", 2560, (1, 9999, 1)),
(PF_SLIDER, "res_y", "Screen height", 1440, (1, 9999, 1)),
(PF_FILE, "source_path", "Source file", ""),
(PF_DIRNAME, "target_dir", "Target directory (will use source path if left blank)", ""),
(PF_STRING, "target_filename", "Target file name (will be equal to source file name if left blank)", "")
],
[],
make_wallpaper_from_file)
main()