Check if Corners are transparent

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
gotskill10
Posts: 17
Joined: 2007-06-02T14:01:34-07:00

Check if Corners are transparent

Post by gotskill10 »

How would I check if an Image has atleast one corner pixel that is transparent?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Check if Corners are transparent

Post by fmw42 »

gotskill10 wrote:How would I check if an Image has atleast one corner pixel that is transparent?
convert each of the four corner pixels, one at a time to txt: format and look at the information

Make test image as gradient with left half transparent and right half opaque:
convert -size 100x100 gradient: -rotate 90 grad100.png
freds-mac-mini:~ fred$ convert grad100.png -threshold 50% grad100t50.png
freds-mac-mini:~ fred$ convert grad100.png grad100t50.png -compose copy_opacity -composite grad100_at50.png

convert grad100_at50.png[1x1+0+0] txt:-
# ImageMagick pixel enumeration: 1,1,65535,rgba
0,0: ( 0, 0, 0, 0) #000000000000000 none

So the above is transparent:


convert grad100_at50.png[1x1+99+99] txt:-
# ImageMagick pixel enumeration: 1,1,65535,rgba
0,0: (65535,65535,65535,65535) #FFFFFFFFFFFF white

So the above is opaque
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Check if Corners are transparent

Post by anthony »

Their was also a post recently about extracting the colors of the four corners, this may provide a better check. For example.

Code: Select all

convert grad100_at50.png -roll +1+1 -crop 2x2+0+0 +repage txt:
produces
# ImageMagick pixel enumeration: 2,2,65535,rgba
0,0: (65535,65535,65535,65535) #FFFFFFFFFFFF white
1,0: ( 0, 0, 0, 0) #0000000000000000 none
0,1: (65535,65535,65535,65535) #FFFFFFFFFFFF white
1,1: ( 0, 0, 0, 0) #0000000000000000 none
or two corners are white and two are transparent.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Check if Corners are transparent

Post by fmw42 »

convert grad100_at50.png -roll +1+1 -crop 2x2+0+0 +repage txt:
neat way to get only the 4 corners
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Check if Corners are transparent

Post by anthony »

And by using -roll you avoid needing to know the size of the image ;-)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply