Imagemagick create image and place image inside with max size - imagemagick

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

Related

how to imagemagick crop only width

im trying to crop an image of size 1920x125058 into 900x125058 from the center
so I used magick convert in.png -crop 900x+510+0 out.png but this outputs a file of size 900x28800. shouldn't the height be untouched if it is not mentioned?
In Imagemagick 7, use magick, not magick convert. For center cropping, use -gravity center
magick in.png -gravity center -crop 900x125058+0+0 +repage out.png

How to use imagemagick to give background and dimensions to my screenshot?

I am new to Image Magick but am trying to get it to convert my image to a size and background pleasant for Twitter. The qualities I'm going for are as follows:
my overall canvas size 16:9, so about 1200x675
my actual screenshot centered and about 3/4 the width
background texture of my choosing
My latest attempt is with the following, but it doesn't seem to actually make any noticeable changes.
convert ds.png -adaptive-resize 1200 -size 1200x675 -texture ~/Pictures/DoctorWho/horizon.jpg -gravity Center ds.png
In Imagemagick 6, Unix syntax, if you just want to resize a small background image, then try
convert \( background.suffix -resize 1200x675 \) \( foreground.suffix -resize x506 \) -gravity center -compose over -composite result.suffix
If you want to crop a large image for the background, then try
convert \( background.suffix -gravity center -crop 1200x675+0+0 +repage \) \( foreground.suffix -resize x506 \) -gravity center -compose over -composite result.suffix
If you need to tile out a small image for the background, then
convert \( -size 1200x675 tile:background.suffix \) \( foreground.suffix -resize x506 \) -gravity center -compose over -composite result.suffix
For Windows remove the \ from the parentheses.
For Imagemagick 7, replace convert with magick.

Imagemagick - move/offset images within a tiled montage?

Consider this example (Ubuntu 18.04, ImageMagick 6.9.7-4 Q16 x86_64 20170114):
convert -size 300x100 xc:red red.png
convert -size 100x100 xc:blue blue.png
montage red.png blue.png -frame 5 -geometry '+0+0' -tile 1x redblue.png
This gives the following image:
What I'd like to do, is move the blue square on arbitrary x position "within its tile"; say align left edge of blue square to where 25% of the red rectangle width would be, or at 50% - or even align right edge of blue square with right edge of red rectangle.
I have seen that -tile-offset exists (https://imagemagick.org/script/command-line-options.php#tile-offset), and I've tried it with this example, but it doesn't look like it does anything.
How can I move an image, part of a ImageMagick montage, within its tile?
EDIT: it looks like -tile-offset can only be specified for explicit tile images (not as in -tile 1x, but as in -tile red.png), and:
Tile smaller image over a background with offsets? - ImageMagick
-tile-offset must come before the tiling. It represents a single global offset, not the spacing for the tiling.
Here's an example:
convert -size 300x100 radial-gradient:\#400-\#FAA red.png
convert -size 500x500 xc: -tile-offset +100+40 +size -tile red.png -border 5 -geometry +5+5 -draw "color 0,0 reset" out.png
then out.png is this (click for full image):
... so to clarify - I'd like to know is its possible to move an image within a tile as obtained in montage tile 1x
As suggested in the comment:
convert -background none red.png \( -size 25x xc:none blue.png +append \) -append result.png
Or with 2 different offsets:
convert -background none red.png \
\( -size 25x xc:none blue.png +append \) \
\( -size 50x xc:none blue.png +append \) \
-append result.png
Not sure what your end-goal is, but you can also do this:
convert -gravity east -background none red.png blue.png red.png blue.png -append result.png
Or this:
convert -gravity center -background none red.png blue.png red.png blue.png -append result.png
In ImageMagick 6, another way is to extend the transparent background, then composite the blue image in the center of the bottom half of the extended image.
convert -size 300x100 xc:red -background none -extent 300x200 -size 100x100 xc:blue -geometry +100+100 -composite result.png

how to place a resize image on a black layer, imagemagick

I would like to take a picture, resize it to 50% and put it on a black layer...
My issue is that resizing the image, resize the whole image (with the black layer too)and put it at the center of the new layer...
here's my code:
'convert -size 1920x1080 xc:Black -gravity center image.png -resize 20% -composite -flatten result.png'
how could I do to just resize the image.png and not the whole layer ?
thanks in advance
g.
I think you probably want this:
convert image.png -resize 50% -gravity center -background black -extent 1920x1080 result.png
Or this, which is another way of doing the same thing:
convert -size 1920x1080 xc:black \( image.png -resize 50% \) -gravity center -composite result.png
Or, a more succinct version:
convert xc:black[1920x1080\!] \( image.png -resize 50% \) -gravity center -composite result.png
The first one resizes your image and then extends it with black from the centre outwards to your desired size.
The second creates the correctly sized canvas, then loads your image and resizes it "on-the-side" in parentheses and composites the result onto the canvas.

How to square an image and pad with transparency from the commandline (imagemagick)

The section entitled Square Padding or Cropping describes a method to generate a square image--for a file whose dimensions are unknown--and pad the background with a color.
How do I perform the same operation, but create a transparent background.
Let's make a red off-square image first, that is 300x200:
convert -size 300x200 xc:red image.png
Now let's put make a square image of it, but using a yellow background so you can see it:
convert -background yellow -gravity center image.png -resize 400x400 -extent 400x400 result.png
Now we can do the same thing again, but make the background transparent:
convert -background none -gravity center image.png -resize 400x400 -extent 400x400 result.png
and, just check to make sure it has worked:
identify result.png
result.png PNG 400x400 400x400+0+0 8-bit sRGB 418B 0.000u 0:00.000
These modified methods from Anthony's examples both work for me:
convert thumbnail.gif \
\( +clone -rotate 90 +clone -mosaic +level-colors grey -transparent grey \) \
+swap -gravity center -composite square_padded.gif
convert thumbnail.gif -virtual-pixel none -set option:distort:viewport \
"%[fx:max(w,h)]x%[fx:max(w,h)]-%[fx:max((h-w)/2,0)]-%[fx:max((w-h)/2,0)]" \
-filter point -distort SRT 0 +repage square_external.gif
A pure imagemagick command would be preferable, but here's a script that uses the 'file' unix/linux command to extract the dimensions of the file which can then be used on a resize to a square of the max dimension.
#!/usr/bin/env ruby
require 'shellwords'
def dims(image_escaped)
size_data = `file #{image_escaped}`
size_data[/, (\d+ x \d+),/, 1].split(' x ').map(&:to_i)
end
def square(image, pad_color='transparent')
image_esc = Shellwords.escape(image)
maxdim = dims(image_esc).max
geometry = "#{maxdim}x#{maxdim}"
# could use convert if don't want to clobber the image
system "mogrify -resize #{geometry} -background #{pad_color} -gravity center -extent #{geometry} -format png #{image_esc}"
end
ARGV.each do |image|
square(image)
end

Resources