Split image to 2 files with different width

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
Gurthfin
Posts: 8
Joined: 2013-04-30T18:16:11-07:00
Authentication code: 6789

Split image to 2 files with different width

Post by Gurthfin » 2018-03-04T08:00:51-07:00

Hi,
I think I do this wrong way, but I wanna split image with 2000px width 1000px height to 2 files, where first have 900px width and second 1200px width. I find this

Code: Select all

magick convert t_ari13a.png -crop 900x1000 tile-%d.png
but this produce 3 files 900x1000, 900x1000 and 200x1000. I looking at crop example and trying everything, but nothing works.

Oh yes, I forgot. I known only vertical split is at 900px.

Bonzo
Posts: 2970
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Split image to 2 files with different width

Post by Bonzo » 2018-03-04T08:46:36-07:00

The problem is your code gets as many images as it can out of the available width with your width setting; hence the 200 wide piece. I am not sure of a way around it other than doing two crops. There may be something here but I could not see anything: https://www.imagemagick.org/Usage/crop/

With V7 you should only need magick and not magick convert.

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

Re: Split image to 2 files with different width

Post by snibgo » 2018-03-04T09:02:53-07:00

If you want two different crops, I would do it like this (Windows BAT syntax):

Code: Select all

magick t_aril3a.png ^
  ( -clone 0 -crop 900x1000+0+0 +repage ) ^
  ( -clone 0 -crop 1200x1000+0+0 +repage ) ^
  tile-%d.png
snibgo's IM pages: im.snibgo.com

Gurthfin
Posts: 8
Joined: 2013-04-30T18:16:11-07:00
Authentication code: 6789

Re: Split image to 2 files with different width

Post by Gurthfin » 2018-03-04T09:21:20-07:00

It wont works. I have lot of pictures and I known only split width. Next picture is 1055x744 and split is on 517.

User avatar
GeeMack
Posts: 653
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Split image to 2 files with different width

Post by GeeMack » 2018-03-04T09:22:52-07:00

Gurthfin wrote:
2018-03-04T08:00:51-07:00
I think I do this wrong way, but I wanna split image with 2000px width 1000px height to 2 files, where first have 900px width and second 1200px width. I find this

Code: Select all

magick convert t_ari13a.png -crop 900x1000 tile-%d.png
but this produce 3 files 900x1000, 900x1000 and 200x1000. I looking at crop example and trying everything, but nothing works.
If you want the first piece to be 900 pixels wide (or any particular width) and the second piece to contain the remainder of the image, there are several ways to accomplish this sort of thing. One simple method that comes to mind is something like this...

Code: Select all

magick input.png -crop 900x ( -clone 1--1 +append ) -delete 1--2 output-%d.png
That crops the image into as many 900 pixel wide pieces as it can, clones everything from the second piece onward and reassembles them, then deletes the unneeded parts before writing the two output images.

Gurthfin
Posts: 8
Joined: 2013-04-30T18:16:11-07:00
Authentication code: 6789

Re: Split image to 2 files with different width

Post by Gurthfin » 2018-03-04T09:34:48-07:00

Thank you, this works.
Is possible to use name of file and add a number to it -example

Code: Select all

magick t_ari13a.png -crop 900x ( -clone 1--1 +append ) -delete 1--2 [name]_%d.png
and produce
t_ari13a_d-0.png
t_ari13a_d-1.png

or something like that?

Bonzo
Posts: 2970
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Split image to 2 files with different width

Post by Bonzo » 2018-03-04T09:35:27-07:00

I thought you should be able to use -gravity but it didn't work.

User avatar
GeeMack
Posts: 653
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Split image to 2 files with different width

Post by GeeMack » 2018-03-04T09:50:39-07:00

Gurthfin wrote:
2018-03-04T09:34:48-07:00
Is possible to use name of file and add a number to it -example

Code: Select all

magick t_ari13a.png -crop 900x ( -clone 1--1 +append ) -delete 1--2 [name]_%d.png
and produce
t_ari13a_d-0.png
t_ari13a_d-1.png
You can use the input file name to set the output file name like this...

Code: Select all

magick input.png -set filename: "%[t]" -crop 900x ( -clone 1--1 +append ) -delete 1--2 "%[filename:]-%d.png"

Gurthfin
Posts: 8
Joined: 2013-04-30T18:16:11-07:00
Authentication code: 6789

Re: Split image to 2 files with different width

Post by Gurthfin » 2018-03-04T10:09:38-07:00

Perfectly works, thank you very much

Post Reply