transparent pixel check, and image dimensions check

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
illyume

transparent pixel check, and image dimensions check

Post by illyume »

I'm pretty sure there's a way to do both of these, and the second one possibly even without imagemagick.

I'm not sure quite what the syntax is, but I want a line of code in a .bat file I'm creating, that's something like:

if [image's top-left-pixel]==transparent goto :skipTransparencyFunction

I just need the syntax on using imagemagick to return the value for an images's top-left-pixel, and check it against a transparent value, to see if it is.

The images in question are .png files, a sample here: http://i29.tinypic.com/ay264y.png

---

Also, I'm needing to check images' dimensions, and if they're above something like 2,000 pixels in either dimension, size the highest one down to 2,000 pixels. I can't seem to find a simple way to check this, in ImageMagick's documentation... and I imagine there's a way to have ImageMagick return just the width or just the height of an image, in number format, right?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: transparent pixel check, and image dimensions check

Post by fmw42 »

In IM command line mode:

see

http://www.imagemagick.org/script/escape.php
http://www.imagemagick.org/Usage/transform/#fx_escapes

Using the IM internal image rose:


convert rose: -format '%[fx:u.a]' info:

if the result is not zero then it is not fully transparent.

This gets the width
identify -ping -format '%w' rose:

This gets the height
identify -ping -format '%h' rose:

and this gets widthxheight

identify -ping -format '%wx%h' rose:

For converting to windows batch processing, see
http://www.imagemagick.org/Usage/windows/
illyume

Re: transparent pixel check, and image dimensions check

Post by illyume »

Alright, perfect. That's just what I needed. :D

This is what I ended up putting into my transparency script function:

Code: Select all

FOR /F %%t IN ('convert %1 -format %%[fx:u.a] info:') DO SET trans=%%t
if %trans%==0 goto :transIMG
It may not be the best way to do it, but it works just fine, so I'm satisfied with it. The transIMG goto point contains a little function that ouputs to my logfile that the current image in question already has transparency, and doesn't need to be converted, and then has a goto command to send it to the end of the .bat file

Next thing to tackle is the image-size testing, and downsizing. Shouldn't be too tough, I figure. The line "identify -ping -format %%w %filename%" works exactly like I need it to.
illyume

Re: transparent pixel check, and image dimensions check

Post by illyume »

Alright, got what I wanted. If anyone's curious, this is my final code:

BatchResize.bat

Code: Select all

::This .bat program requires ImageMagick to be installed on the local computer. www.imagemagick.org
@ECHO OFF
ECHO BatchConvert Program
ECHO Writing log file to resize_log.txt
ECHO. >> resize_log.txt
ECHO. >> resize_log.txt
ECHO. >> resize_log.txt
ECHO. >> resize_log.txt
Echo ----- ----- ----- >> resize_log.txt
ECHO Function called at %time% on %date% >> resize_log.txt
set /a varfilecount=0
set /a varcount=0
set /a varconvcount=0
for /F "usebackq delims=" %%k IN (`dir *.png /s /b`) DO  set /a varfilecount+=1
echo %varfilecount% files found to test for resizing.
echo %varfilecount% files found to test for resizing. >> resize_log.txt
pause
for /F "usebackq delims=" %%k IN (`dir *.png /s /b`) DO call resize.bat "%%k"
ECHO --- >> resize_log.txt
ECHO Functions completed successfully. >> resize_log.txt
ECHO --- --- --- >> resize_log.txt
ECHO. >> resize_log.txt
PAUSE
resize.bat

Code: Select all

@echo off
echo File: %1
FOR /F %%h IN ('identify -ping -format %%h %1') DO SET /a imgheight=%%h
FOR /F %%w IN ('identify -ping -format %%w %1') DO SET /a imgwidth=%%w
echo --- >> resize_log.txt
echo File: %1 >> resize_log.txt
echo Image dimensions: %imgwidth% by %imgheight%
echo Image dimensions: %imgwidth% by %imgheight% >> resize_log.txt
if %imgwidth% gtr 1600 goto :resize
if %imgheight% gtr 1600 goto :resize
goto :small


:resize
echo Resizing image...
convert -filter Gaussian -resize 1600x1600 %1 "%~dpn1.png"
echo Image resized. >> resize_log.txt
goto :Endcall
set /a varconvcount+=1
:Endresize

:small
echo Image does not need to be resized. >> resize_log.txt
goto :Endcall
:Endsmall

:endCall

cls
set /a varcount+=1 
echo %varcount% of %varfilecount%
Post Reply