Extract multiple images from single image using mask

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
wyattbiker
Posts: 3
Joined: 2018-10-11T14:31:45-07:00
Authentication code: 1152

Extract multiple images from single image using mask

Post by wyattbiker » 2018-10-11T14:39:42-07:00

I have an image.png with multiple rectangles, each one surrounded by transparency. All different width/heights.

I created a mask using:

Code: Select all

convert image.png -alpha extract mask.png
So the transparency area is all black and the images are all white.

Now what I would like to do is extract from image.png into as many files as there are white mask images. Is there some way I can do this? Or is there a better way.

Thanks

User avatar
fmw42
Posts: 24134
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Extract multiple images from single image using mask

Post by fmw42 » 2018-10-11T14:48:01-07:00

Please, always provide your IM version and platform when asking questions, since syntax may differ.

If you are on a Unix like system, then see my script, multicrop2, at my link below.

Is that what you are trying to do?

If not, post your input image to some free hosting service such as dropbox.com that won't change the format (or zip it first) and put the URL here.

snibgo
Posts: 10958
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Extract multiple images from single image using mask

Post by snibgo » 2018-10-11T14:57:30-07:00

I show some methods on Subimage rectangles, with Windows BAT scripts.
snibgo's IM pages: im.snibgo.com

wyattbiker
Posts: 3
Joined: 2018-10-11T14:31:45-07:00
Authentication code: 1152

Re: Extract multiple images from single image using mask

Post by wyattbiker » 2018-10-11T15:38:12-07:00

fmw42 wrote:
2018-10-11T14:48:01-07:00
Please, always provide your IM version and platform when asking questions, since syntax may differ.

If you are on a Unix like system, then see my script, multicrop2, at my link below.

Is that what you are trying to do?

If not, post your input image to some free hosting service such as dropbox.com that won't change the format (or zip it first) and put the URL here.
Hi thanks for the response. Version: ImageMagick 7.0.8-12 Q16 x86_64 2018-09-23 https://imagemagick.org

It looks like multicrop2 it will do the job. I am using a Mac OS X, High Sierra, any issues there?

So the command can be as simple as this?

Code: Select all

./multicrop2 -b none image.png outimages.png
I don't need unrotate nor extend/trim atm and I am using the rgb(0,0,0,0.0) as the mask.

User avatar
fmw42
Posts: 24134
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Extract multiple images from single image using mask

Post by fmw42 » 2018-10-11T15:51:32-07:00

If your background is not a pure color, then you might need to use the -f option. Also if there is noise in the image, you may want to use the -d option. There is no provision for you to provide your own mask. The script generates its own mask from the background color and fuzz value.

If you background is not nearly constant, then if you have a mask, you may use it to convert your input image so that it has a constant color background. But that is your preprocessing before providing that result to the script.

wyattbiker
Posts: 3
Joined: 2018-10-11T14:31:45-07:00
Authentication code: 1152

Re: Extract multiple images from single image using mask

Post by wyattbiker » 2018-10-12T06:44:54-07:00

Here is the final command that worked.

Code: Select all

../multicrop2 -b "rgb(0,0,0,0.0)" mergedimage.png test.png
Background is constant. Great utility. Thanks!

Post Reply