Image magic convert command creates more than one file - imagemagick

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.

Related

Gray scale to text scan image to black/white image with higher resolution

convert 0101.jp2 -threshold 50% -type bilevel -monochrome -compress LZW ../0101.tiff
The resulting image looks jagged when I use the above command to convert a colored scanned text page to a black/white image (must be one bit per pixel). I want to make it of a higher resolution to look smoother. How can I use convert to do so?
Note that SO automatically converts tif image to jpg format so the output image shown below is not the same as the output image. You will need to run the convert command to get the true output image in tif.
If instead of thresholding you apply a strong contrast the gray pixels on the edge remain in a range of grays and the output is not jagged.
convert Original.jpg -sigmoidal-contrast 30 Corrected.jpg
(there are several ways to increase contrast in Magick)

preventing resized images from becoming blurred

im trying to resize image from 72x72 to 512x512 with following command
convert input.png -resize 512x512 output.png
but the output image (output.png) become blur
how to prevent resized images from becoming blurred
how to prevent resized images from becoming blurred
If you want a pixelated image of the original use -sample
# Create small image.
convert -size 72x72 plasma: 72x72.png
# Magnify the image with pixel subsampling.
convert 72x72.png -sample 512 512x512_sample.png
It's true that you can't restore missing data when upscaling images, but there's a lot of various algorithms to calculate what may be missing.
Try using the -filter option in addition to -resize, and checkout the wonderful usage examples here.
Probably the best you can do is use a sharper -filter such as catrom and then do post processing using -unsharp.
convert input.png -filter catrom -resize 512x512 -unsharp 0xSigma output.png
where sigma is the sharpening value, try sigma=1 or 2 (or as desired)
But it will not maintain the same quality as the input as others have mentioned above.
See -unsharp at http://www.imagemagick.org/script/command-line-options.php#unsharp
You can't. For an equal sharpness, you would need more data in the bigger image. Since you have only the data from a small image the result is blurred.
Look at it the other way, if what you asked was possible, instead of compressing the 512x512 image, we would first scale it down to 72x72, compress that (much smaller file) and sent it over with instructions to scale it up to 512x512.

Simple ImageMagick real world usable

I've read the ImageMagick documentation here and here and been unable to achieve a couple of simple tasks. Would appreciate any simple pointers or direction, or even commands I can execute that will work on Linux.
I want to convert any image-type (JPG, GIF, PNG, TIFF) to save to a PNG, losslessly, and as compressed as possible without any loss of quality. Ideally in 96 DPI so they look good in Retina screens.
To then take the above generated PNG and also resize it in specific sizes, with height and width specified.
What am I missing with the convert command?
If you want to convert a TIF, GIF or JPEG to PNG, the command is:
convert image.tif result.png
or
convert image.jpg result.png
In answer to your questions...
Question 1
PNG is lossless by definition, so that is not an issue. To get better compression of a PNG, you generally need to reduce the colours, i.e.
convert image.jpg -colors 64 result.png # or try 255 which allows a palettised image
The dpi is pretty irrelevant until you print on paper. More pixels are needed if you want more quality on screen.
Question 2
If you want to resize an image without destroying its aspect ratio, use
convert image.jpg -resize 200x100 result.png # retain aspect ratio
If you don't care if that makes the image look stretched or distorted, and you want exactly 200x100, tell ImageMagick that you really mean it by shouting:
convert image.jpg -resize 200x100! result.png # ignore aspect ratio

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