Can anyone point me in the right direction for batch handling an opacity
change? What I want to achieve is the point the GIMP at a folder and have
it reduce all images to 75% opacity.
I'm afraid I'm new to both the GIMP and Scriptfu, so its a little confusing.
Any help would be really appreciated. (I'm on 2.2.13 on Windows)
Best regards
John
PS - I've done quite a lot of searching on this already and come across some
good advice at
http://groups.google.co.uk/group/comp.graphics.apps.gimp/browse_thread/thread/bb627e1279c05f53/071e3edf342089b4?lnk=gst&q=batch&rnum=2#071e3edf342089b4
but still haven't found an answer.
https://lists.xcf.berkeley.edu/lists/gimp-user/2002-April/004509.html
cheers
:) It took some hunting, but here it is:
convert in-image.png -channel Alpha -fx 'u*0.75' out-image.png
Will make a file that is a copy of in-image.png and write it to
out-image.png with the Alpha channel at 75% of the original image. This
still works even if the original image doesn't have an alpha channel.
Note that the final image has to support Alpha. Additionally the mogrify
command doesn't seem to support the -fx command line option. *shrugs*
So under Linux, a bash prompt session would look like this:
for i in *; do convert $i -channel Alpha -fx 'u*0.75'
$i-transparent.png; done
This will create a series of transparent copies of all the images in a
directory. However, this will be messy because they'll be mixed in with
the original images, and will have suffixes such as .png.png, .jpg.png.
A more advanced solution would be as follows
--------------------copy below into a script file ----------------
#!/bin/bash
## This script takes three arguments, the directory of the source
## images, the directory for the result images, and the value to
## multiply the alpha channel by.
## E.g. assuming the script file is named transparent.sh
## transparent.sh ~/Media ~/Media/Transparent 0.75
## If the result image directory is given as a relative filepath, it
## will be created within the source image directory.
##
## Further improvements that could be made are:
## - To put in a -h or --help option
## - To allow recursive searching of a directory.
## The second could be achieved by replacing the second for file
## statement with
## for file in `find $1 -iname *.$suffix`
## though this would make the logic of the convert line more complex
## if the relative structure of the input directory was to be
## reproduced in the output directory.
## Additionally, this script doesn't do any sanity checking to ensure
## that an input file can't be overwritten by an output file.
##
## Copyright GPL v2, 2006, Joal Heagney
##
pushd $1
mkdir -p $2
for suffix in jpg jpeg JPG JPEG gif GIF tif tiff TIF TIFF png PNG
do
for file in *.$suffix
do
convert $file -channel Alpha -fx 'u*$3' $2/`basename $file
$suffix`.png
## Note that the convert and $suffix`.png should be all one line
done
done
popd
--------------------end of script ---------------------------------
Joal Heagney
Thanks very much for your help. I'm not really familiar with ImageMagick.
Do I need something on top of Mogrify or is that just a standalone download?
Anyway, I'll study your script below and no doubt come back with a few more
questions in another post.
Thanks both for your time
Best regards
John
"Joal Heagney" <jhe1...@bigpond.net.au> wrote in message
news:hA1Ug.39885$rP1....@news-server.bigpond.net.au...
You're on MS-Windows? I'm not entirely sure how ImageMagick works on
anything other than Linux. On Linux, ImageMagick is a whole suite of
software applications, convert, mogrify, etc. If Mogrify is the only
application you've downloaded, then it won't work with this script. As
stated in my previous post, for some reason mogrify (or at least the
linux version) doesn't recognise the -fx option.
---the command line---
The ; bit just tells the command line to split the command into a series
of different lines. In actual fact, what my command line is saying is:
for i in *
do convert -$ -channel Alpha -fx 'u*0.75' $i-transparent.png
done
Breaking it down for you:
for i in *
do
This is a bash shell "for loop". Bash is kinda like the MS-DOS prompt.
The "for i in *" will loop through each file in the current directory
and set the 'i' variable to that value. That's why this command line and
the script WON'T recursively search down through directories for images.
It only works on the images in the current directory. Additionally it
will also try to treat any subdirectories as image files. A dreadful
bug, but I wanted to give a simple example first.
You may or may not be able to do something similar under microsoft.
convert $i -channel Alpha -fx 'u*0.75' $i-transparent.png
This is the imagemagick stuff. This you will be able to do under
microsoft, though microsoft may require you to change the - option flags
into something else. E.g. passing options to the microsoft dir command
requires slashes rather than dashes.
The -channel Alpha bit means that the next option will only work
on the Alpha (transparency) channel.
The -fx 'u*0.75' takes the first image/layer (u) in the file
and multiplies it by 0.75. (Limited to the Alpha channel though.) Then
it saves it to a file with the same name as the input file, but with
-transparent.png tagged to the end. The .png bit will make sure that the
output is in a form that supports alpha transparency
done
This bit ends the bash "for loop".
----the script----
For the bash script I gave you, the #!/bin/bash bit at the start tells
linux command prompts what script interpreter to use, in this case the
bash shell. On linux, a #!/bin/python line would tell the command prompt
to use the python script interpreter. #!/bin/perl would be for perl scripts.
The rest of the # lines are comments and are completely ignored by the
interpreter. The ## bit is just a convention to make comments stick out.
One # would work just as well.
$1, $2, $3 etc. are variables automatically set to the arguments of the
script. As noted in the ## comments above, the first argument $1 is
meant to be the source directory, the second argument $2 the output
directory and the third argument $3 the multiplier for the Alpha channel.
pushd is a linux command that changes you to a new directory, (push
directory) but REMEMBERS what directory you were in when you issued the
command.
popd returns you to that original directory (pop directory). You can
issue a series of pushd commands that will bounce you all over the
place, and popd will take you back through them in correct sequence.
mkdir -p will make a new directory, including any parent directories
required. This bit is to make sure that the output directory actually
exists.
E.g. mkdir -p Music/Jazz will make a Music directory, and then a Jazz
directory in Music. If the directories already exist, then mkdir -p does
nothing.
basename takes an argument and strips off all directory prefixes. The
command also optionally strips of a suffix, e.g. .gif or .jpg. Enclosing
the basename $file $suffix in `` (slanted quotes) causes the bash
interpreter to run the command, and then plug the results into the
commandline.
So putting $2/`basename $file $suffix`.png at the end of the convert
line will take the name of the file, strip off the leading directories
and the trailing suffix, place that file under directory $2 (the output
directory) and give the file a .png ending. This also tells convert to
convert the file to png format.
pushd, popd, mkdir, and basename are all linux commands. You will need
to find microsoft replacements for these commands.
Hope that helps?
Joal Heagney
>John wrote:
>> Hi Joal and Stu,
>>
>> Thanks very much for your help. I'm not really familiar with ImageMagick.
>> Do I need something on top of Mogrify or is that just a standalone download?
>>
>> Anyway, I'll study your script below and no doubt come back with a few more
>> questions in another post.
>>
>> Thanks both for your time
>>
>> Best regards
>>
>> John
>
>You're on MS-Windows? I'm not entirely sure how ImageMagick works on
>anything other than Linux. On Linux, ImageMagick is a whole suite of
>software applications, convert, mogrify, etc.
ImageMagick works in exactly the same way on Windows and there is an
installation binary that works as Windows users expect including
putting the viewer and documentation in the start menu and the
executables on the Windows PATH.
Microsoft don't offer bash but I think that your script will work with
the Korn Shell which is included in Microsoft's SFU. SFU is a free
download from
<http://www.microsoft.com/technet/interopmigration/unix/sfu/default.mspx>.
Good to know. The only problem I can see is that mkdir and basename are
linux apps. (pushd and popd are part of the shell.) Are there Microsoft
alternatives?
Joal Heagney
> Good to know. The only problem I can see is that mkdir and basename are
> linux apps. (pushd and popd are part of the shell.) Are there Microsoft
> alternatives?
>
> Joal Heagney
Well what-ya-know. The site you mentioned also has alternatives to mkdir
and basename. Hmmm.
"We are the Unix, resistance is futile."
:)
Joal Heagney