Overlap multiple images at some pixel level using Imagemagick - imagemagick

For example, I have 4 images with a size of 1000x800. I want to merge all these images into one image. I know there is a command of
convert +append image[1-4].jpg output.jpg
But I want to merge the second image into the first image by overlapping 250 pixels.
i.e., In my final image, image1 has 750 pixels as well as image2 & 3 and the last image has 1000 pixels.
By using above convert command, we will get an image size of 4000x800, but here I want it to be ((750*3)+1000*1)x800. i.e., 3250x800.
Also, how can I do this by appending vertically?

The "+smush" operator will act like "+append", but it takes an argument. A positive number will append the images separated by that many pixels. A negative number will append the images with that much overlap. Your example command would look like this...
convert image[1-4].jpg +smush -250 output.jpg
To append images vertically, use the "-smush" form of the operator. In either case the operator will align the images according to the current "-gravity" setting.

You do not say how you want to overlap them. If you do not need to blend, then in ImageMagick, you can use smush rather than append. Smush allows offsets (gaps) or overlaps. The former with positive values and the latter with negative values to overlap succeeding images. -smush appends vertically and +smush appends horizontally. The background color controls the spacing for gaps when using positive values for the argument.
So try
convert image[1-4].jpg +smush -256x0 output.jpg
or
convert image1.jpg image2.jpg image3.jpg image4.jpg +smush -256x0 output.jpg
or if those are the only images with that syntax, then
convert image*.jpg +smush -256x0 output.jpg

I just tested the following in Imagemagick 6.9.9.33 Q16 Mac OSX.
Note I tested using -smush with positive values so as to see gaps and not overlaps. That was just for testing. If you follow the last command, it should work fine with negative values, if you want to overlap rather than put gaps between the images.
Create 13 images named rose0.jpg ... rose12.jpg
convert rose: -fill white -gravity center -pointsize 18 -annotate +0+0 "0" test/rose0.jpg
convert rose: -fill white -gravity center -pointsize 18 -annotate +0+0 "1" test/rose1.jpg
convert rose: -fill white -gravity center -pointsize 18 -annotate +0+0 "2" test/rose2.jpg
convert rose: -fill white -gravity center -pointsize 18 -annotate +0+0 "3" test/rose3.jpg
convert rose: -fill white -gravity center -pointsize 18 -annotate +0+0 "4" test/rose4.jpg
convert rose: -fill white -gravity center -pointsize 18 -annotate +0+0 "5" test/rose5.jpg
convert rose: -fill white -gravity center -pointsize 18 -annotate +0+0 "6" test/rose6.jpg
convert rose: -fill white -gravity center -pointsize 18 -annotate +0+0 "7" test/rose7.jpg
convert rose: -fill white -gravity center -pointsize 18 -annotate +0+0 "8" test/rose8.jpg
convert rose: -fill white -gravity center -pointsize 18 -annotate +0+0 "9" test/rose9.jpg
convert rose: -fill white -gravity center -pointsize 18 -annotate +0+0 "10" test/rose10.jpg
convert rose: -fill white -gravity center -pointsize 18 -annotate +0+0 "11" test/rose11.jpg
convert rose: -fill white -gravity center -pointsize 18 -annotate +0+0 "12" test/rose12.jpg
Then smush them. This fails and only appends 0-2. (I think this stucture is limited to whatever digits it sees)
convert test/rose[0-12].jpg -smush 10 test/rose_smush.jpg
This works if using only one-digit numbers.
convert test/rose[0-9].jpg -smush 10 test/rose_smush.jpg
The proper way to do this is to use the following structure.
convert test/rose%d.jpg[0-12] -smush 10 test/rose_smush.jpg
See the section of Filename Reference at http://www.imagemagick.org/script/command-line-processing.php

Related

align text to left with imagemagick

I am using ImageMagick to add text to an image. I used to -gravity Center flag to put the text in the center of the image. But I think this is resulting in the text being center aligned too. I want the text at the center of the image but left aligned. Here is an example of what I'm trying to have:
This is the output I'm getting:
Current output
This is what I want:
This is my requirement
How do I accomplish this? This is my first time using ImageMagick. Please guide me.
Here is one way to do that in Imagemagick 6. I specify the background color, the font color (fill), the font and the pint-size and gravity west (left side). I use label: to create the two lines of text with a new line between them. This creates a text image of the size needed to hold the text. Then I pad the image all around that to the final size with the text image in its center using the same background color.
convert -background black -fill white -font ubuntu -pointsize 28 -gravity west label:"This is line 1 of text\nThis is line 2 of text" -gravity center -extent 400x300 result.png
See
https://imagemagick.org/Usage/text/
https://imagemagick.org/Usage/crop/#extent
ADDITION
If you want to put the text over an existing image, then you do something similar, but in place of the extent, we composite the text image over the background image.
The following is Unix syntax. For Window, remove the backslashes \ before the parentheses.
Input:
convert lena.png \( -background black -fill white -font ubuntu -pointsize 21 -gravity west label:"This is line 1 of text\nThis is line 2 of text" \) -gravity center -compose over -composite result.png
Result:
Or if you do not want the black background, use "none" for the color.
convert lena.png \( -background none -fill white -font ubuntu -pointsize 21 -gravity west label:"This is line 1 of text\nThis is line 2 of text" \) -gravity center -compose over -composite result2.png

Why is this ImageMagick convert call not adding centered text to my image?

I have, in a script:
os.system('convert -font ' + os.path.join(os.path.dirname(__file__),
'fr/Verdana.ttf') + ' -gravity Center -pointsize 100 label:"Fr."' +
' /tmp/fr/' + filename + ' /tmp/fr/' + filename)
That should be equivalent to:
convert -font /Users/jonathan/link/fr/Verdana.ttf -gravity Center -pointsize 100 label:"Fr." /tmp/fr/12345.png /tmp/fr/12345.png
But no text or other discernible change occurs on the image.
I have also tried:
os.system('convert -font ' + os.path.join(os.path.dirname(__file__),
'fr/Verdana.ttf') + ' -gravity Center -pointsize 100 -annotate 0 "Fr."' +
' /tmp/fr/' + filename + ' /tmp/fr/' + filename)
This should be equivalent to:
convert -font /Users/jonathan/link/fr/Verdana.ttf -gravity Center -pointsize 100 -annotate 0 "Fr." /tmp/fr/12345.png /tmp/fr/12345.png
What should I be doing differently to have "Fr." centered in large black letters in a font size I provide?
Thanks,
The problem is caused by the fact that label: creates its own canvas. Let's start with a green image called a.png
Now, if you try to add a label like this:
convert a.png -gravity Center -pointsize 100 label:"Fr." result.png
you will get TWO output images - result-0.png and result-1.png because, as far as ImageMagick is concerned, you have said you want the original image (a.png) and another lump of canvas with some letters on that you gave no indication where you would like it put.
If you tell ImageMagick to append these two canvases side by side in a single picture like this, you will see what you have created
convert a.png -gravity Center -pointsize 100 label:"Fr." +append result.png
The secret is to composite the label on top of your original image, whilst making sure the background of the label is transparent:
convert a.png -gravity Center -pointsize 100 -background none label:"Fr." -composite result.png
I can get the desired result (to a 2nd file) if I use the equivalent of:
convert /tmp/fr/12345.png -font /Users/jonathan/link/fr/Verdana.ttf -gravity Center -pointsize 100 label:"Fr." -composite /tmp/fr/12345.new.png

Add gradient overlay under a photo label using imagemagick

I'm trying to convert a bunch of photos using imagemagick. However, I hadn't figured out how to overlay an image with gradient and write some text on it. I know the text part though:
convert IMG_8408.jpg \
-font URWChanceryMediumI \
-pointsize 250 \
-draw "gravity south
fill black text 0,40 'Some text stuff here'" \
test.jpg
Is there a way to add a white gradient to the bottom? Note, that the image size may vary.
What I have:
What I want:
I picked the colors so that it's clearly visible what I want to achieve
You can achieve desired output with 3 commands:
a. create the upper part of your image (a solid rectangle with your selected background color):
convert -size 640x200 xc:#A02B2B background.jpg
b. create another image containing the text over a gradient:
convert -size 640x110 gradient:#A02B2B-#126B27 -pointsize 25 -draw "gravity south fill black text 0,40 'Some text stuff here'" text.jpg
c. combine the images to obtain the final output:
montage background.jpg text.jpg -tile 1x2 -geometry +0+0 output.jpg
Note: I modified text creation parameters in step 2 to keep the command short, but you can add back your original settings
Use the following command:
magick -size 640x310 -define gradient:vector="0,107 0,0" gradient:"#a02b2b-#126b27" -flip -gravity south -font script-mt-bold -pointsize 48 -annotate +0+24 "Some text stuff here" output.png

ImageMagick background image?

I just recently started to explore ImageMagick and I have a doubt. I have been searching but could not find a solution.
With the following code:
convert -background lightblue -fill blue -font Candice -pointsize 30 \
-size 220x220 -gravity Center caption:'$txt' \
$name.gif
How do I use an image as background instead a static color?
I have tried -background "file.jpg" but it does not work.
Use -texture instead of -background.
I think I missunderstood your initial question. This will tile an image onto a set size background and add some text.
$txt = 'Caption';
// Tile the image - no background as it will not be seen behind the tile
$cmd = " -size 220x220 tile:noise.jpg -font Candice -fill blue -pointsize 30 ".
"-gravity Center -annotate +0+0 $txt";
exec("convert $cmd output.jpg");
Quite a few ways depending the effect you are after but this is one simple way:
convert input.jpg -fill blue -font Candice -pointsize 30 -gravity Center -annotate +0+0 "Some text" $name.gif
I can not get the hang of the way this site posts code :(

Imagemagick label issue

I am using imagemagick 6.2.8 and i want to add a label at the bottom of the image aligned on the right side.
this is my code:
convert image.png -gravity center -background "#f0f0f0" -font bgothm.ttf -pointsize 18 label:"text text" -append "append_image.png"
however the label is aligned to the left
How can i align the label to the right?
Edit:
I found this: Before IM v6.4.7 it was much more difficult to align appended images, and generally involved using a "-flop" for right alignment. Or using "-extent" or "-border" to adjust the image width for centered aligned appends.
But i never used imagemagick
To align bottom-right, use -gravity southeast.
convert image.png -gravity southeast -background "#f0f0f0" -font bgothm.ttf -pointsize 18 label:"text text" -append "append_image.png"
Edit: I have updated my solution to work with your ImageMagick version.
Do the double -flop trick. One flop to mirror the text, so it will be right aligned and one flop to mirror the append to make it readable:
convert image.png -background "#f0f0f0" -font bgothm.ttf \
-pointsize 18 label:"text text" -flop -append -flop "append_image.png"
Produces this image for me. Please note that the order of -flop -append -flop do matter!
But still, a better solution would be to upgrade ImageMagick to >= 6.4.7 and use -gravity SouthEast:
convert image.png -gravity SouthEast -background "#f0f0f0" -font bgothm.ttf \
-pointsize 18 label:"text text" -append "append_image.png"

Resources