Add top and left borders with magick command - imagemagick

Is there a way to add 5 or any number of white/transparent pixels at the top and left borders of an image with the magick command in Linux?

Use the -splice operator. First make a solid magenta rectangle:
magick -size 100x50 xc:magenta image.png
Now splice on a yellow chunk (so you can see it) 10 wide and 20 tall:
magick image.png -background yellow -gravity northwest -splice 10x20 result.png
Change yellow to none for transparent pixels.
Change magick to convert for v6 ImageMagick.
If you just want to splice to the East side:
magick image.png -background yellow -gravity east -splice 10x east.png
If you just want to splice to the South side:
magick image.png -background yellow -gravity south -splice x10 south.png

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

Imagemagick Add Border to Inner Rectangle Only

I am trying to add a border to the 100x100 blue square only, without adding it to the 200x200 background red frame. How would I achieve this? I've experimented with many configurations including parentheses ().
exec("convert -size 200x200 xc:red -size 100x100 -draw rectangle -bordercolor yellow -border 2 xc:blue -geometry +5+5 -composite temp_bg.png ")
Try starting with the blue box, adding a border and then extending like this:
magick -size 100x100 xc:blue -bordercolor lime -border 10 -gravity northwest -background red -extent 400x400 -bordercolor yellow -border 10 result.png
Sorry, no time, gotta dash - add in a -page ... or -geometry ... before extent to set the position of the blue box.

Imagemagick create image and place image inside with max size

I have trimmed .pngs and need them placed on a canvas (3000x3000 px) BUT with a max size.
convert -size 3000x30000 xc:transparent test.png -gravity south -composite -size 2200x2200 result.png
The code I have now works, but the sizing of the image is off. My canvas is 3000x3000px as intended, but the image placed on the canvas doesn't have the correct size. It should have a max width/height of 2200px and if possible be scaled up, if they are to small in height.
This ImageMagick command will resize the input image to 2200 pixels on the longer side while maintaining its aspect, then create a 3000x3000 transparent canvas, then swap the input image and the canvas, and finish by compositing the resized input image onto the transparent canvas...
convert input.png -resize 2200x2200 \
-size 3000x3000 xc:none +swap -gravity south -composite result.png
For Windows change that continued line backslash "\" to a caret "^". For ImageMagick v7 use "magick" instead of "convert".
Unix syntax:
convert -size 3000x30000 xc:transparent \( test.png -resize 2200x2200 \) -gravity south -composite result.png
Windows syntax:
convert -size 3000x30000 xc:transparent ( test.png -resize 2200x2200 ) -gravity south -composite result.png
Why not use -extent?
convert input.jpg -resize 2200x2200 -background none -gravity south -extent 3000x3000 result.png

ImageMagick white out border stripes

I am trying to white out borders of an image. That is, white out 100 px vertical stripe from left, and similarly from right, top, bottom. The following works for left:
mogrify -crop +100+0 -background white -gravity west -splice 100x aaa.tif
But I cannot figure out how to do the same with other sides. I tried many geometries, east, west, this, that, no success. Also please let me know if there is a better alternative than the above command.
Start with a rose:
I'll do the borders with yellow and magenta so you can see what I am doing on StackOverflow's white background.
All Sides
Shave 10px off all sides and then put 10px back on all sides:
convert rose: -shave 10x10 -bordercolor magenta -border 10 result.png
Right Side
convert rose: -gravity east -chop 10x -background yellow -splice 10x result.png
Left Side
convert rose: -gravity west -chop 10x -background yellow -splice 10x result.png
Top
convert rose: -gravity north -chop x10 -background yellow -splice x10 result.png
Bottom
convert rose: -gravity south -chop x10 -background yellow -splice x10 result.png
Left and Right
convert rose: -shave 10x -bordercolor magenta -border 10x result.png
Top and Bottom
convert rose: -shave x10 -bordercolor magenta -border x10 result.png
Tags: ImageMagick, border, bordering, inside, gravity, one side, multiple sides, edges, framing, frame, overpaint, white-out
If you want the equivalent of Photoshop's "Border Outside" just omit the -shave or -chop.
Might be worth exploring -extent option, but I feel it could be quicker just to append padding.
For example...(using blue for visual example)
convert -background blue \
-size 100x xc:blue \
\( rose: -crop +50+0 \) \
-size 100x xc:blue \
+append \
output.png

how do i place a 4 * 6 image on a letter page at the top

I am using imagemagick to convert files and reposition them, i have a 4 * 6 png which i need to position on a letter canvas on the top half of the page.
I have the below command which i am using, but its confusing. can anyone suggest how i can achieve what i want.
this is what i have tried, can any one guide me on this.
convert -rotate -270 -page Letter me-9370120111400937899958.png on-9370120111400937899958.pdf
I have also tried this, but the overlayed image is not moving and is stuck to the bottom
%x{convert -page Letter -rotate -270 "/var/folders/rp/rk2q4l7j4ds_w37vwvgx46tr0000gn/T/a8.png" -geometry +50+50 "/var/folders/rp/rk2q4l7j4ds_w37vwvgx46tr0000gn/T/a8.pdf"}
I have tried reading on this link http://www.imagemagick.org/script/command-line-processing.php#geometry but could not figure out.
Updated Answer
It occurred to me later that you may have meant this:
convert -page letter -gravity north \( a.jpg -background yellow -splice 0x10 \) a.pdf
Obviously change yellow to none and increase the 0x10 to 0x20 to move further down the page, and add -rotate 90 before the splice maybe.
Original Answer
Not sure exactly what you mean, but I think this will get you started. Let's try some options. I will make the canvas yellow so you can see it and the file you want to position on top will be red.
So let's try some options...
First, let's move the image across to the right by zero pixels and down from the top-left by 40 pixels - the default position (or gravity) is NorthWest so we are positioning relative to that.
convert -size 612x792 xc:yellow a.jpg -geometry +0+40 -composite result.jpg
If you want the image centred, use -gravity north and position relative to that - a little closer to the edge this time maybe:
convert -size 612x792 xc:yellow -gravity north a.jpg -geometry +0+10 -composite result.jpg
If you want the background rotated:
convert -size 792x612 xc:yellow -gravity north a.jpg -geometry +0+40 -composite result.jpg
If you want just the overlay rotated, do that in "aside processing":
convert -size 612x792 xc:yellow -gravity north \( a.jpg -rotate 90 \) -geometry +0+40 -composite result.jpg
If you want the canvas white, change yellow to white. If you want the canvas transparent, change yellow to none.

Resources