How to strip png images using ImageMagick? - imagemagick

convert -strip -interlace JPEG -quality 80 origi.jpg output-file.jpg
origi image size : 40.6 kb
output-file image size : 11.3 kb
But if I convert one png file as
convert -strip -interlace PNG -quality 80 he.png output-file.png
he.png image size : 711 kb
output-file image size : 1 mb
convert -strip -interlace PNG he.png output-file.png
he.png image size : 711 kb
output-file image size : 972.3 kb
Why the file size has been increased in case of PNG after conversion to output-file.png?
Am I using correct parameters for png conversions?

If you convert a regular PNG image into an interlaced one, the resulting filesize will probably be larger, as explained in the PNG specification (emphasis added):
Pass extraction ... splits a PNG image into a sequence of
reduced images (the interlaced PNG image) where the first image
defines a coarse view and subsequent images enhance this coarse view
until the last image completes the PNG image. This allows progressive
display of the interlaced PNG image by the decoder and allows images
to "fade in" when they are being displayed on-the-fly. On average,
interlacing slightly expands the datastream size, but it can give the
user a meaningful display much more rapidly.
The "-quality 80" option was not helpful because that forced ImageMagick to not use "filtering" to improve compression.

Related

Image magic convert command creates more than one file

I executed below command to convert a .tif file to a .jpg file. But for some tif images it generates 3 jpg files when only one file is expected. One is the expected jpg file, one is the same image in a black background and the other is just a white image.
magick convert /<.tif image name> -intent relative -resize 1500x1500> -quality 95 -colorspace sRGB -strip -auto-orient /<output .jpg image name>
Does anyone know the reason for this? what property of the input file causing this? or is there a issue with the command?
magick convert /<.tif image> -intent relative -resize 1500x1500> -quality 95 -colorspace sRGB -strip -auto-orient /<output .jpg image>
Expect this to give a single jpg image. But it gives 3 images for some input .tif images
Just adding some meat to #GeeMack's comment...
TIFF files often contain multiple images - or IFDs as referred to in the documentation. These can represent many things, but the most common are:
a low-resolution, flattened preview image followed by a full-resolution image aimed at providing quick previews
multiple pages of longer documents
colour separations for printing
the many channels of multi/hyper-spectral images
the layers of a multi-layer images, e.g. Photoshop editing layers
images and their associated masks, or classes/categories/classifications
... and so on.
A quick way to check what you have is with ImageMagick's identify command as that will produce a line for each image in the file, and you can often tell by the sizes, shapes and types of the layers which is a small preview and which is high resolution image, or that there are 242 channels of identical resolution images for a EO-1 hyperspectral imager.
magick identify IMAGE.TIF
Here's an example:
magick identify Prokudin-Gorskii.tif
Prokudin-Gorskii.tif[0] TIFF 3702x3205 3702x3205+0+0 16-bit sRGB 134.955MiB 0.060u 0:00.064
Prokudin-Gorskii.tif[1] TIFF 3702x3205 3702x3205+0+0 16-bit sRGB 134.955MiB 0.000u 0:00.001
Prokudin-Gorskii.tif[2] TIFF 625x175 625x175+841+814 16-bit sRGB 134.955MiB 0.000u 0:00.001
and you can see from the sizes that there are two full layers followed by a reduced size layer that is only annotation or markup on a small area of the image.
Another useful technique is to lay out all the images within a TIFF beside each other in a row across the page, with 10 pixel gaps between, using a command like this:
magick IMAGE.TIFF +smush 10 contents.jpg
We can now see that the three layers in the foregoing image correspond to a flattened version of all the layers on the left, followed by the two individual layers themselves in the centre and the reduced size yellow line overlay layer on the right.
If we then determine that it is only the first, flattened image we are interested in, we can extract and manipulate that alone by adding its sequence number in square brackets afterwards. So, to extract just the first flattened image:
magick IMAGE.TIF[0] extracted.tif
You can also extract multiple individual images and ranges, using commas and dashes.
Note also that magick convert is generally not what you want.
Note also that exiftool is lighter weight than a full ImageMagick installation and can also tell you what's in a multi-IFD TIFF.

How do I convert jpg image to tiff with 300DPI and reduce noise to 100% in Image magic?

I have been working on an image from last 25 days with zero output. So I came here in search of an answer.
I have a jpg image of 7MB. I upload it in Photoshop and changed the width to 96 Inch, Resolution for 300 pixels/inch, checked resample option and selected Preserve Details 2.0 and reduce noise to 100%. It gave me a 1.5 GB image as output.
Now I tried the same steps in image magic
gm convert -scale 768 -units PixelsPerInch -density 300x300 -resample 300x300 -noise 100% image.jpg -resize 768 image.tiff
Above command gave output in KBs. I need help.
If you want a 96inch wide image at 300dpi, you will need a width of 28,800 pixels, so start with:
gm convert input.jpg -resize 28800x result.jpg
That will resize the image to the correct width and do whatever is required with the height to preserve the image's aspect ratio - i.e. without distorting it.

how to convert scanned jpg files to pbm format losslessly?

Using ImageMagick's convert utility to convert some scanned jpg files to pbm files.
However, even if the option -quality 100 is used, the pbm's resolution still looks worse than the original scanned jpg file.
Worse, the scanned jpg file is a colored one, while the converted pbm is black and white.
Info of original jpg:
image size: 2256 × 1568 pixels
dpi: 300 pixels/inch
color model: RGB
info of the converted pbm:
image size: 2256 × 1568 pixels
dpi: 72 pixels/inch
color model: Gray
Currently, here is what I did to convert the format:
qiang#bonjour:~/scan$ convert scan000.jpg scan000.pbm
Am I missing any option to use with convert? As I mentioned earlier, -quality 100 had been tried, but to no avail.
Using ImageMagick, I think you want to output to PPM not PBM. Try
convert image.jpg image.ppm
or try the ascii version by using
convert image.jpg -compress none image.ppm
PBM is binary (black/white) and PGM is grayscale. If you want to keep color, then you need to use PPM.
Unfortunately, I believe that ImageMagick can only read DJVU format images. So you cannot write to it directly from ImageMagick.

Using Imagemagick to convert PDFs having rasters in them results in white backgrounds behind the sections covered by the rasters

I tried to use ImageMagick (v6.8.9-9 Q16) to convert a PDF containing a PNG file embedded in it to a PNG file.
The original PNG file had a transparent background. In the PDF too it appears fine. But in the PNG obtained after conversion, the area originally occupied by the PNG in the PDF has a white background. Please see the links for more clarity.
The command I ran is as follows:
convert -colorspace sRGB dice.pdf converted_dice.png
I also tried setting the -transparent white switch but it ends up taking out whites that were actually required in the final image.
Are there any extra switches or parameters to pass to convert in order to get rid of just this white background?
Kurt already explains the whole thing in great detail. So here is just how to assemble an image with ImageMagick after running it through pdfimages -png
pdfimages -png my.pdf my
This resluts in two files
identify my-0*png
my-000.png PNG 360x310 360x310+0+0 8-bit sRGB 256c 3.3KB 0.000u 0:00.000
my-001.png PNG 360x310 360x310+0+0 8-bit sRGB 256c 9.44KB 0.000u 0:00.000
my-001.png is the image labeled smask in pdfimages -list. To reassemble the image back to it's original form use -compose CopyOpacity with the ImageMagick command composite
composite -compose CopyOpacity my-001.png my-000.png my-reassembled.png
See also http://www.imagemagick.org/Usage/masking/#masks for more information.
Your approach to this task cannot work.
The command you used will convert the complete letter-sized PDF page (612 x 792 pt) into a PNG image.
However, the original size of the image embedded in the PDF page (612 x 792 pt) is 800 x 600 pixels. This can be seen by running pdfimages -list:
pdfimages -list dice.pdf
page num type width height color comp bpc enc interp object ID x-ppi y-ppi size ratio
----------------------------------------------------------------------------------------
1 0 image 800 600 rgb 3 8 image no 12 0 72 72 277K 20%
1 1 smask 800 600 gray 1 8 image no 12 0 72 72 50.1K 11%
So this is the first problem when converting the PDF page: it does not give your the correct size of the contained images.
The second, more fundamental problem however is: any image you get from converting a PDF page is the combination of all PDF objects overlayed on each other as they are from the page area. (Of course you could crop only part of the page -- but this gives you likewise the combination of all PDF objects from the cropped area...). The results of this you've encountered when you tried to convert all white pixels into transparent ones: since the originally different objects are merged into one representation of pixels, you can no longer discriminate between them as required.
You should take a different approach and use a different tool to extract the image: use pdfimages (the tool used above with the -list parameter to display image properties from the PDF's pages). As you can see, there are two images list: one is an RGB raster image, the other is a grayscale raster image, dubbed as type smask (softmask).
Here is a command to extract both images as PNG:
pdfimages -png dice.pdf dice-images
This will extract the two:
dice-images-0000.png (a color image)
dice-images-0001.png (a grayscale image)
(Note: Only very recent versions of pdfimages, the Poppler version, will let your extract the images as PNG. Within the PDF there is no such thing as PNG. There are only raster data, compressed with different methods. Older versions will only be able to extract images as PPM or PNM. This does not have any influence on what I describe below. Even if you extract PPM/PNM images, these two files can still be processed as described below...)
Below is a side-by-side, scaled-down montage of the two:
As you can see, the image itself does not have a transparent background, but a white one. (It does not have an Alpha channel.) Within the PDF format, these two images are used in combination to create transparent areas:
what appears completely black in the softmask (right) means: this pixel of the real image (left) is meant to be fully transparent.
what appears completely white in the softmask (right) means: this pixel of the real image (left) is meant to be fully opaque.
what appears in a shade of gray in the softmask (right) means: this pixel of the real image (left) is meant to be partially transparent (in line with its level of gray/black).
To combine these two files (color image and grayscale softmask) back into one PNG with transparency, you can employ ImageMagick now...

ImageMagick convert adds several extra "border" colors from tiff to jpeg?

I created an 8-bit .tiff image ("test.tiff") containing a grid of 30 different color patches in the RGB color space using ImageMagick -convert.
When I convert this image into a jpeg (which is what I need) using:
convert -quality 100 -colorspace RGB -depth 8 test.tiff test.jpg
The identify -verbose command reveals that the resulting jpeg has several additional colors in the color table, each only taking up a few (1-4) pixels and residing very near the desired colors in RGB space. My assumption is that some kind of border bleeding is happening; maybe due to compression?
I don't understand why this border bleeding has occurred, especially given that it does not occur when I convert the tiff image to either a bmp or pcx image.
Thank you
By definition, JPEG is a lossy compression. The effects your experiencing are expected with the JPEG format. Setting the -quality of 100 will not have a 1-to-1 image result as tiff.
See additional answers:
Should I use JPG or TIFF for high-quality prints?
[...] because every time [JPEG] would save it it would generate some changes.
Is Jpeg lossless when quality is set to 100?
At [quality] 100, you just get the LEAST loss possible.
I don't know how you created your 30 colour swatch, or how your histogram looks, but you might try adding -dither None and -colors 30 options to your convert commands:
convert test.tiff -dither None -colors 30 ...

Resources