Properly converting a CMYK image to RGB with RMagick

不想你离开。 提交于 2019-12-29 04:02:09

问题


I have been using the below to do a color conversion

    if @image.colorspace == Magick::CMYKColorspace
      # @image.colorspace #=> CMYKColorspace=12
      @image.colorspace = Magick::RGBColorspace
      @image = @image.negate
    end

It works, approximately, but the color luminosity is off. The fact that I need to negate the image leaves a very bad smell.

The documentation mentions using color_profiles, but beyond that I can not find much.

I am now trying

@image = @image.quantize(16777216, Magick::RGBColorspace)

And the colors are better, but still off.


回答1:


Thanks Pekka, you tipped me off to the answer (+1).

You must have ImageMagick compiled with the Little Color Management System (LCMS) installed. This may already be the case if an installer or package was used. But I was compiling from source. It was as simple as installing LCMS from source and rebuilding ImageMagick (./configure; make; make install).

In ImageMagick the below works well to reproduce accurate color:

convert FILENAME -profile /PATH_TO_PROFILE/sRGB.icm OUT.jpg

So in RMagick I use the below:

if @image.colorspace == Magick::CMYKColorspace
   # Adjust the path as necessary
   @image.color_profile ="/usr/local/share/ImageMagick-6.5.4/config/sRGB.icm"
end

@image.write("out.jpg") { self.quality = 85 }



回答2:


I've spent a long time trying to go from a CMYK EPS to a RGB PNG using RMagick and Rails. Hopefully this will be of use to someone:

def convert_image_from_cmyk_to_rgb( image )
  #puts image.alpha?
  if image.colorspace == Magick::CMYKColorspace
    image.strip!
    image.add_profile("#{Rails.root}/lib/USWebCoatedSWOP.icc")
    image.colorspace == Magick::SRGBColorspace
    image.add_profile("#{Rails.root}/lib/sRGB.icc")
  end
  image
end

You can download the ICC files direct from Adobe at http://www.adobe.com/support/downloads/iccprofiles/iccprofiles_win.html

The only thing I haven't been able to suss is how to maintain transparency. The EPS I want to use has a transparent background which is being turned into white. Unfortunately I can't do something like image.transparent( "white" ) as I have white in the image that I want to keep as white.

If I uncomment the puts image.alpha? in the above code it returns false.

Does anyone know if what I'm trying to do is possible with the current version of RMagick, as I'm beginning to wonder if importing CMYK EPSs with transparency isn't supported.

Thanks!




回答3:


The incoming files, in this case, do have a profile. I will investigate some more. I got lost with the color profiles (like where do I download them? the ICC site was not much help)

You are not the only one confused; I was too. There are discussions on the ImageMagick site that might be worth siftirng through: Here As far as I understood back then, properly working with profiles is possible when the profile used can be identified (e.g. a monitor profile) or is embedded in the file (which can be done at least for TIFF and JPG in Photoshop, I think). Check e.g. this: Here. Good luck.




回答4:


I found that The Who's command line solution worked beautifully, but the RMagick solution did not work for me.

To get it to work in RMagick, I instead I had to use the Magick::Image#add_format method, which, according to the docs, will allow you to specify an source and destination profile. It looks like this:

if img.colorspace == Magick::CMYKColorspace
  img.add_profile(RGB_COLOR_PROFILE)
end 



回答5:


RE: LCMS on Centos 5.5, be sure to download and build the latest LCMS from source (vs. yum install). Otherwise, IM won't find LCMS at build and you'll be scratching your head, like me, wondering why LCMS isn't included in IM delegate libs.



来源:https://stackoverflow.com/questions/1853359/properly-converting-a-cmyk-image-to-rgb-with-rmagick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!