Need some assistance getting gifs to watermark in batch

ImageMagickObject is a Windows COM+ interface to ImageMagick. COM+, Visual Basic, and Delphi users should post to this discussion group.
Post Reply
RichVin
Posts: 6
Joined: 2014-06-13T02:31:31-07:00
Authentication code: 6789

Need some assistance getting gifs to watermark in batch

Post by RichVin »

Hi,

I'm not sure if i'm posting in the right section but i'm using windows 8.

I have been using this command to watermark gif animations and it works good, however i would want to somewhat automate the process.

Code: Select all

C:\Program Files (x86)\ImageMagick>convert input.gif -coalesce -gravity SouthWest -geometry +0+0 null: watermark.png -layers composite -layers optimize output.gif
Is there any easy way to modify the string to take *.gif from a folder and then run the watermark process and output it with the same filename as original source in a different folder i specify?

Would appreciate any help!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Need some assistance getting gifs to watermark in batch

Post by snibgo »

I would put it in a "for" loop, eg

Code: Select all

set INDIR=\some\where
set OUTDIR=\some\other\place

for /F "usebackq" %%F in (`dir /b %INDIR%\*.gif`) do (
  convert ^
    %%F ^
    -coalesce ^
    -gravity SouthWest -geometry +0+0 null: watermark.png ^
    -layers composite -layers optimize ^
    %OUTDIR%\%%~nxF
)
snibgo's IM pages: im.snibgo.com
RichVin
Posts: 6
Joined: 2014-06-13T02:31:31-07:00
Authentication code: 6789

Re: Need some assistance getting gifs to watermark in batch

Post by RichVin »

thanks for such a quick response!

i used your script and it did do work somewhat but i ran into issues when trying more than one image

see below what happened when script is being run

Code: Select all

C:\Program Files (x86)\ImageMagick>test.bat
C:\Program Files (x86)\ImageMagick>set INDIR=D:\test
C:\Program Files (x86)\ImageMagick>set OUTDIR=D:\complete
C:\Program Files (x86)\ImageMagick>for /F "usebackq" %F in (`dir /b D:\test\*.gif`) do (convert     %F     -coalesce     -gravity SouthWest -geometry +0+0 null: watermark.png     -layers composite -layers optimize     D:\complete\%~nxF )
C:\Program Files (x86)\ImageMagick>(convert     555.gif     -coalesce     -gravity SouthWest -geometry +0+0 null: watermark.png     -layers composite -layers optimize     D:\complete\555.gif )
C:\Program Files (x86)\ImageMagick>(convert     fdsfsd.gif     -coalesce     -gravity SouthWest -geometry +0+0 null: watermark.png     -layers composite -layers optimize     D:\complete\fdsfsd.gif ) convert.exe: unable to open image `fdsfsd.gif': No such file or directory @ error/blob.c/OpenBlob/2658. convert.exe: Missing Null Image List Separator layers Composite @ error/mogrify. c/MogrifyImageList/8234.
I can see it says "unable to open image" but the image is there, it works if i remove the image called 555.gif and only have fdsfsd.gif in the folder, any ideas?

EDIT: On further testing watermarking test.gif and fdsfsd.gif works in batch, i have no idea why... but when i added more gifs to the indir folder they give same error, i've checked the file permissions and they all seem ok, could it be the windows UAC that somehow is messing up? Perhaps i should try on a windows 7 machine or do you have any ideas what could be causing this?

Thanks very much for your assistance!
RichVin
Posts: 6
Joined: 2014-06-13T02:31:31-07:00
Authentication code: 6789

Re: Need some assistance getting gifs to watermark in batch

Post by RichVin »

After further testing it seems that it only works on the first image in the loop, the other images get "path not found", maybe cause the loop adds `` marks to the filename or something?

The funny thing is it seems windows is caching the bat script cause if i run the script with image 555.gif separately and it succeeds, then i remove it and run it with fdsfsd.gif and it succeeds these 2 images will continue to successfully convert even tho i keep adding more pictures, like it's running from memory.

I tried on a windows 7 computer now with all images in the input folder from scratch and it only converts the first image in the loop, all the rest get path not found so i suspect there's a quotation mark somewhere placed wrong, i'm no expert as you can see, please help :)

Thanks!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Need some assistance getting gifs to watermark in batch

Post by snibgo »

I am on Windows 8.1.

Change the line

Code: Select all

    %%F ^
to

Code: Select all

    %INDIR%\%%F ^
snibgo's IM pages: im.snibgo.com
RichVin
Posts: 6
Joined: 2014-06-13T02:31:31-07:00
Authentication code: 6789

Re: Need some assistance getting gifs to watermark in batch

Post by RichVin »

you're a master of the craft sir, thank you very much, works perfectly.
RichVin
Posts: 6
Joined: 2014-06-13T02:31:31-07:00
Authentication code: 6789

Re: Need some assistance getting gifs to watermark in batch

Post by RichVin »

Hate to ask you and be greedy, but is there any easy way to get the script to preserve folder structure from input to output with files ending up relative to their location in the input folders?

No bigy if it's not possible, this has helped me a lot already! Thanks
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Need some assistance getting gifs to watermark in batch

Post by snibgo »

Sorry, I don't understand the question. What "folder structure"? My script reads the gifs in one directory, creating gifs in another. It doesn't operate on trees of directories (though that would be possible). Nor does it create any directories.

It should create exactly one output gif for every input gif, and should ignore all other files.
snibgo's IM pages: im.snibgo.com
RichVin
Posts: 6
Joined: 2014-06-13T02:31:31-07:00
Authentication code: 6789

Re: Need some assistance getting gifs to watermark in batch

Post by RichVin »

Yep sorry your script does exactly what i wanted (originally) but i got greedy. I would like it to mimic the folder structure from input to output as well as put the files into the same structure it had in input as output

Like i said, it's really not a big deal, you've been tremendous help already.

EDIT: Just realized script doesn't handle any files with spacers in them, any ideas on this?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Need some assistance getting gifs to watermark in batch

Post by snibgo »

If you want it to operate on subfolders, put "/s" is the "dir" command, and remove %INDIR% from the line "%INDIR%\%%F ^".

However, IM won't create subfolders (aka subdirectories) that don't already exist. You have to do that yourself, before converting the files. You could do this in a for loop, but "xcopy /t" should do the job.

You also need to juggle the output directory name, so it would look something like this. I have ignored drive letters; if you want them, you will need to adjust this. I've put quotes around filenames, and "tokens=*", to cope with embedded spaces.

Code: Select all

setlocal enabledelayedexpansion

xcopy %INDIR%\*.gif %OUTDIR% /t

for /F "tokens=* usebackq" %%F in (`dir /s /b %INDIR%\*.gif`) do (
  echo %%F

  set FULLOUTDIR=%%~pF
  set FULLOUTDIR=!FULLOUTDIR:%INDIR%=%OUTDIR%!

  echo FULLOUTDIR=!FULLOUTDIR!

  %IM%convert ^
    "%%F" ^
    -coalesce ^
    -gravity SouthWest -geometry +0+0 null: watermark.png ^
    -layers composite -layers optimize ^
    "!FULLOUTDIR!%%~nxF"
)
snibgo's IM pages: im.snibgo.com
Post Reply