Post Reply 

Batchscript won't work.

Jun 24, 2014, 19:15
Post: #1
Batchscript won't work.
OK, I following script in Python under Windows8, but it throws me an error, I can't run it.


The script is as follows:

from os import environ, listdir
import os.path

from gimpfu import pdb


MAX_WIDTH = 1920
MAX_HEIGHT = 1280


def get_images(target_dir):
'''
Возвращает список картинок для обработки.
@param target_dir: string
@return: list
'''
images = [filename for filename in listdir(target_dir) if filename.lower().endswith('jpg')]
images.sort()
return images


def get_image_new_dimensions(image):
'''
Возвращает новые размеры картинки с сохранением пропорций.
@param image: object
@return: int, int
'''
width = pdb.gimp_image_width(image)
height = pdb.gimp_image_height(image)
if width <= MAX_WIDTH and height <= MAX_HEIGHT:
return width, height
if width > height:
return MAX_WIDTH, int(MAX_WIDTH * height / width)
return int(MAX_HEIGHT * width / height), MAX_HEIGHT


def convert_image(target_dir, output_dir, image_name):
'''
Обрабатывает картинку.
@param target_dir: string
@param output_dir: string
@param image_name: string
'''
output_path = os.path.join(output_dir, image_name)
if os.path.exists(output_path):
print '%s already exists, skipping'%image_name
return
image = pdb.file_jpeg_load(os.path.join(target_dir, image_name), image_name)
pdb.gimp_file_load(image)
layer = image.layers[0]
width, height = get_image_new_dimensions(image)
pdb.gimp_image_scale(image, width, height)
pdb.plug_in_c_astretch(image, layer)
pdb.plug_in_sharpen(image, layer, 50)
pdb.gimp_file_save(image, layer, output_path, image_name)
pdb.gimp_image_delete(image)


def convert_images(target_dir, output_dir, images):
'''
Обрабатывает картинки.
@param target_dir: string
@param output_dir: string
@param images: list
'''
count = len(images)
for i, image_name in enumerate(images):
print 'converting %s (%s/%s)'%(image_name, i + 1, count)
convert_image(target_dir, output_dir, image_name)


def check_dirs(target_dir, output_dir):
'''
Возвращает, корректно ли указаны директории.
@param target_dir: string
@param output_dir: string
@return: bool
'''
if target_dir is None:
print '!!! please specify the target directory via the environment variable $TARGET_DIR'
return False
if not os.path.exists(target_dir):
print '!!! looks like the target directory %s does not exist'%target_dir
return False
if not os.path.exists(output_dir):
print '!!! looks like the ouput directory %s does not exist'%output_dir
return False
return True


def run():
'''
Запускает обработку.
'''
target_dir = environ.get('TARGET_DIR')
output_dir = os.path.join(target_dir, 'converted')
if check_dirs(target_dir, output_dir):
print 'converting images from %s to %s'%(target_dir, output_dir)
images = get_images(target_dir)
convert_images(target_dir, output_dir, images)
pdb.gimp_quit(1)


run()

I try to run it using following cmd command
gimp -idf --batch-interpreter python-fu-eval -b "execfile('convert.py')"

but throws me batch command experience error. How to solve it?
Find all posts by this user
Quote this message in a reply
Jun 24, 2014, 19:56
Post: #2
RE: Batchscript won't work.
A batch I wrote used:

Code:
gimp-2.6 -idf --batch-interpreter python-fu-eval -b "import sys;sys.path=['.']+sys.path;import batch;batch.run('./images')" -b "pdb.gimp_quit(1)"

The difficulty is that the file isn't in the python path...

Batch.py:
Code:
#!/usr/bin/python
# -*- coding: iso-8859-15 -*-

import os,glob,sys,time
from gimpfu import *


def process(infile):
    print "Processing file %s " % infile
    image = pdb.file_jpeg_load(infile,infile)
    drawable = pdb.gimp_image_get_active_layer(image)

    print "File %s loaded OK" % infile
    pdb.plug_in_photocopy(image, drawable,8.,0.8,0.2,0.2)
    pdb.plug_in_cartoon(image, drawable, 7.,0.2)
    outfile=os.path.join('processed',os.path.basename(infile))
    outfile=os.path.join(os.path.dirname(infile),outfile)
    print "Saving to %s" % outfile
    pdb.file_jpeg_save(image, drawable, outfile, outfile, "0.5",0,1,0,"",0,1,0,0)
    print "Saved to %s" % outfile
    pdb.gimp_image_delete(image)


def run(directory):
    start=time.time()
    print "Running on directory \"%s\"" % directory
#    os.mkdir(os.path.join(directory,'processed'))
    for infile in glob.glob(os.path.join(directory, '*.jpg')):
        process(infile)
    end=time.time()
    print "Finished, total processing time: %.2f seconds" % (end-start)


if __name__ == "__main__":
    print "Running as __main__ with args: %s" % sys.argv

Additionally have a look there: http://gimpforums.com/thread-debugging-p...fu-scripts

Now answering your questions on
https://Gimp-Forum.net
Visit this user's website Find all posts by this user
Quote this message in a reply
Jun 24, 2014, 20:16
Post: #3
RE: Batchscript won't work.
(Jun 24, 2014 19:56)ofnuts Wrote:  A batch I wrote used:

Code:
gimp-2.6 -idf --batch-interpreter python-fu-eval -b "import sys;sys.path=['.']+sys.path;import batch;batch.run('./images')" -b "pdb.gimp_quit(1)"

The difficulty is that the file isn't in the python path...

Batch.py:
Code:
#!/usr/bin/python
# -*- coding: iso-8859-15 -*-

import os,glob,sys,time
from gimpfu import *


def process(infile):
    print "Processing file %s " % infile
    image = pdb.file_jpeg_load(infile,infile)
    drawable = pdb.gimp_image_get_active_layer(image)

    print "File %s loaded OK" % infile
    pdb.plug_in_photocopy(image, drawable,8.,0.8,0.2,0.2)
    pdb.plug_in_cartoon(image, drawable, 7.,0.2)
    outfile=os.path.join('processed',os.path.basename(infile))
    outfile=os.path.join(os.path.dirname(infile),outfile)
    print "Saving to %s" % outfile
    pdb.file_jpeg_save(image, drawable, outfile, outfile, "0.5",0,1,0,"",0,1,0,0)
    print "Saved to %s" % outfile
    pdb.gimp_image_delete(image)


def run(directory):
    start=time.time()
    print "Running on directory \"%s\"" % directory
#    os.mkdir(os.path.join(directory,'processed'))
    for infile in glob.glob(os.path.join(directory, '*.jpg')):
        process(infile)
    end=time.time()
    print "Finished, total processing time: %.2f seconds" % (end-start)


if __name__ == "__main__":
    print "Running as __main__ with args: %s" % sys.argv

Additionally have a look there: http://gimpforums.com/thread-debugging-p...fu-scripts

OK, I have run your code too, and it stills throws me an error. I have made folder images and put 2 jpgs files inthere but nothing happens. Sad
Find all posts by this user
Quote this message in a reply
Jun 24, 2014, 22:16
Post: #4
RE: Batchscript won't work.
What error exactly?

Now answering your questions on
https://Gimp-Forum.net
Visit this user's website Find all posts by this user
Quote this message in a reply
Jun 24, 2014, 22:47
Post: #5
RE: Batchscript won't work.
#!/usr/bin/python
# -*- coding: iso-8859-15 -*-

import os,glob,sys,time
from gimpfu import *


def process(infile):
print "Processing file %s " % infile
image = pdb.file_jpeg_load(infile,infile)
drawable = pdb.gimp_image_get_active_layer(image)

print "File %s loaded OK" % infile
pdb.plug_in_photocopy(image, drawable,8.,0.8,0.2,0.2)
pdb.plug_in_cartoon(image, drawable, 444.,0.2)
pdb.gimp_levels_stretch(drawable)
drawableOne=pdb.gimp-layer-copy(drawable,TRUE)
drawableTwo=pdb.gimp-layer-copy(drawable,TRUE)
pdb.gimp-image-unset-active-channel(drawable)
pdb.gimp-desaturate-full(drawable,2)
drawableThree=pdb.gimp-layer-copy(drawableOne, true)
drawableFour=pdb.gimp-layer-copy(drawableOne, true)
drawableFive=pdb.gimp-layer-copy(drawableOne, true)
pdb.gimp-layer-set-mode(drawableThree,16)
pdb.gimp-invert(drawableThree)
pdb.gimp-image-merge-down(image, drawableThree,2)
pdb.script-fu-distress-selection(0,image, drawableThree,240,1,1,1,1)
pdb.script-fu-distress-selection(0,image, drawableTwo,240,1,1,1,1)
pdb.gimp-layer-set-mode(drawableTwo,16)
pdb.script-fu-distress-selection(0,image, drawableTwo,15,1,1,1,1)
pdb.gimp-layer-set-mode(drawableOne,16)
pdb.gimp-layer-set-mode(drawableOne,1)
pdb.gimp-image-merge-visible-layers(image,2)
pdb.gimp-image-set-active-channel(drawable)
pdb.gimp-layer-set-mode(drawable,14)
pdb.gimp-image-flatten(image)
outfile=os.path.join('processed',os.path.basename(infile))
outfile=os.path.join(os.path.dirname(infile),outfile)
print "Saving to %s" % outfile
pdb.file_jpeg_save(image, drawable, outfile, outfile, "0.5",0,1,0,"",0,1,0,0)
print "Saved to %s" % outfile
pdb.gimp_image_delete(image)


def run(directory):
start=time.time()
print "Running on directory \"%s\"" % directory
# os.mkdir(os.path.join(directory,'processed'))
for infile in glob.glob(os.path.join(directory, '*.jpg')):
process(infile)
end=time.time()
print "Finished, total processing time: %.2f seconds" % (end-start)


if __name__ == "__main__":
print "Running as __main__ with args: %s" % sys.argv


THANKS, Man! OK I have finally made it, however for my code it still throws an error. Can you help me with it!
Find all posts by this user
Quote this message in a reply
Jun 24, 2014, 23:01
Post: #6
RE: Batchscript won't work.
Yes if you tell me the error...

Now answering your questions on
https://Gimp-Forum.net
Visit this user's website Find all posts by this user
Quote this message in a reply
Jun 24, 2014, 23:15
Post: #7
RE: Batchscript won't work.
(Jun 24, 2014 23:01)ofnuts Wrote:  Yes if you tell me the error...

It throws an error to gimp_desaturate_full is not defined.
Find all posts by this user
Quote this message in a reply
Jun 24, 2014, 23:22
Post: #8
RE: Batchscript won't work.
OK, I am using
pdb.gimp_desaturate_full(drawable, gimpfu.DESATURATE_AVERAGE)

now and it works. One thing now concerns me is that how can I copy layers? What function to call?
Find all posts by this user
Quote this message in a reply
Jun 24, 2014, 23:43
Post: #9
RE: Batchscript won't work.
Something I didn't notice....Your code has dashes where it should have underscores, In Python this is pdb.gimp_desaturate_full. This is also true for your other PDB calls.

If you use the python-fu console to try things, you can use the "Browse" button to search functions, and when you click the "Apply" button it inserts a prototype call in the python-fu console input.

"true" is also undefined. You should use "True", but "TRUE" also works because it's defined with a value of "True".

Better use the named constants instead of integers (DESATURATE_AVERAGE instead of 2)(for these the dashes in the names should also be replaced by underscores).

Now answering your questions on
https://Gimp-Forum.net
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Help - can't get Mathmap and GMIC to work together paul2332 1 183 Jul 15, 2017 18:58
Last Post: ythgilb
  Cant get Heal Selection to work ianp5a 1 269 Oct 9, 2014 20:04
Last Post: rich2005
  plug_in_dog won't work covjek123 2 374 Jun 28, 2014 21:47
Last Post: covjek123
Exclamation GMIC doesnpt work as previews EscapedNight 2 693 Mar 22, 2013 13:28
Last Post: EscapedNight
  many G'MIC effects don't work, many more come out different than preview moltres_rider 2 787 Dec 2, 2012 17:32
Last Post: moltres_rider

Forum Jump:


GIMP ForumPortalArchiveContactTermsRSS