crop gif image using libMagick - image

i am trying to crop a gif image using the libMagick.so library.
./convert --version
Version: ImageMagick 6.2.8 03/31/08 Q16 file:/usr/share/ImageMagick-6.2.8/doc/index.html
using the following command:
convert img.gif -crop 91x68+6+116 out.gif
the out.gif image gets crop, but not re sized. i get the same image size while all of it is transparent except the 91 pixels starting at 6 pixels from the left point and 68 pixels starting 116 pixels from the top.
how can i make the convert command crop and leave only the cropped part as the output image.
by the way, when my output image is jpg, i get the expected results.

You need to add the -repage option for GIF files:
convert img.gif -crop 91x68+6+116 -repage 0x0 out.gif
The problem is that GIFs can contain multiple images (of different sizes) when they're animated so cropping just affects one of the images while leaving the overall GIF canvas size untouched. Using 0x0 for the canvas size is an easy shortcut to make ImageMagick figure out how big the image should be:
A given a canvas size of zero such as '0x0' forces it to recalculate the canvas size so the image (at its current offset) will appear completely on that canvas.

Related

Image with transparent background in Flutter

I have image of object with transparent background for my Flutter app.
But Flutter shows this image with transparent background, as it is.
How do i hide transparent background of the image in Flutter?
The image you are using is not a proper transparent PNG file. It's a JPEG. So, please use a proper transparent PNG file. Here are some difference between a JPEG and PNG:
Both support true color or a palette of 16 million colors, PNG also
supports 256 color and monochrome images.
JPEG uses a lossy algorithm, PNG uses the ubiquitous lossless
algorithm which we all know as ZIP.
PNG supports alpha as well as single color transparency. JPEGS are
opaque.
Compression ratio of images can be upto 50x for a JPEG but maybe at
most 4:1 in PNGs for most images with many colors

No-crop canvas increase to 2:1 aspect ratio with ImageMagick

I am in a situation where I have a set of images with variable width and height and I need to ensure that those images comply with an aspect ratio of 2:1.
The only assumption I can make is that those images have an aspect ratio greater than 2 so I need to expand the canvas.
With ImageMagick I can do this:
magick input.jpg -background black -gravity north -extent 2:1 output.jpg
But this crops instead of adding canvas as desired. Is there an option to avoid cropping, i.e. force image padding? Remember I do not know the image resolution beforehand.
Of course with a more complex script I can extract the image dimensions and compute the desired resolution but I am trying to keep things simple if possible.
P.S.: The images comes from a laser scanner and are going to be used in a 360 panorama viewer hence the need to meet an aspect ration without cropping.
With ImageMagick v7 you can do some inline calculations that will adjust the results of the -extent operation to pad any size input to a 2:1 ratio. See this command as an example...
magick input.png -gravity center -extent "%[fx:max(w,h*2)]x%[fx:max(w/2,h)]" result.png
That will pad any extra space with black. Add -background <somecolor> before the extent operation to change the padding color.

How to center images in gif using convert from imagamagick?

My goal is to create gif image from jpg which I will rotate on angle A with delay D imagemagick. So, i've got this code:
for (( i=0; i < 360; i+=$a )) do
name=%(printf "%03d" $i)
convert $image -rotate $i "temp/$name.jpg"
done
convert -delay $d "temp/*.jpg" out.gif
The problem is that images in out.gif is not centered. How can i center them?
(image should be rotated relative to the center)
The problem is that images in out.gif is not centered.
It just seems that way when you look at the final gif. However, the image already rotates around its center. Example: I have the image 1.png and rotate it in steps of 15°.
convert 1.png -rotate 15 2.png
convert 1.png -rotate 30 3.png
convert 1.png -rotate 45 4.png
Now let's have a look at the images 1-4.
As we can see, the image rotates around its center, but the image dimension changes.
When we convert those four images into one gif animation, we have to set a fixed dimension for the whole animation and a position of each frame on the canvas. Without giving further information, ImageMagick uses the first frame's dimension as the dimension of the animation and places each frame at the top left. Since the first frame (rotation 0°) is the smallest, the other frames get cropped. The following illustration shows what will get cropped.
To solve the problem, we have two options:
When creating the animation, center each frame.
When rotating, crop the result such that all frames have the same size.
I would go with 2. ImageMagick has the operator -distort SRT <ANGLE> which rotates and crops in one step. Using this operator will also look smoother in animations, see here. Replace your -rotate $i with -distort SRT $i.
The ImageMagick "-rotate" operator will increase the output dimensions so the entire input image still fits within the output. That produces results with various dimensions for different angles of rotation. Using the plus form of "+distort SRT" will have the same effect.
And "-distort SRT" will rotate the image on center inside its input dimensions, but that will clip off the corners as they rotate outside the original frame.
There is a way to rotate an image several times, pivoting on the center point, and in a viewport that accommodates the entire image even when rotated.
By setting the input image name "$image", the number of frames "$f", and the delay per frame "$d", a command like this can do everything your "for" loop is doing while eliminating the writing and reading of temporary files. It looks more complicated than it really is...
image=input.jpg
f=20
d=50
convert -delay $d -dispose previous "$image" -background none -virtual-pixel none \
-duplicate $f +delete -set option:distort:viewport "%[fx:hypot(w,h)]x%[fx:hypot(w,h)]" \
-distort SRT "%[fx:w/2],%[fx:h/2] 1 %[fx:360/n*t] %[fx:hypot(w,h)/2],%[fx:hypot(w,h)/2]" \
out.gif
That command will read the input "$image", duplicate it to create a total of "$f" frames, set the delay to "$d", rotate each image "360/$f" degrees more than the previous, and output the final result as an animated GIF.

Drawing a 20x20 pixel greyscale image

I'm currently trying to write a program that takes a user-inputted 20x20 pixel greyscale image of a digit and predicts what number the user drew. Here is a sample image of what a typical user input would be:
How can I ask the user to draw such an image?
Maybe shell out to ImageMagick to create a 400x400 greyscale image:
convert -size 400x400 xc:gray image.bmp
and then start MS Paint (Windows) or GIMP (Linux/macOS) to edit the image. Then, when the user exits, resize the image to 20x20 and save as a PGM which is dead easy to read and necessarily greyscale:
convert image.bmp -resize 20x20 result.pgm

Resize without whitespace surrounding on images using graphicsmagick or imagemagick

I'm using graphicsmagick to resize an image to a thumbnail, but it's adding a white surrounding border padding.
The command I'm using is:
gm convert matrix.jpg -resize "80x80>" -gravity center -extent 80x80 thumbnail.jpeg
As you can see, there is some white padding around the image, but I don't want this. Ideally I'd like (the entire image not just a crop of it) to fill the desired 80x80 output size.
How can this be achieved in either imagemagick or graphicsmagick?
I used ImageMagick with this image. This solution requires to know the size of the input image.
Input
The image has 145 pixels horizontally and 200 pixels vertically.
Crop
Crop from the top of the image:
convert -crop 145x145+0+0 -resize 80x80 matrix.jpg thumbnail.jpeg
I used 145x145 in order to extract a square from the original image. +0+0 is the offset of the extracted square, hereby the top left.
Crop with the center of the image:
convert -crop 145x145+0+27 -resize 80x80 matrix.jpg thumbnail.jpeg
The vertical offset is set to 27 because we have to remove 55 (200 - 145) pixels on top or bottom, so we need to remove 27 (55 ÷ 2) pixels on the top and 28 pixels on the bottom.
Crop with the bottom of the image:
convert -crop 145x145+0+55 -resize 80x80 matrix.jpg thumbnail.jpeg
Resizing without crop
convert -resize 80x80\! matrix.jpg thumbnail.jpeg
The ! flag (escaped with \! as suggested in the documentation) after the resize parameters forces ImageMagick to ignore the aspect ratio.
If you want to keep the original aspect ratio, without image distortion, you can use the ImageMagick -trim option to get rid of the white padding:
convert "matrix.jpg" -resize "80x80" -gravity center -extent 80x80
-trim "thumbnail.jpg"
This will produce a 58x80 uncropped image with the same aspect ratio as the original. It is 58x80 because ImageMagick uses the larger dimension of the original to compute the scale factor (in this case 80/200) and scales the smaller dimension by that same factor to preserve aspect ratio.
If you want an uncropped image of exactly 80x80 pixels, this is a different aspect ratio than the original. The output image will have distortion, and #AL's resizing without crop option will work.
convert "matrix.jpg" -resize "80x80!" -gravity center -extent 80x80
"thumbnail.jpg"
Tested in Windows 7 with ImageMagick 6.8.9. #AL syntax is probably Linux.

Resources