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.
Related
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.
we run a mogrify batch job like mogrify -path "WEBREADY\DONE" -layers merge -trim +repage -resize "1200x1200>" -define jpeg:extent=500kb -format jpg *.*
This works fine for images with a white background. But recently we saw a 2% error rate and discovered the error. Mogrify trims as expected only on the web the images is surrounded by white so this looks quite silly with grey-ish in corners.
THe solution would be to either
Detect the non-white background. And in that case -only- do not trim
Or to detect the background color (it is JPG) in top left corner and forst set this to an all white background.
Can this be done in my 1 command above for the commandline? (so find the grey background first and change it to white)
You can get the colour of the top-left pixel like this:
convert shirt.jpg -crop 1x1+0+0 txt:
# ImageMagick pixel enumeration: 1,1,65535,srgb
0,0: (57311,57311,57311) #DFDFDF srgb(223,223,223)
So, it is #DFDFDF.
If you now try and replace that shade with yellow (I know you want white but you can't see that on here):
convert shirt.jpg -fill yellow -opaque "#DFDFDF" result.png
and you can now see the problem. The background is not uniform and its colour also appears in your shirt. If you add some fuzz, it makes it more pronounced:
convert shirt.jpg -fill yellow -fuzz 10% -opaque "#DFDFDF" result.png
Even if you try flood filling from the top-left, you still have to hope that there are no pixels in the edges of your shirt that are similar to that corner:
convert shirt.jpg -fill yellow -fuzz 5% -floodfill +0+0 "#dfdfdf" result.png
I need to take a normal image:
and add a white, transparent overlay so it looks something like this:
Don't pay attention to the text on the converted image or the fact that it's a cropped version of the original. I need to simply convert the top to the exact same image, just with this white, transparent overlay. I also need it to be a cli command.
Updated Answer
This is even easier
convert house.jpg -fill white -colorize 80% result.png
Original Answer
Something like this maybe...
convert house.jpg \( +clone -fill white -colorize 80% \) -composite result.png
I have three related images - a base image, a glow image and a "dirty" image. Initially all have the same dimensions and are correctly positioned when I overlay them.
I'm using Image Magick via a shell script to trim the transparent edges off the images. When I do a simple trim on all the images, it upsets the alignment of the images relative to each other.
I'm looking for a way using bash / ImageMagick to trim / crop the images as much as possible while keeping the relative positions intact. Or in other words, trim the glow image, and then apply the same cropping to the other two images.
Any help would be greatly appreciated.
You probably need to try with -crop operator instead of -trim.
-crop simply cut off all images given size and position specified.
Ok, got it. The trick is to get ImageMagick to run the trim operation of the largest image (the glow in my case), but instead of outputting an image, simply output the trim parameters instead.
Then, feed this back into a crops operation on each image, like this:
convert Penguin.png -crop `convert PenguinGlow.png -trim -format '%wx%h%O' info:` +repage PenguinTrimmed.png
convert PenguinDirt.png -crop `convert PenguinGlow.png -trim -format '%wx%h%O' info:` +repage PenguinDirtTrimmed.png
convert PenguinGlow.png -crop `convert PenguinGlow.png -trim -format '%wx%h%O' info:` +repage PenguinGlow.png
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