Page 1 of 1

Batch convert while checking transparency

Posted: 2014-10-21T15:04:11-07:00
by malbertmalbert
Hi I'm trying to convert a bunch of png files to jpg from one folder. But some are transparent so I don't want to convert those.
I'm on Windows and using the command line. What I have found is this:

Code: Select all

$ convert -path (foldername) -format jpg *.png
Which will convert all of the png's to jpg's
I also found "%[opaque]" which should return 1 if it is transparent but I don't know how to combine these two statements. I basically want it to skip all png files that are transparent.
Any help would be appreciated.

Thanks,
Malbert

Re: Batch convert while checking transparency

Posted: 2014-10-21T15:57:25-07:00
by snibgo
malbertmalbert wrote:I also found "%[opaque]" which should return 1 if it is transparent
Where did you find that? It's wrong. "%[opaque]" returns the string "true" if the image is fully opaque; otherwise (if fully or partly transparent) it returns "false".

I would write a for-loop that loops through the png files. For each one, use "identify -format %[opaque]" to see if it is opaque. If it is, do the conversion.

Re: Batch convert while checking transparency

Posted: 2014-10-23T10:58:19-07:00
by malbertmalbert
Thanks you so far; I'm probably still having some syntax problems though if you could help. This is what I entered:

Code: Select all

FOR %i IN (*.png) DO IF [indentify -format %[opaque] %i] == [true] (convert -format %i .jpg)
and the error I get is -format was unexpected at this time.

Re: Batch convert while checking transparency

Posted: 2014-10-23T11:43:57-07:00
by snibgo
That is good pseudo-code, but isn't valid syntax for any shell that I know.

Here is a Windows BAT script that does the job. The two "echo" statements are only to see what is happening; they can be removed.

Code: Select all

rem Convert *.png files that are fully opaque.

setlocal enabledelayedexpansion

for %%i in (*.png) do (
  echo %%i

  for /F "usebackq" %%F in (`identify -format %%[opaque] %%i`) do set OP=%%F

  echo !OP!

  if "!OP!"=="true" convert %%i %%~ni.jpg
)

Re: Batch convert while checking transparency

Posted: 2014-10-24T08:59:32-07:00
by malbertmalbert
Works beautifully, wish there was a reputation system here to give you some.
I had one last question I did this:

Code: Select all

  if "!OP!"=="true" convert %%i %%~ni.jpg>>changedimages.txt
Added ">>changedimages.txt" to output the name of a file that it changed to a text file, but it only creates the text file and doesn't write anything to it. I need this so I can keep track of the files that are changed.
It would be best if after it appends a file's name to go to the next line as well if that is possible. Any ideas?

Re: Batch convert while checking transparency

Posted: 2014-10-24T13:09:33-07:00
by snibgo

Code: Select all

  if "!OP!"=="true" convert %%i -format "%%f\n" -write info: %%~ni.jpg>>changedimages.txt
"-write info:" writes some default text, which I have over-ridden with "-format".

%f is the escape for the filename. For other escapes, see http://www.imagemagick.org/script/escape.php

"\n" adds a newline.

EDIT: Or, if you want to keep it simple, leave the convert alone and add an echo command:

Code: Select all

if "!OP!"=="true" (
  convert %%i -format "%%f\n" -write info: %%~ni.jpg
  echo %%i>>changedimages.txt
)