I have an image I am reading from a pdf file and converting to jpg. It works fine, until I apply "resize_to_fit" - which results in a black-rectangle (of the specified size).
file = file + "[0]"
jpg_file = file + ".jpg"
pdf = Magick::Image.read(file) do
self.quality = 80
self.density = '300'
self.colorspace = Magick::RGBColorspace
self.interlace = Magick::NoInterlace
end
pdf.first.resize_to_fit!("600")
}
pdf.first.write(jpg_file)
Subsituting:
pdf.first.change_geometry!('600x600') { |cols, rows, img|
img.resize!(cols, rows)
}
... for the resize makes no difference, nor changing the quality, or the density, nor omitting the colorspace and interlace settings.
Since I have a good image at full size (a mostly white image), I don't see why "resize" or "change_geometry" would output pure black.
Ideas?
Dozens of random experiments later, I found the only conversion of size which does not output a black rectangle, is:
pdf.first.sample!(0.25)
The limitation, of course, is that I must have a consistent input-size for this to work, as the other argument-set (x and y) will change the aspect-ratio.
Also, the quality produced by 'sample' is horrible, no matter the settings applied on the input or output side.
I need a way to get resize_to_fit to work properly. I am following the docs and examples, so the result makes no sense. I really hope someone who uses rmagick often, and is familiar with what parts of it are not broken, or what I am doing wrong, can respond with help. Thanks
Answer from @bumpy was the solution. I am now doing it a different way using Carrierwave, but I rewound the code and did an A:B test; the line
pdf.first.alpha(Magick::DeactivateAlphaChannel)
... works. Note that Carrierwave does the conversion correctly, and with decent quality results (identical to this solution), without any special settings. I would guess this is built into its defaults for conversions to jpg.
It's possible your PDF file has a transparent background, which is causing the problem. Try removing the alpha channel before the resize using
pdf.first.alpha(Magick::DeactivateAlphaChannel)
pdf.first.resize_to_fit!("600")