Railsana
Railsana

Reputation: 1852

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?

Upvotes: 0

Views: 413

Answers (2)

fmw42
fmw42

Reputation: 53164

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.

Upvotes: 0

Railsana
Railsana

Reputation: 1852

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

Upvotes: 0

Related Questions