Last active
June 12, 2018 07:57
-
-
Save josuigoa/53b85c325aede63af36b104d5cad1736 to your computer and use it in GitHub Desktop.
Gimp plug-in to scale images recursively from a folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
from gimpfu import * | |
import glob | |
from os import listdir | |
from os.path import isfile, isdir, join, splitext | |
import os | |
import sys | |
import time | |
import datetime | |
# print-en irteera fitxategi honetara berbideratuko da, lehenagoko testuari gehituz ('a' -> append) | |
sys.stdout = open( '/home/josu/gimpstdout.txt', 'a') | |
# sys.stdout = open( 'd:\\neria\\gimpstdout.txt', 'a') | |
# sys.stderr = open( 'd:\\neria\\gimpstderr.txt', 'a') | |
def run(*args): | |
ts = time.time() | |
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S') # 2012-12-15 01:21:05 | |
dir_name, extension_ind, scale_factor = args | |
extension = ('.xcf', '.psd', '.jpg', '.png')[extension_ind] | |
print "*** BEGIN *** scale_images" | |
print 'args:', dir_name, ',', extension | |
print 'date:', st | |
out_dir = dir_name + '_scaled' | |
if not os.path.exists(out_dir): | |
os.mkdir(out_dir) | |
scale_images(dir_name, scale_factor, out_dir, extension) | |
def scale_images(dir, scale_factor, out_dir, extension): | |
for file in listdir(dir): | |
path = join(dir, file) | |
filename, file_extension = os.path.splitext(file) | |
if (isdir(path)): | |
out_dir_path = join(out_dir, file) | |
if not os.path.exists(out_dir_path): | |
os.mkdir(out_dir_path) | |
scale_images(path, scale_factor, out_dir_path, extension) | |
elif file_extension == extension: | |
new_filename = out_dir + os.path.sep + filename + '.jpg' | |
print 'saving: ', new_filename | |
image = pdb.file_jpeg_load(path, path) | |
pdb.gimp_image_scale(image, image.width*scale_factor, image.height*scale_factor) | |
merged_layer = pdb.gimp_image_merge_visible_layers(image, 2) | |
pdb.file_jpeg_save(image, merged_layer, new_filename, "raw_filename", 1, 0.5, 0, 0, image.filename + " erdira eskalatua", 0, 0, 0, 0) | |
register( | |
"scale_images", | |
"Irudiak eskalatzeko", | |
"Irudiak eskalatzeko", | |
"Josu Igoa", | |
"Josu Igoa", | |
"2016", | |
"<Toolbox>/Pluginak/Scale images", "", | |
[ | |
(PF_DIRNAME, "directory", "Directory to find files with the given extension and export them", ""), | |
(PF_OPTION, "file_extension", "Extension of the files which will be exported", 0, ('xcf', 'psd', 'jpg', 'png')), | |
(PF_FLOAT, "scale_factor", "The scale factor", "0.5"), | |
], | |
[], | |
run | |
) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment