seamless join PNGs one above the other - bash

How can I use ImageMagick (probably the montage command) to join png images into a new png, where each of the input pngs becomes a part of the final one? All vertically arranged?
Like so:
2 pngs of roughly equal width (±2px) and different heights joined on top of one another seamlessly.
I don't really mind so much what happens to excess/smaller widths.

convert 1.png 2.png -append result.png
To align left edges, add -gravity West.
To align right edges, add -gravity East.
To align centres, add -gravity center.
To add a transparent spacer between:
convert -background none 1.png -size 10x10 xc:none 2.png -append result.png
Oh, you've gone horizontal! Change -append to +append.

this one:
convert -append img1.png img2.png out.png
e.g. from
will get
And the:
convert +append img1.png img2.png out2.png
will produce

Related

ImageMagick: trim but keep a minimum size

I'm using ImageMagick to trim some PDFs of text that I've converted to jpg for a project. In most cases it works well but when the page has just a small amount of text, say half a sentence, trim works too well so the image is just that, half a sentence.
It's a problem as it causes some display issues where I'm presenting it so it'd be helpful if there was a minimum image size. Is there a way of doing that in ImageMagick? Or adding padding to an image if it is converted below a certain size?
This is the command I'm using:
convert '*.jpeg' -fuzz 1% -trim +repage -set filename:base "%[basename]" "%[filename:base].jpg"
I'm using ImageMagick 7.1.0-19 Q16-HDRI arm 2021-12-22
Currently there is no way that I can think that would limit the trim in that way in Imagemagick. But you can automatically pad the image with some color such as white to some minimum size.
However, you will need to use "magick" in place of "convert" for Imagemagick 7 to make it work properly.
Input (256x256):
Lets pad it with white to 100x100 (minimum)
magick image.png -fuzz 1% -trim +repage -gravity center -bordercolor white -border "%[fx:w<100?round(0.5*(100-w)):0]x%[fx:h<100?round(0.5*(100-h)):0]" result.png
or better
magick input.png -fuzz 1% -trim +repage -gravity center -background white -extent "%[fx:w<100?100:w]x%[fx:h<100?100:h]" result.png
I had the exact same question recently, and the answer was to combine -trim and -extent.
convert -trim -background none -gravity center -extent 1024x1024 input.png output.webp
The image will first be trimmed, then extended to a given size.
You might want to change -gravity and -background depending on your use case.

How to use convert -crop to add more background to animated gif/enlarge image?

I'm trying to crop an animated gif usind convert -crop. In some cases it's necessary to add more background to the image to fit. With other image formats it's done with
convert original.gif -rotate 0 -crop 1519x759-237-61\! -background white -flatten edited.gif
For gifs I tried
convert original.gif -coalesce -rotate 0 -crop 1519x759-237-61\! +repage edited.gif
convert is clipping/trimming the background and just the "subject" of the image is shown.
Example is here: https://imgur.com/ls1ED0Z
Result is here: https://imgur.com/59678cD
Expected Result is https://imgur.com/vZGaD7r
I added the red border to show how big the image is. Someone with a great solution? :)
Jan
To do what you want in Imagemagick, you should be using -extent rather than -crop in order to extend the size of the output.
Try this. Adjust the size and offset and background color as desired.
convert original.gif -coalesce -rotate 0 +repage -background white -extent 1519x759-237-61! -bordercolor red -border 3 result.gif
I have added a red border around the image.
With IM you can choose to "-crop", which is most often used to reduce the viewport dimensions, or "-extent", which can also be used to enlarge the viewport. It looks like there are a couple other issues with achieving the result in your sample image, but for starters, try simply substituting your "-crop" with "-extent" like this...
convert original.gif -coalesce -rotate 0 -extent 1519x759-237-61 +repage edited.gif
That will put your input image in a viewport of 1519x759, and place its upper left corner at 237 pixels from the left and 61 pixels from the top. You shouldn't need the exclamation point "!" in any case.

Crop part of an image and paste it into another with Imagemagick does not work

I have two images and I want to crop a part of the first one, and paste it into the other one in a specific place using imagemagick. Also the crop part would be gray
I want this: http://imgur.com/3PomJ9k
But I got this: http://imgur.com/5XmhytN
I have tried:
convert source.jpg ( +clone -crop 240x270+595+140 -resize 112x146 -type Grayscale ) -geometry +10+200 -composite destiny.jpg
but as you see, it does not works as expected.
Although the crop part is cloned as gray, the whole first image is cloned too, but I need to keep in background the second image in color and the crop part in gray in front of it.
Some ideas?
From our discussion in chat, I think you need something along these lines - you may need to fiddle with the actual numbers but hopefully the concept is clear:
convert other.jpg \( source.jpg -crop 240x270+595+140 -colorspace gray -resize 116x150! \) -geometry +4+108 -composite result.png

How to rotate an image without changing its size?

When an image is rotated by convert -rotate command the image size is enlarged. Is there a way to rotate around the center and to keep the image size, cropping the edges?
convert image.jpg -distort SRT -45 rotate_cropped_image.png
See http://www.imagemagick.org/Usage/warping/#animations
Example:
See also help on -distort: http://www.imagemagick.org/script/command-line-options.php?#distort
This seems now to simply "just work" -- for counter-clockwise 90 degrees:
$ convert image.jpg -rotate -90 rotated_ccw.jpg
If you know the size of the image the following works:
convert -rotate 45 -gravity center -crop NxN input output
tested with square images.
there may be a way to specify NxN is the input image size.
I've found this answer on Imagemagick forum:
A simple solution without knowing what the original size of the image
was, is to use the Alpha Composite Operator 'Src' as a 'crop to this
image size' type of operation. See:
http://www.cit.gu.edu.au/~anthony/graphics/imagick6/compose/#src
For example (ImageMagick version 6 only):
convert image.jpg \( +clone -background black -rotate -45 \) \
-gravity center -compose Src -composite rotate_cropped_image.png

Add lines of transparent pixels to image using command line

I have a few 15x15 PNGs that I need to convert to 18x18 PNGs.
However, I don't want to simply scale the entire image up.
What I want is basically keep the exact old image, but "pad" it with lines of invisible/transparent pixels.
So let's say the original image looked like this (imagine the Xs are pixels):
XXXX
XXXX
XXXX
XXXX
I want to turn it into something like this (X being pixels of original image, T being new transparent pixels):
XXXXT
XXXXT
XXXXT
XXXXT
TTTTT
Is this possible using command-line tools (so that this can be automated), perhaps with something like imagemagick?
This will do the job:
convert input_file -background transparent -extent '18x18' output_file
You can do that with ImageMagick's -append and +append operators. -append appends to the vertically (bottom), +append appends horizontally (right):
convert \
-background transparent \
15x15.png \
null: null: null: +append \
null: null: null: -append \
18x18.png
The null: picture is an ImageMagick built-in: it represents a 1 transparent pixel.
But as usually is the case with IM: there are a thousand ways to reach the same goal.
Should you ever need to add the transparent parts to the left and top (instead of the right and bottom), or should you want to add the pixels to just one, or to three or to four (instead of only two edges, it should be obvious to you how to modify my command.
Maybe you find Theodros' answer more intuitive (+1 on for it!). In this case, should you want to change the borders where you add your pixels, you can add the -gravity parameter to his command:
convert infile -background transparent -gravity southeast -extent '18x18' outfile
Other than south, you can also use north, northeast, east, ... and northwest. Not to forget center...

Resources