I've installed ImageMagick on AWS. The OS version is Linux RHEL 6.5.
The ImageMagick version is 6.9.1-Q8.
But whenever I execute this command then it always converts to 96 dpi:
./convert -units PixelsPerInch /DATA1/test.gif -density 72 /DATA1/test.72dpi.jpg
Original Image -
After I execute the command in Windows then it changes the DPI to 12 -
This command does not make any different with density. Please let me know what could be the issue.
You don't say what you expect your command to do, but I assume you want to resmple to the new dpi setting, in which case you'd need:
convert -units PixelsPerInch -size 100x100 xc:black -resample 96 a.gif
which makes the 100x100pixel input image into a 133x133 pixel image - because 96/72 = 1.33
The GIF format actually totally ignores/discards the dpi anyway, as you can see diff finds no difference between these two images - one at 51 PixelsPerInch and one at 53 PixelsPerCentimeter:
diff <(convert -units PixelsPerInch -density 51 xc:black GIF:-) <(convert -units PixelsPerCentimeter -density 53 xc:black GIF:-)
The article here explains the DPI rather well.
Related
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
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.
If the page parameter is set, conversion of PDF files at a given density outputs blank pages.
"convert -units PixelsPerInch -density 300 $myfiles -page A4 -gravity center test.pdf"
If I omit page parameter from the command, I get appropriate output but at 72dpi default resolution.
Any idea?
A4 page size is 595 x 842. So in ImageMagick you could try
convert -units PixelsPerInch -density 300 $myfiles +repage -resize 595x842 test.pdf
That will make an A4 pixel dimension image with 300 dpi. You could also do
convert -units PixelsPerInch -density 300 $myfiles +repage -resize 595x842 -density XX test.pdf
Where XX is the dpi you want when printing the image of that size.
I added +repage to remove any input image virtual canvas, since you did not specify what format images you are using for $myfiles. Without +repage, that could have caused a large bit of white space at the top of your result.
Note it is always best and most helpful to provide your ImageMagick version and platform when asking questions about its use.
While I was trying to tweak the command I found that a set density (i.e, density 300) with a given page parameter actually sets the density of the -page A4 but not the converted object on page as the set density can't actually determine resolution of the -page A4 to which it shall be applicable. As a result, the command returns blurry or blank image on set page.
However, extent parameter is what, which actually outputs the appropriate image as it is possible to set the page resolution with this parameter at a predefined density. The following example will make it absolutely clear.
Resolution of A4 size page at 300 dpi is 2480x3508, thus correct command for a set density like 300 dpi shall be:
"convert -units PixelsPerInch -density 300 $myfiles -gravity center -extent 2480x3508 test.pdf"
Resolution of A4 size page at 72 dpi is 595x842, thus correct command for a set density like 72 dpi shall be:
"convert -units PixelsPerInch -density 72 $myfiles -gravity center -extent 595x842 test.pdf"
I am converting various PDFs uploaded by end users into images using following command
-density 140 -limit memory 64MB -limit map 128MB [pdffile] page.png
Here is the result. On the right we have original PDF and on the left output image. As you can see the colors are quite noticeably different.
What could be causing this and how fix it?
try following command:
-density 140 -limit memory 64MB -limit map 128MB -colorspace RGB [pdffile] page.png
Edit: I later discovered that ImageMagick can do it fine, I just needed to use -colorspace sRGB
My final command was:
convert -density 560 -limit memory 64MB -limit map 128MB \
-colorspace sRGB [pdffile] -scale 25% page.png
The oversampling and scaling down was to counter the poor anti-aliasing mentioned below.
Before I discovered that, here was my earlier solution...
In my case the colors produced by ImageMagick's convert were oversaturated, quite like those in the question. I was trying to convert this file using IM 6.7.7.10-6ubuntu3.
-resample 100 made no difference.
-colorspace RGB seemed to produce more accurate saturations, but the entire image was darker than it should have been.
Curiously, this suggestion to use GhostScript instead of ImageMagick for the conversion, produced very close to the correct colors:
gs -q -sDEVICE=png16m -dSubsetFonts=true -dEmbedAllFonts=true \
-sOutputFile=page.png -r200 -dBATCH -dNOPAUSE [pdffile]
(The original suggestion passed the -dUseCIEColor option, but in my case this appeared to reduce the gamma: light pixels were fine, but the dark pixels were too dark, so I removed it.)
After that, the only thing that bothered me was that the anti-aliasing/edges were a little off in places (especially visible on curves passing 45 degrees). To improve that, I created the output at four times the required resolution, and then scaled down afterwards, rendering those errors almost imperceptible. Note that I had to use ImageMagick's -scale for this, and not -geometry or -resize, in order to avoid bicubic ringing effects.
Use the -resample option:
-density 140 -resample 100 -limit memory 64MB -limit map 128MB [pdffile] page.png
Open Source MuPDF util mutool retains color and size using default parameters below
you need though to list the pages separated by a comma at the end of the command.
mutool draw -o draw%d.png abook.pdf 1,2
Otherwise if using Linux try Windows for better colorspace RGB interpretation when using imagemagick's convert.
The following images show how anti-aliasing improves if you sample at a higher resolution and then scale down.
Although 1120 was slightly better quality than 560, it took a long time to convert, so I would probably choose 560 for a good time:quality trade-off.
-colorspace sRGB -density 140
-colorspace sRGB -density 280 -scale 50%
-colorspace sRGB -density 420 -scale 33.3333%
-colorspace sRGB -density 560 -scale 25%
-colorspace sRGB -density 1120 -scale 12.5%
(It is easier to see the difference if you download the last two images and flip between them in your favourite image viewer. Or scroll up this list of images, instead of down. You should seem them becoming progressively uglier.)
When I convert PDF to JPG using below command
#] convert -geometry 1024x768 -density 200 -colorspace RGB /opt/test.pdf +adjoin /tmp/check/test_%0d.jpg
I'm getting the output as
test_0.jpg
test_1.jpg
test_2.jpg
how can I have the sequence to be as below:-
test_1.jpg
test_2.jpg
test_3.jpg
If you're using ImageMagick 6.2 or greater you can use the 'scene' parameter to start at a particular number.. so something like:
convert -geometry 1024x768 -density 200 -colorspace RGB /opt/test.pdf +adjoin -scene 1 /tmp/check/test_%0d.jpg
should output the file sequence you want starting at 1.
+adjoin -scene 342 image_%03d.gif
would output image_342.gif, image_343.gif etc
Hope that helps