Batch convert while checking transparency

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
malbertmalbert
Posts: 3
Joined: 2014-10-21T14:51:34-07:00
Authentication code: 6789

Batch convert while checking transparency

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Batch convert while checking transparency

Post 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.
snibgo's IM pages: im.snibgo.com
malbertmalbert
Posts: 3
Joined: 2014-10-21T14:51:34-07:00
Authentication code: 6789

Re: Batch convert while checking transparency

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Batch convert while checking transparency

Post 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
)
snibgo's IM pages: im.snibgo.com
malbertmalbert
Posts: 3
Joined: 2014-10-21T14:51:34-07:00
Authentication code: 6789

Re: Batch convert while checking transparency

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Batch convert while checking transparency

Post 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
)
snibgo's IM pages: im.snibgo.com
Post Reply