Peter Hull
Peter Hull

Reputation: 7077

Assembling an image from parts of others with ImageMagick (image crop syntax)

I have a set of source PNG images and I want to use parts of them to assemble a final PNG image. The parts are rectangular and never overlap on the destination but are of different sizes. Sometimes it is the whole of a source image and sometimes just a subsection. I want to edit the sources many times and re-assemble the final image each time, so I tried to write a script using sh and Imagemagick to do it.

I tried this

convert \
-size 512x512 null:\
-page +96+32 source_a.png\
-page +96+0 source_b.png[32x32+16+16]  \
-background transparent\
-layers merge\
destination.png

(just with two source images for illustration) I want all of source_a.png and a piece of source_b.png. The first is OK, but using the 'inline crop' syntax on source_b.png gives me an error:

convert: geometry does not contain image `source_b.png' @ warning/transform.c/CropImage/666.

The image is big enough:

$ identify source_b.png
source_b.png PNG 64x48 64x48+0+0 8-bit sRGB 3.7KB 0.000u 0:00.000

What's the best way to do this? I am using ImageMagick 6.9.7-0 Q16 on MacOS 10.12

Upvotes: 2

Views: 284

Answers (2)

Mark Setchell
Mark Setchell

Reputation: 208077

An alternative might be to use -geometry and -composte to achieve the same effect:

convert -size 512x512 xc:white                            \
    source_a.png              -geometry +96+32 -composite \
    source_b.png[32x32+16+16] -geometry +96+0  -composite \
    result.png

Upvotes: 3

emcconville
emcconville

Reputation: 24439

PNG's will preserve the paging from inline cropping, so the addition page will through the ROI out of bounds. I imaging it'll be simpler to -repage the inline crop then attempting to clear previous paging & setting new page.

convert -size 512x512 null: \
        -page +96+32 source_a.png \
        \( source_b.png[32x32+16+16] -repage +96+0 \) \
        -background transparent\
        -layers merge\
        destination.png

Upvotes: 2

Related Questions