Tekforums

Chat => Entertainment & Technology => Topic started by: Clock'd 0Ne on July 04, 2011, 10:52:31 AM

Title: Batch optimising images from command line/context menu
Post by: Clock'd 0Ne on July 04, 2011, 10:52:31 AM
I've started using two tools to optimise/losslessly compress images down as small as possible for the web using two command line .exe tools, pngout and jpegtran

Both of these work on the same basic command: pngout inputfile outputfile and jpegtran inputfile outputfile

Is there some way of coding a batch file that will recurse a directory and find all applicable images (*.png or *.jpg respectively), then batch run this command on them?

Even better would be having a method of integrating this into the Explorer shell context menus, so I could select a group of images, right click and choose Optimise with pngout or Optimise with jpegtran

I'm sure there's some smartie here with the answer :)
Title: Re: Batch optimising images from command line/context menu
Post by: Leon on July 04, 2011, 12:40:30 PM
For basic resizing I just use the XP Powertool (redone for Windows 7 (http://imageresizer.codeplex.com/)) but it may be too basic for your needs :)
Title: Re: Batch optimising images from command line/context menu
Post by: Clock'd 0Ne on July 04, 2011, 14:09:31 PM
No, these are a lot more advanced than that. It's not about resizing, its taking images and computing them down to smaller filesizes without any degredation (the PNG one is basically using GZIP style compression algorithms within the PNG files themselves). With the PNGs especially, I managed to trim 5Mb off a folder of PNGs for one website doing this (which is a massive saving in web page loading terms), but it's time consuming to keep running the command by hand changing the filename each time.
Title: Re: Batch optimising images from command line/context menu
Post by: Mongoose on July 04, 2011, 18:21:09 PM
can't give you an exact command, but I use imagemagick for all my command line image adjustment needs, it can do most things and is very happy with wildcards
Title: Re: Batch optimising images from command line/context menu
Post by: XEntity on July 04, 2011, 19:18:40 PM
If you want to rename JPGs to PNGs for example use this:

copy the batch and the exe in to the folder you want to do, or put full file paths..


mkdir compressed
for %%I in (*.jpg) do pngout %%I compressed/%%I
pause


If you want to use any additional png out switches just add them on the end as usual..

Edit.. Just changed to make a sub directory and copy there..
Title: Re: Batch optimising images from command line/context menu
Post by: Clock'd 0Ne on July 04, 2011, 19:22:33 PM
So this would run the program across all PNGs in the 'old' subfolder and output to the 'new' subfolder?

for %%I in (*.png) do pngout old\%%I new\%%I

e.g: pngout old\filename.png new\filename.png


Edit: Ahh excellent, I'll give it a try  :ptu:
Title: Re: Batch optimising images from command line/context menu
Post by: XEntity on July 04, 2011, 19:23:57 PM
Quote from: Clock'd 0Ne on July 04, 2011, 19:22:33 PM
So this would run the program across all PNGs in the 'old' subfolder and output to the 'new' subfolder?

for %%I in (*.png) do pngout old\%%I new\%%I

e.g: pngout old\filename.png new\filename.png

Yep, but slashes might be the wrong way round... I updated mine as you posed to create a sub dir..

edit... also I'm sure you could get away with *.* to pick anything from the folder and convert to PNG
Title: Re: Batch optimising images from command line/context menu
Post by: Clock'd 0Ne on July 04, 2011, 19:29:15 PM
I doubt I'll want to do any conversions, but this will definitely come in handy now I know how to use and modify it  :D
Title: Re: Batch optimising images from command line/context menu
Post by: XEntity on July 04, 2011, 20:00:30 PM
Here's the full awesome-o full code version...


echo off
cls

echo. Awesome-O PNG Converter
echo. ~~~~~~~~~~~~~~~~~~~~~~~~~
echo.

echo. Checking "OLD" folder exists
echo. ##################################
echo.

IF EXIST %CD%/OLD/ GOTO FINDNEW
echo.
echo. Not found! You need to have a subfolder called "OLD" to convert dumbass!!
echo.
echo.

goto END

:FINDNEW

echo. That's a good start...
echo.
echo.
echo. Checking "NEW" folder exists
echo. ##################################
echo.

IF EXIST %CD%/NEW/ GOTO FOUND

MKDIR NEW

echo. Folder not found so I created one!
echo. ##################################
echo.

goto COMPRESS

:FOUND
echo. We're on a roll!
echo.
echo.

:COMPRESS

echo. Getting busy making some PNGs!
echo. ##################################
echo.

for %%I in (old/*.*) do pngout old/%%I new/%%I

echo.
echo. All done sir!
echo.
echo.

:END

pause
Title: Re: Batch optimising images from command line/context menu
Post by: Clock'd 0Ne on July 04, 2011, 20:27:40 PM
That's ruddy marvellous :bow: you beauty Wub
Title: Re: Batch optimising images from command line/context menu
Post by: XEntity on July 04, 2011, 20:58:58 PM
Quote from: Clock'd 0Ne on July 04, 2011, 20:27:40 PM
That's ruddy marvellous :bow: you beauty Wub

No worries, and you could always get it to create the OLD folder automatically at some point after line 12, before or after prompt, so you then wouldn't have to go and create an "OLD" folder, and will be ready and waiting for you to drop in some images..

drop in..
"mkdir OLD"

And amend insulting text to something else (insult optional)
Title: Re: Batch optimising images from command line/context menu
Post by: Clock'd 0Ne on July 06, 2011, 11:05:17 AM
I had to modify this to include a cd\mydirectory at the start then it worked a treat, as the batch file path defaults to C:\windows\system

Also, I did some googling and eventually found to read from a directory such as the current users desktop and came up with this using a variable substitute:

for /R %USERPROFILE%\DESKTOP\OLD\ %%I in (*.png) do pngout %%I %USERPROFILE%\DESKTOP\NEW\%%~nI

http://www.battoexe.com/commands/for.html (http://www.battoexe.com/commands/for.html)
Title: Re: Batch optimising images from command line/context menu
Post by: XEntity on July 06, 2011, 19:30:28 PM
Quote from: Clock'd 0Ne on July 06, 2011, 11:05:17 AM
I had to modify this to include a cd\mydirectory at the start then it worked a treat, as the batch file path defaults to C:\windows\system

Also, I did some googling and eventually found to read from a directory such as the current users desktop and came up with this using a variable substitute:

for /R %USERPROFILE%\DESKTOP\OLD\ %%I in (*.png) do pngout %%I %USERPROFILE%\DESKTOP\NEW\%%~nI

http://www.battoexe.com/commands/for.html (http://www.battoexe.com/commands/for.html)

Out of interest what OS are you using? Mine defaulted to the current directory?

But anyway, just copy this line in to the start of the file after 'echo off' on a new line..

cd %~dp0

That will change the current working directory to that of the batch file.. then can just use sub folders...
Title: Re: Batch optimising images from command line/context menu
Post by: XEntity on July 06, 2011, 19:33:30 PM
Updated code for reference using the current working directory logic..

echo off
cd %~dp0
cls

echo. Awesome-O PNG Converter
echo. ~~~~~~~~~~~~~~~~~~~~~~~~~
echo.

echo. Checking "OLD" folder exists
echo. ##################################
echo.

IF EXIST %CD%/OLD/ GOTO FINDNEW
echo.
echo. Not found! You need to have a subfolder called "OLD" to convert dumbass!!
echo.
echo.

goto END

:FINDNEW

echo. That's a good start...
echo.
echo.
echo. Checking "NEW" folder exists
echo. ##################################
echo.

IF EXIST %CD%/NEW/ GOTO FOUND

MKDIR NEW

echo. Folder not found so I created one!
echo. ##################################
echo.

goto COMPRESS

:FOUND
echo. We're on a roll!
echo.
echo.

:COMPRESS

echo. Getting busy making some PNGs!
echo. ##################################
echo.

for %%I in (old/*.*) do pngout old/%%I new/%%I

echo.
echo. All done sir!
echo.
echo.

:END

pause
Title: Re: Batch optimising images from command line/context menu
Post by: Clock'd 0Ne on July 06, 2011, 19:46:39 PM
I'm using Windows 7 Home Premium, but I was wrong, it was actually C:\Users\Nige on opening. Looking under SET it seems thats recorded as HOMEPATH as well as USEPROFILE so perhaps it opens under one of those?
Title: Re: Batch optimising images from command line/context menu
Post by: XEntity on July 06, 2011, 20:02:22 PM
Hmm... maybe it's an admin rights thing as I'm on W7HP but logged in as administrator?

Does adding my command line work?

I've also noticed however that it doesn't like spaces in file names or the folder that it is sat within?
Title: Re: Batch optimising images from command line/context menu
Post by: Clock'd 0Ne on July 06, 2011, 20:13:32 PM
I always do directory/filenames in 802.3 format, otherwise you have to remember to enclose them in speech marks, e.g. "C:\Program Files\" or C:\Progra~1\

Having said that, it works fine for me once I've cd\ into the correct directory, which does have spaces in, and I've not done any of that... and I've just worked out why!
I wasn't in the system directory as I thought, it was just that the directory had a space in, so when I did the cd\ it was reading the directory with 802.3 format how I typed it, not with spaces.

Without changing directories:
IF EXIST "%CD%\OLD\" GOTO FINDNEW works
IF EXIST %CD%\OLD\ GOTO FINDNEW doesn't :)

Also, backslashes, not forward slashes, or it gets confused
Title: Re: Batch optimising images from command line/context menu
Post by: XEntity on July 06, 2011, 20:22:26 PM
Also don't forget to make a similar change on the png script as well otherwise it doesn't like spaces in the PNGs..

I've also used %~dp0 as apparently that plays a little nicer, although might only be UNC.. which wont work as the current director can't be set as a UNC path, unless you put in a check to then map a network drive!  :worried:

Here's the full updated code anyway that will take spaces etc...

echo off
cd %~dp0
cls

echo. Awesome-O PNG Converter
echo. ~~~~~~~~~~~~~~~~~~~~~~~~~
echo.

echo. Checking "OLD" folder exists
echo. ##################################
echo.
IF EXIST "%~dp0OLD/" GOTO FINDNEW
echo.
echo. Not found! You need to have a subfolder called "OLD" to convert dumbass!!
echo.
echo.

goto END

:FINDNEW

echo. That's a good start...
echo.
echo.
echo. Checking "NEW" folder exists
echo. ##################################
echo.

IF EXIST "%~dp0NEW/" GOTO FOUND

MKDIR NEW

echo. Folder not found so I created one!
echo. ##################################
echo.

goto COMPRESS

:FOUND
echo. We're on a roll!
echo.
echo.

:COMPRESS

echo. Getting busy making some PNGs!
echo. ##################################
echo.

for %%I in (old/*.*) do pngout "%~dp0OLD/%%I" "%~dp0NEW/%%I"

echo.
echo. All done sir!
echo.
echo.

:END

pause