Getting a perimeter and filling in the rest

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
dcotto
Posts: 12
Joined: 2019-09-24T15:24:28-07:00
Authentication code: 1152

Getting a perimeter and filling in the rest

Post by dcotto »

trying to make a mask out of this image. How would one go about creating it

my assumption would be to use a threshold where the white perimeter is solid white.
if i get the perimeter to be continuous solid white, how would i tell IM to close/fill the inside?

if someone knows a better approach please let me know thanks

below is the image
https://ibb.co/QYWHkmC
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Getting a perimeter and filling in the rest

Post by snibgo »

What version of IM, on what platform?

What mask do you want? The central rectangle, or that plus the enclosing black area, or that plus the enclosing white area, or what?
snibgo's IM pages: im.snibgo.com
dcotto
Posts: 12
Joined: 2019-09-24T15:24:28-07:00
Authentication code: 1152

Re: Getting a perimeter and filling in the rest

Post by dcotto »

i want to mask the whole ipad, starting from the white outer portion. so in the end it should look like in the image below but better (the image still has some black leaking in the white areas

https://ibb.co/M6v2swB
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Getting a perimeter and filling in the rest

Post by snibgo »

Your required output has a wide black margin on the right. I don't know where that has come from. Ignoring that, we can floodfill to isolate the outer black border. That gives a rough edge we can smooth with a blur. The blur has to be large enough to help, but not so large that it damages the curved corners. We do this three times for an almost perfect result. Windows BAT syntax.

Code: Select all

magick ^
  i-Pad-threshold.png ^
  -strip ^
  -fill Red -draw "color 0,0 floodfill" ^
  -fill White +opaque red ^
  -fill Black +opaque White ^
  ( +clone -channel RGB -blur 0x10 -threshold 50%% +channel ) ^
  -compose Lighten -composite ^
  ( +clone -channel RGB -blur 0x10 -threshold 50%% +channel ) ^
  -compose Lighten -composite ^
  ( +clone -channel RGB -blur 0x10 -threshold 50%% +channel ) ^
  -compose Lighten -composite ^
  x.png
snibgo's IM pages: im.snibgo.com
Post Reply