Properly converting a CMYK image to RGB with RMagick - ruby

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.

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 }

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!

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.

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

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.

Related

Issue generating grayscale gradients using RMagick

All, have been using RMagick/ImageMagick for years with great success. One of the tasks I use it for is on-demand gradient generation (pending full support for CSS gradients, oh god I can't wait).
Recently noticed a bug/behavior that I can't fathom at all. Have been trying to even come up with a theory, thought the bright minds of StackOverflow might be able to help.
In short form, generating gradients as PNGs works fine, unless you're using grayscale values, in which case the output is... strange. Try the below script.
require 'RMagick'
include Magick
puts "ImageMagick version: " + Magick_version
puts "RMagick version: " + Version
fill = GradientFill.new(0, 0, 20, 0, '#888888', '#888888')
img = Image.new(20, 20, fill)
img.write('./test.png') { self.format = 'PNG' }
The expected output would be a 20x20 png that's solid mid-gray. What I get instead is a pure white block. If I have a color for the second value, it seems to work fine. Any ideas?
For reference, I'm using:
ImageMagick version: ImageMagick 6.8.0-2 2012-11-28 Q8
RMagick version: RMagick 2.13.2
As so often happens, 5 minutes after posting this, I found the answer. Short answer, there was a short-lived bug in ImageMagick regarding writing PNGs that are pure gray. Bug was present from 6.8.0-1 to 6.8.0-8.
Upgrading should fix it...

Mysterious PNG RGB+Alpha image that works with IE6

http://moztw.org/images/product-front-thunderbird.png
I am sure that this is a PNG image in RGB colors and a alpha channel (look at the shadow below the icon), but this file mysteriously works with IE6 w/o any special CSS hack (though it seems the alpha channel is being replaced by 2-bit mask in IE6).
Can anyone tell me exactly what information is in the file? It would be even better if someone could give a guidance on how to create such file. Thanks.
Edit: Friends at moztw.org added script hack to the website, so the above statement is no longer verifiable. But your are still welcome to investigate the image format.
That's PNG8+alpha. Explanation and examples in "PNG that works" article.
You can generate such files with pngquant (on a Mac, ImageAlpha is a GUI for it).

wkhtmltopdf + mixed photo colors

I have a page with color and black & white images, when I use wkhtmltopdf to generate a pdf from the html file,
I get only the color images, how can I get it to print all the images, I have tried the advance options, but no success.
If I print in grayscale, then all the images are coming, but I don't want grayscale.
Any help?
I figured out how to solve this problem, I tried a trial-and-error method and figured out that if we provided the following parameters all the images irrespective of color or B&W, it would come up
wkhtmltopdf --image-quality 75
NOTE: Any value <= 75 works, higher value print a blank space for B&W images only
This question is old, but I'll add this in case someone has a similar issue and is unable to upgrade.
I found that applying this CSS solved the problem of non-color image rendering for me.
img {opacity: 0.99;}
To follow on from my comment above:
Changing the image quality only helps some cases
I contacted the developer (antialize) and he has now fixed this issue on his staging branch.
There isn't a release yet with this fix, so you will have to compile it yourself.
If you compile using the latest staging branch, all images now work as expected. Remember to compile following the "Installing QT yourself" instructions.
Furthermore, if you want to compile statically, see my note (on the compilation wiki page, made on Dec 19, 2011) about editing two files to allow static compiling of webkit (the 2 files that need to change are /tools/configure/configureapp.cpp and configure).

Importing SVG into an RMagick RVG session

We have some code that draws things using RVG in RMagick. We scale them at various sizes. What I'm trying to do is use a file that's saved as an SVG as a template.
The problem is, is when I load an SVG using Magick::Image.read, and then 'use' it, it rasterizes it, and then scales it, instead of producing pretty vectors.
Is there a way one might go about doing this properly?
Thanks!
Thoughts:
Imagemagick is fundamentally raster oriented. If you want to keep your image in vector format, don't use Imagemagick, as the author will tell you. That said, even though imagemagick may by default convert your image to raster, you can convert it back from raster into vector format (sometimes).
SVuGy may help. It doesn't appear to have a lot of support, but may work. I haven't used it.
The rmagick developer claims to have a SVG to RVG parser, you might try contacting him at rmagick AT rubyforge DOT org.
If all else fails, punt. You could write an xslt to convert your svg to rvg. The two formats are close enough that this might not be too painful. Else a tool like genshi might be a faster conversion path for you if you know more python than xlst.

Convert a .doc or .pdf to an image and display a thumbnail in Ruby?

Convert a .doc or .pdf to an image and display a thumbnail in Ruby?
Does anyone know how to generate document thumbnails in Ruby (or C, python...)
A simple RMagick example to convert a PDF to a PNG would be:
require 'RMagick'
pdf = Magick::ImageList.new("doc.pdf")
thumb = pdf.scale(300, 300)
thumb.write "doc.png"
To convert a MS Word document, it won't be as easy. Your best option may be to first convert it to a PDF before generating the thumbnail. Your options for generating the PDF depend heavily on the OS you're running on. One might be to use OpenOffice and the Python Open Document Converter. There are also online conversion services you could try, including http://Zamzar.com.
Sample code to answer the comment by #aisensiy above :
require 'rmagick'
pdf_path = "/path/to/interesting/file.pdf"
page_index_path = pdf_path + "[0]" # first page in PDF
pdf_page = Magick::Image.read( page_index_path ).first # first item in Magick::ImageList
pdf_page.write( "/tmp/indexed-page.png" ) # implicit conversion based on file extension
Based on the path clue in answer to another question :
https://stackoverflow.com/a/6369524/765063
Not sure about .doc support in any open source library but ImageMagick (and the RMagick gem) can be compiled with pdf support (I think it's on by default)
PDF support is a little buggy in ImageMagick - but it's by far the best OS way for ruby. There's also a google summer of code project for pure Ruby PDF support.
I've read stuff about using OpenOffice without the GUI to transform .doc files - but it'll be complicated at best.
As the 2 previous posters said, ImageMagick's probably the easiest way to generate the thumbnails.
You could exec something like:
´convert -size 300x300 doc.pdf doc.png´
(The backquotes tell Ruby to shell it out).
If you don't want to use exec to do the conversion you could use the RMagick gem to do it for you but it's probably a bit more of code.
If you don't mind paying for Imgix, it handles PDFs too. You get all the benefits of a fast CDN with it.

Resources