Fix scaling; add sub-methods for single file and batch conversion
This commit is contained in:
parent
65df52ba90
commit
32084ab57a
@ -23,34 +23,34 @@
|
||||
# ------------
|
||||
# Rel 1: Initial release.
|
||||
|
||||
import os
|
||||
from gimpfu import *
|
||||
|
||||
def python_fu_make_wallpaper(resX, resY, sourceFile, targetFile) :
|
||||
def make_wallpaper(res_x, res_y, source_path, target_path):
|
||||
# create new image in resolution size
|
||||
resX = int(resX)
|
||||
resY = int(resY)
|
||||
res_x = int(res_x)
|
||||
res_y = int(res_y)
|
||||
|
||||
img = gimp.Image(resX, resY)
|
||||
img = gimp.Image(res_x, res_y)
|
||||
|
||||
bg = img.new_layer("bg", resX, resY)
|
||||
pic = pdb.gimp_file_load_layer(img, sourceFile)
|
||||
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 > resX or pic.height > resY:
|
||||
scaleX = (resX - 50.0) / pic.width
|
||||
scaleY = (resY - 50.0) / pic.height
|
||||
scale = min(scaleX, scaleY)
|
||||
x0 = int((resX - pic.width * scale) // 2)
|
||||
y0 = int((resY - pic.height * scale) // 2)
|
||||
x1 = resX - x0
|
||||
y1 = resY - y0
|
||||
pic.scale(x0, y0, x1, y1)
|
||||
|
||||
pic.set_offsets((resX-pic.width)/2,(resY-pic.height)/2)
|
||||
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 == resX and pic.height == resY):
|
||||
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)
|
||||
|
||||
@ -58,8 +58,8 @@ def python_fu_make_wallpaper(resX, resY, sourceFile, targetFile) :
|
||||
bg.fill(FOREGROUND_FILL)
|
||||
|
||||
# desature for certain bgcolors
|
||||
colorHSV = color.to_hsv()
|
||||
if colorHSV[1] > 50 and colorHSV[2] > 50:
|
||||
color_hsv = color.to_hsv()
|
||||
if color_hsv[1] > 50 and color_hsv[2] > 50:
|
||||
merged = img.merge_visible_layers(FALSE)
|
||||
|
||||
img.add_layer(pdb.gimp_layer_copy(merged, FALSE))
|
||||
@ -77,24 +77,82 @@ def python_fu_make_wallpaper(resX, resY, sourceFile, targetFile) :
|
||||
final = img.merge_visible_layers(FALSE)
|
||||
|
||||
# save
|
||||
pdb.gimp_file_save(img, final, targetFile, targetFile)
|
||||
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",
|
||||
"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/Make Wallpaper...",
|
||||
"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_SPINNER, "resX", "Screen width", 2560, (1, 9999, 1)),
|
||||
(PF_SPINNER, "resY", "Screen height", 1440, (1, 9999, 1)),
|
||||
(PF_FILE, "sourceFile", "Source file", "/home/sarah/sync/private/pictures/bandori/toxxtu77dsm41.jpg"),
|
||||
(PF_STRING, "targetFileName", "Desired target path", "/home/sarah/sync/private/pictures_converted")
|
||||
[
|
||||
(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),
|
||||
],
|
||||
[],
|
||||
python_fu_make_wallpaper)
|
||||
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...",
|
||||
"",
|
||||
[
|
||||
(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()
|
Loading…
Reference in New Issue
Block a user