Why Graphicsmagic produces thumbnails much larger in file size that Imagemagick? - imagemagick

input.jpg is 3264×2448 px, 1.78 MB.
I have created two thumbnails with identical arguments, in imagemagick 7.0.8-11:
convert input.jpg -quality 50 -thumbnail 200x200 thumb_im.jpg
and graphicsmagick 1.3.30:
gm convert input.jpg -quality 50 -thumbnail 200x200 thumb_gm.jpg
The results are: imagemagick — 4.31 kB, graphicsmagick — 27.8 kB. Visually these images are similar in quality but not identical.
What am I missing?

Related

Imagemagick conversion process

I was trying to resize a pdf file to 8.5x11 with ImageMagick and in doing so, I am loosing the quality of my pdf file.
I was using convert source.pdf -extent 612x792 destination.pdf command.
Can someone please help me with this one?
Imagemagick is rasterizing the PDF, then scaling it, then producing an output PDF of differing dimensions, which contains raster data only.
Scale a PDF to fit directly like this:
cpdf -scale-to-fit "612 792" in.pdf -o out.pdf
This keeps the PDF's original vector artwork and fonts intact.
Imagemagick uses Ghostscript and will rasterize your PDF. It will not maintain its vectors. If you want higher quality, use a higher -density to rasterize and then resize back to whatever you want. For example, I use a density 4x the default of 72 or 288 and then resize by 25%=1/4 to get back to the nominal size.
convert -density 288 -units pixelsperinch image.pdf -resize 25% result.pdf
You can then include your -density 72 -gravity XX -extent 612x792 before the output.
Or you can compute do -density 288 etc and then just resize to 612x792.
I've tried this and it worked.
convert -density 300 -define pdf:fit-page=Letter -gravity Center source.pdf output.pdf

Convert RAW to PNG with Imagemagick

I'm trying to convrt RAW image to PNG with Imagemagick.
Imagemagick version I'm using is 6.7.8-9.
The RAW image want to convert is:
https://drive.google.com/file/d/1V1c-ytjkLaCM3KbAc6Yxj2nfWzNkYohc/view?usp=sharing
My client gave me a big RAW image which contains more than 1000 RAW images and it is generated from Dicom. Firstly, I cropped just one image with the command (crop command did not work somehow, so used convert):
convert -size 512x512 -depth 16 UYVY:original.raw result.raw
result.raw appears good on ImageJ.
Now, I have no idea how to get PNG from it.
I tested some commands:
This one generated a very bad quality:
convert -size 512x512 -depth 16 gray:result.raw result.png
This one gets green-ish image:
convert -size 512x512 -depth 16 uyvy:result.raw result.png
If I open result.raw on ImageJ and save as PNG, it works perfectly.
I got an answer from Imagemagick community. Firstly, below code to get the first image from the original raw was wrong.
convert -size 512x512 -depth 16 UYVY:original.raw result.raw
This is the correct way (512*512*2bytes=524228)
head --bytes 524288 original.raw >firstimage.raw
Then convert to PNG:
magick -size 512x512 -depth 16 gray:firstimage.raw -evaluate AddModulus 50% -auto-level x.png
Here is another way in ImageMagick that I think is more intuitive to your signed raw data. I simply specify the data is signed using -define quantum-format=signed. Then stretch the result to full dynamic range using -auto-level.
convert -size 512x512 -depth 16 -define quantum:format=signed gray:original.raw -auto-level result.png
If using ImageMagick 7, then replace convert with magick.

Applying watermark on an image

I'm trying to apply watermark on an image using the following imagemagick command
convert input.png watermark.png.png -gravity northwest -composite output.png
The input png file size is 16KB and the watermark file size is 900bytes, but when I executed the above command to apply a watermark, the output png size is 61KB which is almost 4X the size of the original input png file. Is there any better way of applying a watermark to an image file with much better result in terms of output filesize
Test Image: https://res.cloudinary.com/deks86ilr/image/upload/v1533015495/1_rnpbye.png
Test watermark: https://res.cloudinary.com/deks86ilr/image/upload/v1533015494/2_usmonh.png
Here are my results of processing of your images with my PNG8 output using ImageMagick 6.9.10.8 Q16 with libpng 1.6.34.
I note that your input image was type palette, which means it is 8-bits of color per pixel and not 24-bit color. So it is already a low quality image.
Input (~16 KB):
Watermark Image (white on transparency -- so it is invisible here):
Convert to 24-bit PNG:
convert input.png watermark.png -gravity northwest -compose over -composite input_with_watermark.png
I see no significant visible quality loss but the output is now increase from 16 KB to 60 KB. But you can use tools such as pngcrush to compress it further.
Convert to 8-bit PNG:
convert input.png watermark.png -gravity northwest -compose over -composite PNG8:input_with_watermark2.png
The file size is now back to about 16 KB. But as you note the quality is a little poorer. This is likely because the input image (at 8-bits and has 217 colors) was first read back to 24-bits, then watermarked, which included new shades of white and then quantized back to 8-bits, but contains only 84 colors colors.
Another way is to add +dither -colors 256 to the command (the +dither turns off dithering):
convert 1_rnpbye.png 2_usmonh.png -gravity northwest -compose over -composite +dither -colors 256 PNG8:watermark3.png
This is a bit better, since it now uses 189 colors and still has a file size of 16 KB.
One final method is to save the colors from your input to a colortable image. Then use -remap to recolor the output using that colortable:
convert 1_rnpbye.png -unique-colors colortable.gif
convert 1_rnpbye.png 2_usmonh.png -gravity northwest -compose over -composite +dither -remap colortable.gif PNG8:watermark4.png
This results in 8-bit output with 227 colors and still a file size of about 16 KB. So it has a few more colors than your input and visually looks about the same quality as your input.
If you cannot reproduce these results, then perhaps you should upgrade either or both ImageMagick and libpng.

Add overlap or duplicate pixels on texture bounds after cropping in imagemagick

I have huge texture Nx1024 pixels. I split it into several 1024x1024 textures with the following command with Imagemagick:
magick image.png -quality 100 -background none -resize x1024 -crop 1024x1024 -extent 1024x1024 output%02d.png
How can I add 2 pixels overlap on left and right sides of output images or duplicate pixels with imagemagick?
PS:
I found the following case (-crop 3x1+2#) crop with equal size but it doesn't suit because I have an unknown width of the input image (it could be multiple of N for 1024)
If on Unix-based OS, then you could try my script, overlapcrop at http://www.fmwconcepts.com/imagemagick/index.php

Cropping with ImageMagick always results in 1x1 images

I'm trying to crop an image into a bunch of 256x256 tiles (with the right-most and bottom-most images less than 256 pixels due to the remainders), but ImageMagick always generates images that are 1x1.
I use this command (Windows 7 command prompt):
convert WBS.png -crop 256x256 +repage +adjoin output\WBS_%02d.jpg
After cropping the following message is displayed:
convert: geometry does not contain image `WBS.png' # warning/transform.c/CropImage/589.
After cropping, the output folder contains 1634 jpg files, all of which are 1 x 1 pixel. The source image is 7218x7650.
Suggestions? I'm sure I'm making some blatant mistake, but I don't know what it is.
This can happen if the origin of the image is not at 0,0. In that case, using +repage before processing the image should do the trick, i.e.
convert WBS.png +repage -crop 256x256 +repage +adjoin output\WBS_%02d.jpg
See also the documentation of the -crop option.

Resources