Page 1 of 1
Chopping(?) up my image
Posted: 2016-06-13T16:14:03-07:00
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
Re: Chopping(?) up my image
Posted: 2016-06-13T17:02:23-07:00
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.
Re: Chopping(?) up my image
Posted: 2016-06-13T17:30:31-07:00
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:
Re: Chopping(?) up my image
Posted: 2016-06-13T17:39:02-07:00
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.
Re: Chopping(?) up my image
Posted: 2016-06-13T17:45:06-07:00
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:
Re: Chopping(?) up my image
Posted: 2016-06-13T18:34:42-07:00
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
Re: Chopping(?) up my image
Posted: 2016-06-13T18:44:11-07:00
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.
Re: Chopping(?) up my image
Posted: 2016-06-13T18:53:51-07:00
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
Re: Chopping(?) up my image
Posted: 2016-06-13T19:56:33-07:00
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.
Re: Chopping(?) up my image
Posted: 2016-06-14T10:29:15-07:00
by markdoughty2000
You guys are awesome. Thanks for your help. I'm going to go try it right now.
mark