How to crop out multiple pictures on a single file?

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
waykeong

How to crop out multiple pictures on a single file?

Post by waykeong »

Image

I have a scanned picture file like example above, which consists of 2-3 picture with different/same sizes.

May I know from the command line, how I can crop out all areas in white color?

If those pictures are same size, any command line I can use to crop each picture and save new files? Also is it possible to crop if pictures' size are different?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to crop out multiple pictures on a single file?

Post by anthony »

The simplest way is to use a script I wrote and added to the IM Examples script area...
http://imagemagick.org/Usage/scripts/divide_vert

this divides the image into segments seperated by lines of constant color!!!!
It has options to remove those lines.

Once you have the images seperated you can trim them to remove the horizontale components.


I created the script to seperate lines of text from an image, and would like to see it added to IM core, as a form a simple image segmentation (both horizontally and vertically).

Basically it was a proof of concept. I am sure a Perl or other language API version can be made which is faster.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
waykeong

Re: How to crop out multiple pictures on a single file?

Post by waykeong »

anthony wrote:The simplest way is to use a script I wrote and added to the IM Examples script area...
http://imagemagick.org/Usage/scripts/divide_vert

this divides the image into segments separated by lines of constant color!!!!
It has options to remove those lines.

Once you have the images separated you can trim them to remove the horizontal components.


I created the script to seperate lines of text from an image, and would like to see it added to IM core, as a form a simple image segmentation (both horizontally and vertically).

Basically it was a proof of concept. I am sure a Perl or other language API version can be made which is faster.
can u send me a compiled version of ur script?
also what is the command line i should use?

Thanks. I am just thinking of making a .bat file, cos I am not so well versed in programming. :>
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to crop out multiple pictures on a single file?

Post by Bonzo »

Using php:
1/ Upload the image
2/ Upload the script and CHMOD it to 755
3/ Write a php file with:

Code: Select all

<?php
// Run the script
// Full path to the script
exec("/home/<user>/public_html/TESTS/divide.sh waykeong.jpg result.png 2>&1", $array); 
//Display any errors
echo "<br>".print_r($array)."<br>"; 
echo "</pre>";
?>
4/ Run the php file

You will end up with file named result-0.png result-1.png etc

I then tried a -trim to remove some of the waste ( I wonder if you could run the script again ? ) on the images I wanted to keep but it didn't remove all the waste.

In windows it would be a pain to run a shell script? but I think its quite straight forward in Linux.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to crop out multiple pictures on a single file?

Post by anthony »

waykeong wrote:can u send me a compiled version of ur script?
also what is the command line i should use?
The script is a UNIX shell script, and as such not compilable, or usable under windows.

In summery what it does is...
  • Save a copy of the original image (so that image is only read once)
    ASIDE: the copy is saved as a temporary ".mpc/.cache" file to allow fast reading by later programs, but for an existing image this step does not matter. also during this step the height of the image is noted.

    Code: Select all

    height=`convert "$input" -format '%[fx:u.h-1]' -identify $tmpdir/original.mpc`
  • Create a list of the number of colors, and the first color of each row of pixels in the original image

    Code: Select all

    convert $tmpdir/original.mpc -crop 0x1 +repage \
            -format '%k %[pixel:s]' info:$tmpdir/image_data
  • loop though that list locating the top of each divisions (and number of rows or height of each division). Divide image when the number of colors changes between: row of one, constant color (gaps - a option to ignore these is present) and more than one color (the interesting parts).

    Code: Select all

    convert $tmpdir/original.mpc -crop 0x$h+0+$top +repage $file
  • extract the last division, which may be a gap or multi-color division, when loop finishes.
  • Create a new image sequence of the divided areas found.
    In no divisions found return whole image, (or null: if image is blank and gap images are specified to be ignored)
It looks complex but the only complexity is looping though the list of color and color counts that was generated by step two above.

As I mentioned I would like to see this script added to the IM core library, but with a 'fuzz' factor used to compare the first color of each division with the rest of the division to allow for division of JPEG and scanned images containing noise.

If both horizontal and vertical divider routines are created then this could also be used to generate a auto-split of GIF animations
http://imagemagick.org/Usage/anim_mods/#split
And posibly as part of a set of routines to output DjVu Image format (good for high compressed scanned documents)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
swistaczek

Re: How to crop out multiple pictures on a single file?

Post by swistaczek »

Could anyone post here this script again? I cant find it
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to crop out multiple pictures on a single file?

Post by anthony »

The URL given above is still valid. I also sent it to you in another discussion.

But here it is again
http://www.imagemagick.org/Usage/scripts/divide_vert

you may also like to look at
http://www.imagemagick.org/Usage/scripts/segment_image
Though that is designed to find masks or areas of constant colors

ALL these are image segmentation methods.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
jumpjack
Posts: 69
Joined: 2010-12-10T05:29:16-07:00
Authentication code: 8675308

Re: How to crop out multiple pictures on a single file?

Post by jumpjack »

I bring up this old thread to ask if, in last two years, something new came around about image segmentation.

I'd like to be able to extract all words from an image, to use each single image as if it was a character in a text, thus allowing text reflow even in scanned pages.

Example image:
Image
By copying a line and pasting it 1 pixel to the right and again 2 pixels to the right, all characters in a word get "melted" together, but words are still separated; melting characters should make it easier to identify single words, .e. determining the boxes which include them. Once I have a box per each word, it's easy to extract and save them.

The script I'm trying to write picks each word and saves it into a separated file.


I think I should use -segment function, but I'm not sure. Can anybody confirm, and give some hints about were to start from?
http://www.tiem.utk.edu/~sylv/HTML/Imag ... gment.html
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to crop out multiple pictures on a single file?

Post by anthony »

The -segment operator is a very old mis-named operator that performs 'color segmention' so as to try an group the colors within an image. It is part of color reduction, and is nothing to do with spatial or image segmentation that you are wanting.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
jumpjack
Posts: 69
Joined: 2010-12-10T05:29:16-07:00
Authentication code: 8675308

Re: How to crop out multiple pictures on a single file?

Post by jumpjack »

So?
No clues about how to determine areas containig words?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to crop out multiple pictures on a single file?

Post by anthony »

Areas containing words are basically areas containing lots of separate segments.

Essentially segment your image (in some way) then junk any large segments.
Now junk any segments that are not found in groups. What is lets should be word-like.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply