Conditional processing based on a given pixel's color?

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
hyppy

Conditional processing based on a given pixel's color?

Post by hyppy »

I've done some searching for a possible solution to this problem, but so far drawn a blank. Any pointers would be much appreciated.

I've a script to create '3D' product images. It takes an image, distorts it, then drops it onto a pre-prepared background containing the common sides and shadow of the product. All is good, and I end up with nice 3D product images on white backgrounds. Here's my script:

Code: Select all

convert source.tif -colorspace RGB -resize 277x350! -alpha on   -virtual-pixel transparent -filter point       \( +clone -flip  -size 277x80 gradient:gray40-black            -alpha off -compose CopyOpacity -composite \) -append       +distort Perspective '0,0 37,44 0,277 37,213 277,350 199,280 350,0 259,18'       -gravity North  -crop 277x350+53-0\!       -background transparent -compose Over -flatten   background.png +swap -composite output.png
The problem though is that in the 1% of cases where source.tif has predominantly white edges, I end up with no distinction between product and background: a white product on a white background.

Is it possible to have the script amended to the following pseudo code:

if the pixel at coordinates 1,1 of source.tif is color #ffffff, add a 1px #cccccc border to the image before processing as normal.

Thanks in advance!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Conditional processing based on a given pixel's color?

Post by fmw42 »

takes multiple commands

color=`convert image -format "%[pixel: s.p{0,0}]" info:`
if [ "$color" = "white" ] then
convert image -bordercolor "#cccccc" -border 1 result
fi

see http://www.imagemagick.org/Usage/transform/#fx_escapes

Note this is unix, so windows users must use other commands. see http://www.imagemagick.org/Usage/windows/
hyppy

Re: Conditional processing based on a given pixel's color?

Post by hyppy »

Thanks Fred,

Not sure I fully understand this yet, but will check it out fully and have play monday morning.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Conditional processing based on a given pixel's color?

Post by fmw42 »

You could also use -fuzz and -trim to remove near white borders and then pad back with -extent to the original size

size=`convert image -format "%wx%h" info:`
convert image -bordercolor white -border 1 -fuzz XX% -trim +repage -background "#cccccc" -extent "$size!" result

where XX% is some percent range near white

see http://www.imagemagick.org/Usage/crop/#trim_color
Post Reply