convert each pdf page to jpg - imagemagick

i wanna convert each page of a pdf to a jpg file. Converting a single page works:
exec('/usr/local/bin/convert -density 288 -resize 50% -quality 85 - colorspace CMYK dv.pdf[8] -colorspace RGB test.jpg');
Converting all pages does not work:
exec('/usr/local/bin/convert -density 288 -resize 50% -quality 85 - colorspace CMYK dv.pdf -colorspace RGB test.jpg');
What could be wrong?

you could use imagick to do this. More information can be found here at http://php.net/manual/en/imagick.setup.php
<?php
$imagick = new Imagick();
$imagick->readImage('myfile.pdf');
$imagick->writeImages('converted.jpg', false);
?>

I could not solve the problem, but i found an alternative solution - im executing Ghostscript without using Imagick:
exec(
"'gs' '-dNOPAUSE' '-sDEVICE=jpeg' '-dUseCIEColor' '-dTextAlphaBits=4'
'-dGraphicsAlphaBits=4' '-o$exportPath' '-r$res' '-dJPEGQ=$quality' '$input'",
$output
);

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.

Dragonfly convert not working with ImageMagick's profile option

I am trying to generate an RGB thumbnail for a CMYK pdf file using the Dragonfly gem. It is working fine with this code:
file.image.convert("-flatten -density 300 -quality 100", 'format' => 'jpg', 'frame' => 0).url
The result is the correct url of the thumbnail image.
Since some users upload CMYK documents, I want to convert them using color profiles and the colorspace option:
file.image.convert("-flatten -profile /path_to/USWebCoatedSWOP.icc -profile /path_to/AppleRGB.icc -colorspace rgb -density 300 -quality 100", 'format' => 'jpg', 'frame' => 0).url
The result is always "nil". No error is shown.
When I execute the code (which is shown in the console) manually in the terminal, the file is correctly converted. The "DRAGONFLY: shell command":
convert /path_to/my_cmyk_file.pdf[0] -flatten -profile /path_to/USWebCoatedSWOP.icc -profile /path_to/AppleRGB.icc -colorspace RGB -density 300 -quality 100 /path_to/my_rgb_thumbnail.jpg
The result is this:
/path_to/USWebCoatedSWOP.icc ICC 1x1 1x1+0+0 16-bit sRGB 557168B 0.010u 0:00.000
/path_to/AppleRGB.icc ICC 1x1 1x1+0+0 16-bit sRGB 552B 0.000u 0:00.000
/path_to/my_cmyk_file.pdf[0]=>/path_to/my_rgb_thumbnail.jpg PDF 420x595 420x595+0+0 16-bit sRGB 56625B 0.180u 0:00.190
What could cause the problem within Dragonfly?
I managed to get it to work with a custom processor and the shell_update functionality:
processor :cmyk_pdf_thumb do |content|
content.shell_update ext: 'jpg' do |old_path, new_path|
"convert -density 300 #{old_path}[0] -flatten -profile USWebCoatedSWOP.icc -profile AppleRGB.icc #{new_path}"
end
end
Now I can convert CMYK documents by file.image.cmyk_pdf_thumb. Even if I set the ext option to "jpg", I have to encode the result to get a JPEG file:
file.image.cmyk_pdf_thumb.encode('jpg').url
I do not know Dragonfly. But you should not use both -colorspace and profiles to convert from CMYK to RGB. Use one or the other. The better choice is to use profiles. You should also set the density before reading the PDF file for better quality, unless you are trying to set the density of the jpg. If so, you should include -units pixelsperinch. Your ImageMagick command should be
convert -density 300 /path_to/my_cmyk_file.pdf[0] -flatten -profile /path_to/USWebCoatedSWOP.icc -profile /path_to/AppleRGB.icc -quality 100 /path_to/my_rgb_thumbnail.jpg
or
convert /path_to/my_cmyk_file.pdf[0] -flatten -profile /path_to/USWebCoatedSWOP.icc -profile /path_to/AppleRGB.icc -density 300 -units pixelperinch -quality 100 /path_to/my_rgb_thumbnail.jpg
If the CMYK pdf already has a profile, then the first profile is not needed and should not be included. You may also not want -quality 100 as that will make a larger file. ImageMagick uses -quality 92 as its default.

ImageMagick jpeg quality extremely low

I have the following image 188_f.jpg:
And the following code:
<?php
$ss = "convert 188_f.jpg -quality 95% -set colorspace RGB -colorspace CMYK 188_f_cmyk.jpg 2>&1";
echo ">" . $ss . "<br />";
echo system($ss);
?>
When I run this code, I get this image as output:
Which is indeed in CMYK, but when viewed at 100% there is a significant loss in quality. (Wasn't sure if SE did any conversions when uploading, so I uploaded the above images via FTP so you can examine them directly if needed.)
I tried moving the quality argument around, with and without the percent sign, and I also looked through the Imagick guide, but I couldn't find any explanation for why the quality goes down. The output file has the same number of pixels.
EDIT: Making no other changes to the script except changing the colorspace options, here were the results:
EDIT 2: Also tried multiple different profiles, using this:
convert 188_f.jpg -quality 95% -profile profiles\\UncoatedFOGRA29.icc -colorspace CMYK 188_f_cmyk.jpg 2>&
None of them got rid of the pixelatedness.
I managed to get an output indistinguishable from your original image with the following options passed either to convert or mogrify:
-filter Triangle -define filter:support=2 -unsharp 0.25x0.25+8+0.065 -dither None -posterize 136 -quality 82 -define jpeg:fancy-upsampling=off -interlace none -colorspace sRGB -strip
(ref: https://www.smashingmagazine.com/2015/06/efficient-image-resizing-with-imagemagick/)
(left: original - 74.8kB, right: converted - 63kB)

Convert RGB to Grayscale in ImageMagick command-line

How do I convert a RGB image (3 channels) to a grayscale one, using the (r+g+b)/3 method?
I look through an examples page: http://www.imagemagick.org/Usage/color_mods/#grayscale
but the desired method:
convert test.png -fx '(r+g+b)/3' gray_fx_average.png
gave me a wrong result - the resulted image has still 3 channels.
You can check this by running a command: identify -format "%[colorspace] <== %f\n" *.png.
convert <img_in> -set colorspace Gray -separate -average <img_out> gives the best result for any image for me.
Using the (r+g+b)/3 method will apply the effects of grayscale, but the image will remain in sRGB (which is the expected behavior for this method). You'll need to specify the desired colorspace along with the -fx command.
convert test.png -fx '(r+g+b)/3' -colorspace Gray gray_fx_average.png
Verify with identify -format "%[colorspace] <== %f\n" gray_fx_average.png
Gray <== gray_fx_average.png
To batch convert images in Fish shell:
for file in *.jpg; convert -colorspace Gray $file $file; end;
A few ways to that in Imagemagick command line are:
convert test.png -grayscale average gray_average.png
or
convert test.png -colorspace OHTA -channel r -separate +channel gray_average.png
or
convert test.png -intensity average -colorspace gray gray_average.png
or
convert test.png -colorspace HSI -channel blue -separate +channel gray_average.png
See
https://imagemagick.org/script/command-line-options.php#grayscale
https://imagemagick.org/script/command-line-options.php#intensity
https://imagemagick.org/script/command-line-options.php#colorspace
Seems like you are taking the red channel to do that, on
convert test.png -colorspace OHTA -channel r -separate +channel gray_average.png
i prefer the green channel (i heard that way works on tv sice ancient days, maybe the best)
I use convert mostly to convert colour pictures of documents into grey-scale pdf documents in order to perform OCR. My best results are using Rec709Luminance. So I recommend
convert colourpicture.png -grayscale Rec709Luminance greyscalepicture.png
Short command, nice outputs.
I use this with good result for gray-scale images (I convert from PNG):
ls ./*.png | xargs -L1 -I {} convert {} -strip -interlace JPEG -sampling-factor 4:2:0 -gaussian-blur 0.05 -colorspace Gray -quality 20 {}.jpg
I use this for scanned B&W pages get them to gray-scale images (the extra arguments cleans shadows from previous pages):
ls ./*.png | xargs -L1 -I {} convert {} -strip -interlace JPEG -sampling-factor 4:2:0 -gaussian-blur 0.05 -colorspace Gray -quality 20 -density 300 -fill white -fuzz 40% +opaque "#000000" -density 300 {}.jpg
I had an issue to convert an sRGB colorspace to a Gray colorspace. I had to delete Alpha channel manually before a conversion. In other case, the image will stay sRGB.
convert image_original.tga -alpha off -set colorspace Gray image_converted.tga

Resources