Crop Image within the border of specific 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
krunalmaniar13
Posts: 4
Joined: 2019-08-14T07:43:16-07:00
Authentication code: 1152

Crop Image within the border of specific color

Post by krunalmaniar13 »

Hello All,

I have a image where i have drown a boundry of specific color, and i want to now crop this image and have only those part of the image which is inside that specific boundry.

Ex:

I have a image of world map and i want cropped image of particular country (Ex. India) whose boundry is drawn with Yellow Color.
The resultant image should only contain the India's map and all the other part of the image can be made blank.

Can somebody suggest the command which i can use for this, Ideally i believe there should some command which find the boundary of particular hash code of color and given the inner part of that boundry as a image.

Thanks in advance for your help
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Crop Image within the border of specific color

Post by snibgo »

What version of IM, on what platform? Sample input image?

One method would be to flood-fill from the top-left corner with yellow. Then trim.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Crop Image within the border of specific color

Post by fmw42 »

Please post an image. You will need to upload to some free hosting service and put the URL here.
krunalmaniar13
Posts: 4
Joined: 2019-08-14T07:43:16-07:00
Authentication code: 1152

Re: Crop Image within the border of specific color

Post by krunalmaniar13 »

Following is the image:
https://ibb.co/3pHgn6S

Here i just want the inner part of the dark yellow boundry
krunalmaniar13
Posts: 4
Joined: 2019-08-14T07:43:16-07:00
Authentication code: 1152

Re: Crop Image within the border of specific color

Post by krunalmaniar13 »

I am using following exe from Imagemagick site

ImageMagick-7.0.8-60-Q16-HDRI-x64-dll.exe

And yes i am doing it on windows Machine
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Crop Image within the border of specific color

Post by fmw42 »

This is in Unix. One of the Windows users can give you the equivalent.

The technique is to clone the image to make a mask. In the clone, make anything not yellow into black and everything else white. Then flood fill from the center with white to make everything inside the yellow area including the yellow into white. Then put the mask into the alpha channel of the original image.

Input:
Image

Code: Select all

declare $(magick GIS-County.jpg -format "centx=%[fx:w/2]\ncenty=%[fx:h/2]" info:)

magick GIS-County.jpg \
\( -clone 0 -fuzz 15% -fill black +opaque "rgb(243,246,67)" -fill white +opaque black \
-fill white -draw "color $centx,$centy floodfill" \) \
-alpha off -compose copy_opacity -composite \
GIS-County_result.png
Image


In Windows, remove the \ from the parentheses. Change the end of line \ to ^. Fill in the $centx,$centy with the center values so that you can ignore the declare line. Otherwise, a Windows user will need to show you the equivalent code for creating and using variables.
krunalmaniar13
Posts: 4
Joined: 2019-08-14T07:43:16-07:00
Authentication code: 1152

Re: Crop Image within the border of specific color

Post by krunalmaniar13 »

Hey fmw42,

Thank you very much for your response, your script was very helpful.
Post Reply