Pregunta

Does anyone know how to adjust the brightness of an image using RMagick? Rmagick has a number of different functions available, including ones to adjust levels and the hue/brightness/saturation levels, but I need to adjust the old-fashioned brightness/contrast levels.

There are custom functions for me to individually adjust each color channel (RGBA), but I'm not sure how to use levels to adjust the overall brightness. Messing with the different channels has yielded images that are color-altered. On GIMP, in the levels menu, the desired functionality I want is under 'Output Levels'. By dragging this below 255, I can achieve a 'darkening' effect. Is there some kind of equivalent in RMagick to control Output Levels? I don't see a channel for it.

Examples:

THIS IS ORIGINAL IMAGE: Original Image

THIS IS WHAT I WANT: Desired Result

THIS IS WHAT HAPPENS WHEN I ADJUST LIGHTNESS (Rmagick's Modulate) Undesired Result from Lightness

¿Fue útil?

Solución

I think this should do what you need.

img = Magick::Image.read('bT9xc.png')
img.first.level(-Magick::QuantumRange * 0.25, Magick::QuantumRange * 1.25, 1.0).write('out.png')

This sets the black point and the white point 'further away' from the range found in the image, which has the effect of making the brightest white in the source image darker, and the darkest black in the source image lighter.

If you want to make it darker overall, just increase the second factor to Magick::QuantumRange * 1.5 or higher.

Otros consejos

I think you can use the modulate method: http://www.imagemagick.org/RMagick/doc/image2.html#modulate

So to increase the brightness by 50% it would be something like:

img.modulate(1.5)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top