Understanding ImageMagick's convert and translating to Ruby RMagick

前端 未结 5 1474
暗喜
暗喜 2021-01-13 15:06

I\'m failing at translating the following PHP/ImageMagick code into Ruby RMagick (to make it more manageable for future users and to understand what it\'s really doing):

5条回答
  •  一整个雨季
    2021-01-13 15:47

    The code checks to see if a particular image contains transparency.

    -format '%[fx:u.a]' info:

    This instructs image magick to check the first image u, the alpha channel of that a and output info on it, it will return 0 if the top-left pixel is transparent and non-zero if not I think. That is why the image is being resized to 1x1, so that only a single pixel needs to be consulted. The -channel o is the opacity channel.

    So the code in English would read, cycle through all PNG files, look at the alpha channel (opacity) only, resize to a single pixel and see if it's transparent. Hence the echo message.

    Unfortunately I do not know Ruby, or RMagick, but a quick look at the API seems to suggest using image.channel(AlphaChannel) to get the alpha channel (AlphaChannel is a ChannelType value, not sure if you have to specify ChannelType.AlphaChannel), then follow with .resize(1,1) to get the size down, and finish with either .pixel_color(0,0) or .get_pixels(0,0,1,1) to get the Pixel object back (get_pixels() returns an array), which I believe has an opacity attribute. However, the channel() command changes the RGB values to the value of the channel selected, and I'm not sure it preserves the opacity channel so you might just need to look at red forinstance, or omit the .channel() call entirely - though I do not know if that would disrupt the result offhand.

    Perhaps if Ruby supports decent functional programming approaches.

    image.channel(AlphaChannel).resize(1,1).pixel_color(0,0).red
    

    or this if pixel_color() does not return the opacity for some reason

    image.channel(AlphaChannel).resize(1,1).get_pixels(0,0,1,1)[0].red
    

    Without the channel() calls it would be:

    image.resize(1,1).pixel_color(0,0).opacity
    

    or

    image.resize(1,1).get_pixels(0,0,1,1)[0].opacity
    

    Again, my Ruby is non-existent, so you may have to rearrange those a lot, but the primitives are there.

    References

    1. RMagick Documentation
    2. ImageMagick 'fx' escapes
    3. ImageMagick -channel options

提交回复
热议问题