Create white rectangle with transparent cutout on transparent BG

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
Duwgati
Posts: 2
Joined: 2017-04-04T01:28:44-07:00
Authentication code: 1151

Create white rectangle with transparent cutout on transparent BG

Post by Duwgati »

I need to create a PNG like this: (www.duwgati.nl/product-mask.jpg)
Image

I have managed to create this image except for the transparent cutout: (www.duwgati.nl/product-mask.png)
Image
As you can see, it creates the transparent "cutout" on the white rectangle, so that doesn't seem to be the right approach for my needs.

So how can I achieve my goal?

P.S. This is the code I have used:
exec('c:\\imagemagick\\convert.exe -size 255x479 xc:white -fill "rgba(255, 215, 0, 0.1)" -draw "roundrectangle 20,20 235,459 20,20" PNG32:test/rectangle.png');

exec('c:\\imagemagick\\convert.exe -size 1366x768 xc:"rgba(0,0,0,0)" PNG32:test/test.png');

exec('c:\\imagemagick\\convert.exe -size 1366x768 xc:"rgba(0,0,0,0)" -gravity center -draw "image over 0,0 0,0 test/rectangle.png" PNG32:test/product-mask.png');
Duwgati
Posts: 2
Joined: 2017-04-04T01:28:44-07:00
Authentication code: 1151

Re: Create white rectangle with transparent cutout on transparent BG

Post by Duwgati »

I found the solution myself:

exec('c:\imagemagick\convert.exe -size 1366x768 xc:"rgba(255,215,0,0)" -fill white -draw "rectangle 555,143 811,625" \(-size 216,442 xc:none -fill black -draw "roundrectangle 575,163 791,605 25,25"\) -alpha Set -compose Dst_Out -composite test/product-mask.png ');
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Create white rectangle with transparent cutout on transparent BG

Post by snibgo »

There are many ways of doing this. For example, we can build an image that is black where the output should be transparent, and white where the output should be opaque. Then we make a clone, turn this white, and CopyOpacity to it. Windows BAT script.

Code: Select all

%IM%convert ^
  -size 1366x768 xc:Black ^
  -fill White -draw "rectangle 555,144 810,623" ^
  -fill Black -draw "roundrectangle 575,164 790,603 20,20" ^
  ( +clone -fill White -colorize 100 ) ^
  +swap ^
  -alpha off ^
  -compose CopyOpacity -composite ^
  out.png
snibgo's IM pages: im.snibgo.com
Post Reply