Tile smaller image over a background with offsets?

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?".
grape
Posts: 6
Joined: 2018-10-02T13:15:03-07:00
Authentication code: 1152

Tile smaller image over a background with offsets?

Post by grape »

Hi, i have been playing with imagemgick for a little bit now. Its really powerful, but the documentation is a bit confusing for me. Hopefully you guys can help me out here.

basically I have a small vector head, and another larger background. I read about the composite command with the tile options like this

magick composite -tile head.jpg bg.jpg output.jpg

but the issue is that the head head just tiles the entire background, and i cant see the background anymore. What I am trying to achieve is the following. The image alignment will pretty much always be alternating.

when i try to add --tile-offset option, the composite command complains, how can I achieve the following result?

I am using Version: ImageMagick 7.0.8-10 Q16 x86_64 2018-08-15 on OSX, but will move to a linux enviroment later.

Edit:
Sorry for being unclear.

Here is what I would like to achieve..

image1 to be overlayed repeated on top, this can be jpg or a png
Image

image2 is the background image
Image

this is the output of how it should come out you can ignore the cat with the space helmet for now. there will only be 1 type of cat head on the background

Image
Last edited by grape on 2018-10-02T18:17:39-07:00, edited 3 times in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Tile smaller image over a background with offsets?

Post by fmw42 »

Please post your two input images? Perhaps you do not want to use tiling? Also you should be using magick and not magic composite. That is and older tool and is giving you IM 6 results. See https://imagemagick.org/Usage/compose/#compose

Use the convert ... -composite syntax. But for Imagemagick 7 change it to magick ... -composite syntax.

See also https://imagemagick.org/script/porting.php#cli

If you can post your two images, then we can be more helpful. Also what is your exact IM 7 version. You can get that from magick -version.

You also say one image is vector, but you show in your command two raster images. Please clarify.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Tile smaller image over a background with offsets?

Post by snibgo »

It seems you have three raster images: stars, cat-helmet and just-cat. The background image might be any size, and you want the cat images to be placed as shown, filling the background, but with gaps as shown. Is that correct?

Then the trick is to first make an image, mostly transparent, with cat-helmet at bottom-left and just_cat at top-right. Then tile that image over the background.
snibgo's IM pages: im.snibgo.com
grape
Posts: 6
Joined: 2018-10-02T13:15:03-07:00
Authentication code: 1152

Re: Tile smaller image over a background with offsets?

Post by grape »

fmw42 wrote: 2018-10-02T17:27:57-07:00 Please post your two input images? Perhaps you do not want to use tiling? Also you should be using magick and not magic composite. That is and older tool and is giving you IM 6 results. See https://imagemagick.org/Usage/compose/#compose

Use the convert ... -composite syntax. But for Imagemagick 7 change it to magick ... -composite syntax.

See also https://imagemagick.org/script/porting.php#cli

If you can post your two images, then we can be more helpful. Also what is your exact IM 7 version. You can get that from magick -version.

You also say one image is vector, but you show in your command two raster images. Please clarify.
sorry for using confusing terminology. What I meant to say is how I can over lay 1 image over another bigger one, with the first image being offset

snibgo wrote: 2018-10-02T17:40:02-07:00 It seems you have three raster images: stars, cat-helmet and just-cat. The background image might be any size, and you want the cat images to be placed as shown, filling the background, but with gaps as shown. Is that correct?

Then the trick is to first make an image, mostly transparent, with cat-helmet at bottom-left and just_cat at top-right. Then tile that image over the background.
yeah exactly. but lets just forget about the cat-helmet for now and use the plain cat. My question is what does the command loook like? How do i add offset value to the IM command?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Tile smaller image over a background with offsets?

Post by fmw42 »

Here is one example (Unix Syntax)

Line1 - read the stars image
Line2 - read the cat image and resize 50% and write to in memory mpr: format and delete the original cat
Line3 - copy the stars image and tile over it with the mpr:cat image
Line4 - composite the tiled cat image over the stars image
Line5 - save the output.

See https://imagemagick.org/Usage/canvas/#tile for the replacement of an image with tiled other image. That save one from having to extract the size of the background image in order to create a tiled image the same size.

Code: Select all

magick stars.jpg \
\( cat.png -resize 50% -write mpr:cat +delete \) \
\( +clone -tile mpr:cat -draw "color 0,0 reset" \) \
-compose over -composite \
stars_cat.jpg
Image


Here is an alternate, that creates offset appending of the cat and a same sized transparent image. Then it is tile out and composited with the stars image.

Code: Select all

magick stars.jpg \
\( cat.png -resize 50% \) \
\( +clone -channel rgba -alpha transparent \) \
\( -clone 1 -clone 2 +append \) \
\( -clone 2 -clone 1 +append \) \
\( -clone 3 -clone 4 -append -write mpr:cat +delete \) \
-delete 1,2,3,4 \
\( +clone -tile mpr:cat -draw "color 0,0 reset" \) \
-compose over -composite \
stars_cat2.jpg
Image

If you want more random placement, then you should composite the cat image several times over the stars image in different locations using -gravity and -geometry. See https://imagemagick.org/Usage/layers/#convert
grape
Posts: 6
Joined: 2018-10-02T13:15:03-07:00
Authentication code: 1152

Re: Tile smaller image over a background with offsets?

Post by grape »

fmw42 wrote: 2018-10-02T20:32:21-07:00 Here is one example (Unix Syntax)

Line1 - read the stars image
Line2 - read the cat image and resize 50% and write to in memory mpr: format and delete the original cat
Line3 - copy the stars image and tile over it with the mpr:cat image
Line4 - composite the tiled cat image over the stars image
Line5 - save the output.

See https://imagemagick.org/Usage/canvas/#tile for the replacement of an image with tiled other image. That save one from having to extract the size of the background image in order to create a tiled image the same size.

Code: Select all

convert stars.jpg \
\( cat.png -resize 50% -write mpr:cat +delete \) \
\( +clone -tile mpr:cat -draw "color 0,0 reset" \) \
-compose over -composite \
stars_cat.jpg
Image


Here is an alternate, that creates offset appending of the cat and a same sized transparent image. Then it is tile out and composited with the stars image.

Code: Select all

convert stars.jpg \
\( cat.png -resize 50% \) \
\( +clone -channel rgba -alpha transparent \) \
\( -clone 1 -clone 2 +append \) \
\( -clone 2 -clone 1 +append \) \
\( -clone 3 -clone 4 -append -write mpr:cat +delete \) \
-delete 1,2,3,4 \
\( +clone -tile mpr:cat -draw "color 0,0 reset" \) \
-compose over -composite \
stars_cat2.jpg
Image

If you want more random placement, then you should composite the cat image several times over the stars image in different locations using -gravity and -geometry. See https://imagemagick.org/Usage/layers/#convert
wow that is definitely a lot more complicated than I thought. Thank you so much for this indepth answer! Reading through it, I def get what you were doing. The main issue with tiling with transparent cat in between the visible ones, is that the pattern becomes too dense. Is there a way to tile the cat head X and Y coordinate off the right corner, at (0,0), so the first head can be at an offset of (x+100, y+0), then the second head can simple be at (x+150, y+200), third can be at (x+100, y+300).. and so forth. Basically to produce the Exact tile result from the first post?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Tile smaller image over a background with offsets?

Post by fmw42 »

I am not following what you are asking. You can use -tile-offset +x+y before doing the tiling. See https://imagemagick.org/script/command- ... ile-offset. You can increase and position the amount of transparency about the cat image by using -extent with a transparent background. See https://imagemagick.org/Usage/crop/#extent

Is this what you want?

Code: Select all

magick stars.jpg \
\( cat.png -resize 50% -write mpr:cat +delete \) \
\( +clone -tile-offset +150+150 -tile mpr:cat -draw "color 0,0 reset"  \) \
-compose over -composite \
stars_cat1b.jpg
Image

This is probably not what you want. I think you just want to composite the cat image multiple times at the desired locations. If the sequence is a regular offset, then you can write a script loop to do the positioning via

Code: Select all

magick stars.jpg \
cat.png -gravity northwest -geometry +X1+Y1 -compose over -composite \
cat.png -gravity northwest -geometry +X2+Y2 -compose over -composite \
...
cat.png -gravity northwest -geometry +Xn+Yn -compose over -composite \
result.jpg
grape
Posts: 6
Joined: 2018-10-02T13:15:03-07:00
Authentication code: 1152

Re: Tile smaller image over a background with offsets?

Post by grape »

I am pretty new into this whole command line thing here. Basically I need to produce Exactly the output image as in the first post, with just the plain cat head instead of the space helmet one.

what does the command look like if i need to use -tile-offset? because when I use it like this, IM complains

Code: Select all

magick composite -tile -tile-offset +20+20 head.jpg bg.jpg output.jpg
composite: unrecognized option `-tile-offset' @ error/composite.c/CompositeImageCommand/1510.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Tile smaller image over a background with offsets?

Post by fmw42 »

-tile-offset must come before the tiling. It represents a single global offset, not the spacing for the tiling.

You should be using magick ... -composite syntax rather than the older magick composite ... syntax.

I think you just want to composite the cat image multiple times at the desired locations. If the sequence is a regular offset, then you can write a script loop to do the positioning. But here is a manual way.

Code: Select all

magick stars.jpg \
cat.png -gravity northwest -geometry +X1+Y1 -compose over -composite \
cat.png -gravity northwest -geometry +X2+Y2 -compose over -composite \
...
cat.png -gravity northwest -geometry +Xn+Yn -compose over -composite \
result.jpg
Scripting is OS dependent.

Can you explain your offset sequence better so that I can understand how to script a loop over your X and Y changes?
grape
Posts: 6
Joined: 2018-10-02T13:15:03-07:00
Authentication code: 1152

Re: Tile smaller image over a background with offsets?

Post by grape »

fmw42 wrote: 2018-10-03T10:12:29-07:00 -tile-offset must come before the tiling. It represents a single global offset, not the spacing for the tiling.

You should be using magick ... -composite syntax rather than the older magick composite ... syntax.

I think you just want to composite the cat image multiple times at the desired locations. If the sequence is a regular offset, then you can write a script loop to do the positioning. But here is a manual way.

Code: Select all

magick stars.jpg \
cat.png -gravity northwest -geometry +X1+Y1 -compose over -composite \
cat.png -gravity northwest -geometry +X2+Y2 -compose over -composite \
...
cat.png -gravity northwest -geometry +Xn+Yn -compose over -composite \
result.jpg
Scripting is OS dependent.

Can you explain your offset sequence better so that I can understand how to script a loop over your X and Y changes?
YES! This is exactly what I am looking for!! Thank you!!!! The command structure is what was really confusing for me. as a test, I just did this

Code: Select all

magick bg.png \
head.jpg -gravity northwest -geometry +300+300 -compose over -composite \
head.jpg -gravity northwest -geometry +600+950 -compose over -composite \
result.jpg
which placed the head over an arbitry location over the background image. All i have to do is to just do do the math on the loop. It also gives me an option to use a different head because its alternating. This is awesome. Thank you!!

One last thing I noticed was that the bg.png file is original 12mb, the result if I used jpg output is around ~3mb, but if I change it to png, it will be around 12.5mb, I have a couple questions

1. can I stop compression from happening or atleast the level of compression for the result jpg?
2. If I choose to use PNG because its lossless, is it possible to change the colorspace to CMYK via Image magick?
3. when you say scripting is OS dependent, what does that mean exactly? right now this works on OSX, does it mean that on an linux enviroment, the command wouldnt work?
4. regarding -gravity northwest option, the documentation says
If the -gravity option is present with NorthEast, East, or SouthEast gravity, it gives the distance leftward from the right edge of the image to the right edge of the cropping region. Similarly, if the -gravity option is present with SouthWest, South, or SouthEast gravity, the distance is measured upward between the bottom edges.
It looks like its basically telling IM where the reference point is? am I understanding this correctly?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Tile smaller image over a background with offsets?

Post by fmw42 »

1) JPG compression is set by -quality. The least compression and highest quality and biggest file size is at -quality 100. But there will always be some compression even for -quality 100. If you want lossless compression, you need JP2 format.

2) You can change the JPG output colorspace to CMYK by either adding -colorspace CMYK before the output or by using profiles. The latter will be more reliable for viewing as many viewers do not know how to deal with CMYK JPG without profiles. Add -profile USWebCoatedSWOP.icc for example.

3) Linux/Mac (any Unix) script looping and variable definition are different than for Windows. You are not using scripting. However even command line syntax can be different, for example new lines in Windows (^) are different from those in Unix (\).

4) Yes, the gravity is the reference point for where -geometry does its offset.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Tile smaller image over a background with offsets?

Post by snibgo »

grape wrote:2. If I choose to use PNG because its lossless, is it possible to change the colorspace to CMYK via Image magick?
Yes, but PNG can't record CMYK. If that's what you want, choose another format, eg TIFF or JPG.
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: Tile smaller image over a background with offsets?

Post by fmw42 »

snibgo wrote: 2018-10-03T11:44:21-07:00
grape wrote:2. If I choose to use PNG because its lossless, is it possible to change the colorspace to CMYK via Image magick?
Yes, but PNG can't record CMYK. If that's what you want, choose another format, eg TIFF or JPG.
Thanks snibgo. I thought he was asking about JPG. I did not read it carefully enough.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Tile smaller image over a background with offsets?

Post by fmw42 »

This pattern:
so the first head can be at an offset of (x+100, y+0), then the second head can simple be at (x+150, y+200), third can be at (x+100, y+300).. and so forth. Basically to produce the Exact tile result from the first post?
does not put the cat image at the locations you show in your first result. The x coordinates alternate in signs by perhaps 100 or 50 relative to the center and the y coordinates do increase.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Tile smaller image over a background with offsets?

Post by fmw42 »

In IM 7 on Unix, this seems to do what I think you want.

Code: Select all

WxH=`magick stars.jpg -format "%wx%h" info:`
ww=`echo "$WxH" | cut -dx -f1`
hh=`echo "$WxH" | cut -dx -f2`
centx=`magick xc: -format "%[fx:round($ww/2)]" info:`
xx=$((centx))
yy=0
i=0
echo "$hh; $ww; $centx; $xx; $yy;"
magick stars.jpg result.png
while [ $xx -lt $ww -a $yy -lt $hh ]; do
echo "$xx; $yy;"
magick result.png \( cat.png +repage -resize 50% \) -gravity northwest -geometry +${xx}+${yy} -compose over -composite result.png
echo ""
i=$((i+1))
xx=`magick xc: -format "%[fx:$i%2==1?($xx-100):($xx+100)]" info:`
yy=$((yy+100))
done
magick result.png stars_cat3.jpg
rm -f result.png
Image
Post Reply