Chopping(?) up my image

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
markdoughty2000
Posts: 2
Joined: 2016-06-13T16:00:15-07:00
Authentication code: 1151

Chopping(?) up my image

Post by markdoughty2000 »

Hi all, I've been banging my head on a wall trying to figure out how to use the command line to split up my image the way I want. What I have and what I'd like:

I have a single texture that is a 200x200 image.

I'd like as output 4 200x200 images (so all the same size as the input image), but with each one having a filled quadrant of 100x100 with the other 3 quadrants now empty and transparent.

My closest try:
convert MyImage.png -crop 100x100 +repage MyImage_100x100.png

But of course I don't actually want 4 100x100 images. I want the quadrant images to stay 'in place' at 200x200, but just with the rest of the stuff erased. I was currently heading down the path of doing extra processing afterwards to rebuild a 200x200 image, but this seems tedious and unnecessary, and I am betting this can more easily be done in a single line if I knew what I was doing.

Any help would be greatly, greatly appreciated!

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

Re: Chopping(?) up my image

Post by snibgo »

There's probably a beautiful, simple, elegant method, but I can't see one. Meanwhile, something like this (untested):

convert
in.png
-background None
( +clone -gravity NorthWest -crop 100x100+0+0 +repage -extent 200x200 +write outNW.png +delete )
( +clone -gravity NorthEast -crop 100x100+0+0 +repage -extent 200x200 +write outNE.png +delete )
( +clone -gravity SouthWest -crop 100x100+0+0 +repage -extent 200x200 +write outSW.png +delete )
( +clone -gravity SouthEast -crop 100x100+0+0 +repage -extent 200x200 +write outSE.png +delete )
NULL:

Add line-endings and other escapes as required by your shell.
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: Chopping(?) up my image

Post by fmw42 »

Independently we have pretty much the same.

One issue above, is that each line must end in ^ except the last one, if those are really new lines. (NOTE: this has been fixed by snibgo, by his note about line endings).

This is tested. In my case using mpr: (and likely snibgo's result above using +clone), all images are deleted except the last. If I delete the last, then IM objects since it has no images at the end and does not like null: with no images to delete.

Using 50% allows this to work for any input size.

Unix syntax

Code: Select all

convert \
MyImage.png -write mpr:img +delete \
\( mpr:img -gravity northwest -crop 50%x50%+0+0 +repage \
-background none -extent 256x256 +write nw_quad.png +delete \) \
\( mpr:img -gravity northeast -crop 50%x50%+0+0 +repage \
-background none -extent 256x256 +write ne_quad.png +delete \) \
\( mpr:img -gravity southeast -crop 50%x50%+0+0 +repage \
-background none -extent 256x256 +write se_quad.png +delete \) \
\( mpr:img -gravity southwest -crop 50%x50%+0+0 +repage \
-background none -extent 256x256 +write sw_quad.png \) \
null:
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Chopping(?) up my image

Post by snibgo »

fmw42 wrote:... If I delete it, then IM objects since it has no images at the end and does not like null: with no images to delete.
Oops, yes, that would cause mine to fail. Thanks for the correction.

Incidentally, percentages can be used in "-extent", so "-extent 200%x200%" could be used here.
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: Chopping(?) up my image

Post by fmw42 »

incidentally, percentages can be used in "-extent", so "-extent 200%x200%" could be used here.
Oops! I forgot to fix that. Thanks, snibgo. So it should be:

Code: Select all

convert \
MyImage.png -write mpr:img +delete \
\( mpr:img -gravity northwest -crop 50%x50%+0+0 +repage \
-background none -extent 200%x200% +write nw_quad.png +delete \) \
\( mpr:img -gravity northeast -crop 50%x50%+0+0 +repage \
-background none -extent 200%x200% +write ne_quad.png +delete \) \
\( mpr:img -gravity southeast -crop 50%x50%+0+0 +repage \
-background none -extent 200%x200% +write se_quad.png +delete \) \
\( mpr:img -gravity southwest -crop 50%x50%+0+0 +repage \
-background none -extent 200%x200% +write sw_quad.png \) \
null:
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Chopping(?) up my image

Post by fmw42 »

Here is another way:

Code: Select all

convert -size 256x256 xc:none \
null: \( MyImage.png -crop 50%x50% \) -layers Composite \
quads1_%d.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Chopping(?) up my image

Post by snibgo »

Yes, that's good, Fred. Here's another:

convert rose:
-background None -crop 50%x50%
( -clone 0 -flatten )
( -clone 1 -flatten )
( -clone 2 -flatten )
( -clone 3 -flatten )
-delete 0-3 out_%d.png

As before, add escapes that your shell wants.
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: Chopping(?) up my image

Post by fmw42 »

Here is a simplification of my previous approach that does not require knowledge of the image size.

Code: Select all

convert MyImage.png \
\( -clone 0 -alpha transparent \) \
null: \( -clone 0 -crop 50%x50% \) -delete 0 -layers Composite \
quads1_%d.png
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Chopping(?) up my image

Post by GeeMack »

With IM7 this also works...

Code: Select all

magick input.png -crop 2x2@ +repage -distort SRT '%[fx:t*90]' \
   -background none -extent %[fx:w*2]x%[fx:h*2] +repage -distort SRT '%[fx:t*270]' output%d.png
To change the order of the output images you can "-swap" around some images in the stack before the first "-distort" or after the last one.

This would require the input/output images are square, but the size doesn't need to be specified in the command.
markdoughty2000
Posts: 2
Joined: 2016-06-13T16:00:15-07:00
Authentication code: 1151

Re: Chopping(?) up my image

Post by markdoughty2000 »

You guys are awesome. Thanks for your help. I'm going to go try it right now. :)

mark
Post Reply