Copy area from infile to outfile

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
Bastrabun
Posts: 3
Joined: 2020-01-16T12:02:28-07:00
Authentication code: 1152

Copy area from infile to outfile

Post by Bastrabun »

I'm on some Ubuntu with IM version 8:6.9.7.4+dfsg-16ubuntu6.8

I have a 64x64 png file and want to extract certain areas of various sizes, then stitch them together in a new png.

Like "Copy region at offset x=4,y=0 with size +8 in x-direction and +8 in y-direction of infile.png and paste it to offset x=8,y=8 of outfile.png, without resizing or altering the ciopied region in any other way" ?

I tried some of the examples, looks like I, being a beginner, cannot wrap my head around

User avatar
fmw42
Posts: 26261
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Copy area from infile to outfile

Post by fmw42 »

Please post an example input and output image to some free hosting service and put the URLs here. We can then help you better.

Otherwise, see the -crop command and the -composite command.

https://imagemagick.org/Usage/crop/#crop
https://imagemagick.org/Usage/compose/#compose

Bastrabun
Posts: 3
Joined: 2020-01-16T12:02:28-07:00
Authentication code: 1152

Re: Copy area from infile to outfile

Post by Bastrabun »

I crop-ped and composite-d back and forth for a while, but somehow I wasn't able to achieve what I wanted.

Attached are infile.png and outfile.png

https://i.postimg.cc/nzqv4V8s/infile.png
https://i.postimg.cc/wxyKdSX1/outfile.png

Context: These (infiles) are the files you get from Minecraft skin editors, like this: https://www.minecraftskins.net/skineditor
To be able to use them in Minetest (https://minetest.net , it is a free and open source game "inspired" by Minecraft), I need to generate a skin-preview (outfile) file, which is what I'm trying to achieve here. In the 64x64 infile there are areas that stand for the limbs, the body or the head and they need to go to the areas in the 16x32 outfile of the same colour, while the background needs to be transparent.

While I would be happy to have such a script ready, I'd be even more happy if I understood how to do it myself.

User avatar
fmw42
Posts: 26261
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Copy area from infile to outfile

Post by fmw42 »

Do you have a list of the crop coordinates for this image for each color, the size of the output you want, and the upper left corner of where you want to put each color? If so, we can help further.

You can also auto convert the white to transparency and extract the bounding boxes for each color in one command using connected components. For example the following code:

Code: Select all

convert infile.png -transparent white \
-define connected-components:verbose=true \
-define connected-components:mean-color=true \
-connected-components 4 \
temp.png | grep -v "srgba(255,255,255,0)"
Will convert the white to transparent and then find the bounding boxes of each color region so that you can crop them out using those coordinates. The grep removes all the transparent regions from the list. It produces.

Code: Select all

Objects (id: bounding-box centroid area mean-color):
  4: 8x12+20+20 23.5,25.5 36 srgba(34,177,76,1)
  9: 4x12+20+52 21.5,57.9 31 srgba(185,122,87,1)
  5: 4x12+44+20 45.5,25.1 31 srgba(163,73,164,1)
  10: 4x12+36+52 37.5,57.1 31 srgba(255,127,39,1)
  3: 4x12+4+20 5.5,25.9 31 srgba(0,162,232,1)
  1: 8x8+8+8 11.5,11.5 28 srgba(237,28,36,1)

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

Re: Copy area from infile to outfile

Post by GeeMack »

Bastrabun wrote:
2020-01-16T15:59:50-07:00
In the 64x64 infile there are areas that stand for the limbs, the body or the head and they need to go to the areas in the 16x32 outfile of the same colour, while the background needs to be transparent.

While I would be happy to have such a script ready, I'd be even more happy if I understood how to do it myself.
This command would take your "infile.png" and output your "outfile.png" with a transparent background...

Code: Select all

convert infile.png -background none -transparent white  \
   \( -clone 0 -crop 8x8+8+8 -set page 16x32+4+0 \) \
   \( -clone 0 -crop 8x12+20+20 -set page 16x32+4+8 \) \
   \( -clone 0 -crop 4x12+4+20 -set page 16x32+4+20 \) \
   \( -clone 0 -crop 4x12+44+20 -set page 16x32+0+8 \) \
   \( -clone 0 -crop 4x12+20+52 -set page 16x32+8+20 \) \
   \( -clone 0 -crop 4x12+36+52 -set page 16x32+12+8 \) \
   -delete 0 -flatten newoutfile.png
That starts by reading the input image and clears the all-white pixels to transparent. Then it clones that image several times inside several sets of parentheses. Inside each parentheses the individual parts are cropped to their dimensions and offsets like WxH+x+y, and the new canvas size and locations are set for each piece. That is also specified as WxH+x+y geometry. After all the parts are cut out and their new page geometry set, the original is deleted and the pieces are flattened onto a transparent background.

That should give you a good idea of how to isolate the crop operations by working on clones inside parentheses. If all your input / output dimensions are always the same, just feed it different file names. If dimensions and layouts change for each run, send the sizes and coordinates into the command as variables.

Bastrabun
Posts: 3
Joined: 2020-01-16T12:02:28-07:00
Authentication code: 1152

Re: Copy area from infile to outfile

Post by Bastrabun »

I tried your solution, and it does what it is supposed to. Thank you very much you two!

I'm especially happy that I do not need to resort to my previous attempt, making temporary files all over.

With "original deleted", you talk about the original clone with id 0, I guess, not the infile, right? Asking, because the infile does not get deleted (and I don't want it to be deleted either)

I remember trying to use clone by myself, but looks like I never got the syntax right or shot myself into the foot by some other way.

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

Re: Copy area from infile to outfile

Post by snibgo »

ImageMagick works with lists of images. In this case, it reads infile.png so there is one image in the list. Then it makes a clone so now there are two. And so on. "-delete 0" deletes the first image in the list. "-delete" doesn't delete any files.
snibgo's IM pages: im.snibgo.com

Post Reply