Cut image into pieces and recombine 3 horizontal

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
Zoker
Posts: 6
Joined: 2016-01-07T02:25:57-07:00
Authentication code: 1151

Cut image into pieces and recombine 3 horizontal

Post by Zoker »

Hi there,

I have three questions:
  1. I want to first cut a image vertical into multiple pieces of the same size. So I have a 800x5000px image for example and want to have 800x1200px images (the last image should be only the rest 800x200px image + white background)

    Code: Select all

    convert -crop 800x1200 input.png output_%d.png
    But I want to start the first cut at the bottom of the image, not at the top. How can I do this?
  2. After that I want to take these pieces and put 3 of them into a horizontal row.
    This is my code:

    Code: Select all

    convert -append cropped_*.png out.png
    How can I set, that it only takes 3 images and after these 3, create a new image with the next 3 images?
  3. Is there also a way to combine these two scripts? Like first cut 3 pieces (starting at the bottom), then combine them vertically, then cut an other 3 and combine them etc.
Thank you very much!
Zoker
sas
Posts: 44
Joined: 2009-11-28T16:35:46-07:00
Authentication code: 8675309

Re: Cut image into pieces and recombine 3 horizontal

Post by sas »

Zoker wrote:But I want to start the first cut at the bottom of the image, not at the top. How can I do this?
Adding "-flip" before and after the "-crop" operation seems to work:

Code: Select all

convert -flip -crop 800x1200 -flip input.png output_%d.png
Zoker wrote:How can I set, that it only takes 3 images and after these 3, create a new image with the next 3 images?
You could use "montage":

Code: Select all

montage cropped_*.png -geometry +0+0 -tile 3x1 out_%d.png
Zoker wrote:Is there also a way to combine these two scripts?
This seems to work:

Code: Select all

montage input.png -flip -crop 800x1200 -flip \
        -geometry +0+0 -tile 3x1 out_%d.png
Zoker
Posts: 6
Joined: 2016-01-07T02:25:57-07:00
Authentication code: 1151

Re: Cut image into pieces and recombine 3 horizontal

Post by Zoker »

sas wrote:

Code: Select all

convert -flip -crop 800x1200 -flip input.png output_%d.png
Awesome, thank you!

But he does not flip the images back again. Only the last one (in this case the top part) is flipped back, but no all others.

The others are not only flipped vertically, but also horizontal...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Cut image into pieces and recombine 3 horizontal

Post by fmw42 »

This should flip all image back again and number them from the original bottom. The last section at the top of the image may be a partial size. The input image needs to come before the crop:

Code: Select all

convert input.png -flip -crop 800x1200 -flip output_%d.png
Zoker
Posts: 6
Joined: 2016-01-07T02:25:57-07:00
Authentication code: 1151

Re: Cut image into pieces and recombine 3 horizontal

Post by Zoker »

fmw42 wrote:This should flip all image back again and number them from the original bottom. The last section at the top of the image may be a partial size. The input image needs to come before the crop:

Code: Select all

convert input.png -flip -crop 800x1200 -flip output_%d.png
Perfect thanks! Can I somehow set the starting number for the %d?

So it does not start with 1, but with 134 e.g
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Cut image into pieces and recombine 3 horizontal

Post by fmw42 »

It should have started with 0, not 1. I believe this should work to start at 134.

Code: Select all

convert input.png -flip -crop 800x1200 -flip -scene 134 output_%d.png
Zoker
Posts: 6
Joined: 2016-01-07T02:25:57-07:00
Authentication code: 1151

Re: Cut image into pieces and recombine 3 horizontal

Post by Zoker »

fmw42 wrote:It should have started with 0, not 1. I believe this should work to start at 134.

Code: Select all

convert input.png -flip -crop 800x1200 -flip -scene 134 output_%d.png
Thank you so much!

Now everything works well, but one thing:

Code: Select all

montage cropped_*.png -geometry +0+0 -tile 3x1 out_%d.png
Most images are put together correctly, but some of them are not in the right order:

Montage 1:
out_1
out_2
out_3
...
Montage x:
out_128
out_129
out_13

So it seems, that he takes the out_13 straight after the out_129.

How can I fix this?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Cut image into pieces and recombine 3 horizontal

Post by fmw42 »

If you have many pieces than you need to use leading zeros in your file names. If you have numbers from 0 to 999, then use %03d. If you have numbers from 0 to 9999, then use %04d. But I am not sure why you should be getting 1,2,3,...13... when you start the numbering from 134?

Code: Select all

convert input.png -flip -crop 800x1200 -flip -scene 134 output_%03d.png
That should number them from 134 - XXX, but you really do not need the %03d vs %d, since your numbers should be in the range 134 to XXX (unless you have more than 999 pieces and then you would need %04d at least)
Zoker
Posts: 6
Joined: 2016-01-07T02:25:57-07:00
Authentication code: 1151

Re: Cut image into pieces and recombine 3 horizontal

Post by Zoker »

Works!!!

I have two images and the first is from 1-133 and the second from 134-x
Post Reply