News:

Tekforums.net - The improved home of Tekforums! :D

Main Menu

Batch optimising images from command line/context menu

Started by Clock'd 0Ne, July 04, 2011, 10:52:31 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Clock'd 0Ne

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 :)

Leon

For basic resizing I just use the XP Powertool (redone for Windows 7) but it may be too basic for your needs :)
.::. www.leonslost.co.uk .::. Media Server Guide .::.

PC: i5 760 .::.  GA-P55-UD3 .::. 8GB Corsair 'Dominator' DDR3 .::. 1GB EVGA GTX 460 SC .::. Win7 Ultimate  .::. Dell 24" Ultra Sharp
Netbook: HP Mini 311c-1101sa .::. 3GB Ram .::. ION Hack .::. Win7 Ultimate
Server: HP MicroServer .::. 3GB Ram .::. 4x 2TB Storage .::. 512MB nVidia 210 .::. Win7 Ultimate, XBMC 11 (Aeon NOX), Sick Beard & Couch Potato
Phone: SE Xperia Mini Pro .::. MiniCMSandwich Lite (Android ICS Custom) .::. OC @ 1.6Ghz
Tablet: Asus Transformer TF101 w/ Dock .::. EOS JB Nightlies (Android JB Custom) .::. OC @ 1.6Ghz

Clock'd 0Ne

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.

Mongoose

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

XEntity

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..

Clock'd 0Ne

#5
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:

XEntity

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

Clock'd 0Ne

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

XEntity

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

Clock'd 0Ne


XEntity

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)

Clock'd 0Ne

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

XEntity

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

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...

XEntity

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

Clock'd 0Ne

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?