Вопрос

I have the following command line script that works as I'd like:

convert photo.jpg -colorspace gray \( +clone -blur 0x10 \) +swap -compose divide -composite -linear-stretch 5%x0% sketch.jpg

But I'm having some difficulty getting the final image to look the same in my Rails project. I'm using RMagick, and I've tried to follow the conversion docs as best I can, but it's not quite right.

My process code (in a CarrierWave::Uploader::Base class) is as follows:

process :do_stuff

def do_stuff
  manipulate! do |img|
    img.colorspace = Magick::GRAYColorspace
    blur = img.clone.blur_image(0,10)
    blurred = Magick::ImageList.new
    blurred << blur
    blurred << img
    sketch = Magick::Image.new(300,20)
    img = sketch.composite(blurred,Magick::CenterGravity,Magick::DivideCompositeOp)
    img = img.linear_stretch('05%','00%')
  end
end

I'm wondering if someone clever can take a look at this code to see it's been correctly converted to Ruby?

Also, and it seems almost trivial (but I can't work it out), how do I get the sketch initializer to use the size of the original image?

Any help much appreciated. Thanks.

Это было полезно?

Решение

Hate to answer my own question, but I seem to have solved it.

It looks like I was trying to be too clever, but after reading the docs a few more times I think I was getting confused with the how the +swap and composite method interact.

As I understand it, the +swap ensures that instead of the blurred image getting composited on the greyscale image, now the greyscale image gets composited on top of the blurred image.

Once this clicked, the script looks much simpler:

process :do_stuff

def do_stuff
  manipulate! do |img|
    img.colorspace = Magick::GRAYColorspace
    blur = img.clone.blur_image(0,10)
    img = blur.composite(img,Magick::CenterGravity,Magick::DivideCompositeOp)
    img = img.linear_stretch('5%','0%')
  end
end

And that seems to produce the same results as my original ImageMagick script.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top