GithubHelp home page GithubHelp logo

Add image tinting about imageproc HOT 13 CLOSED

image-rs avatar image-rs commented on August 18, 2024
Add image tinting

from imageproc.

Comments (13)

theotherphil avatar theotherphil commented on August 18, 2024

I couldn't make sense of the ImageMagick code, but I guess we're after something like the following:

use image::{
    GrayImage,
    Luma,
    Pixel,
    Rgb,
    RgbImage
};

use definitions::Clamp;

pub fn tint_image(image: &GrayImage, tint_color: Rgb<u8>) -> RgbImage {
    let (width, height) = image.dimensions();
    let mut out = RgbImage::new(width, height);

    for y in 0..height {
        for x in 0..width {
            let tinted = tint(*image.get_pixel(x, y), tint_color);
            out.put_pixel(x, y, tinted);
        }
    }

    out
}

/// Tint a grayscale value with the given color.
pub fn tint(gray: Luma<u8>, color: Rgb<u8>) -> Rgb<u8> {
    let dist_from_mid = (gray[0] as f32 - 128f32).abs();
    let scale_factor = 1f32 - 4f32 * dist_from_mid * dist_from_mid;
    Rgb([gray[0]; 3])
        .map2(&color, |p, q| {
            Clamp::clamp(p as f32 + scale_factor * q as f32)
        })
}

#[cfg(test)]
mod test {

    use super::{
        tint_image
    };
    use utils::{
        load_image_or_panic
    };
    use image::Rgb;
    use test;
    use std::path::Path;

    #[test]
    fn test_tint() {
      let ip = Path::new("./tests/data/elephant.png");
      let tp = Path::new("./tests/data/truth/tintyphant.png");
      let image = load_image_or_panic(&ip).to_luma();
      let eq = tint_image(&image, Rgb([0u8, 0u8, 255u8]));
      let _ = eq.save(&Path::new(tp)).unwrap();
    }
}

from imageproc.

theotherphil avatar theotherphil commented on August 18, 2024

@softprops: is this roughly what you were after? I'll tidy this up before committing anyway (it was bodged together in a few minutes, and has some odd artefacts), but I'll wait and see if this is actually what's wanted first.

lumaphant
tintyphant

from imageproc.

softprops avatar softprops commented on August 18, 2024

Damn you guys are fast! I'm out to town today but I'll give it a spin when i get hone tonight. I think this is perfect for tinting but what I was looking for was something slightly different.

The duotone effect looks something like that but parameterized with 2 colors that form a gradient that could be color mapped to the grayscale pixels.

Here's some visual examples

https://www.google.com/search?q=duotone&client=ms-android-verizon&biw=360&bih=264&prmd=isvn&source=lnms&tbm=isch&sa=X&ved=0ahUKEwjXjaz_lqDKAhWCKj4KHb2hAgYQ_AUIBigB

from imageproc.

softprops avatar softprops commented on August 18, 2024

There's a bit more info under the image magic docs about the technique. http://www.imagemagick.org/Usage/color_mods/#duotone

from imageproc.

theotherphil avatar theotherphil commented on August 18, 2024

Cool, thanks. I missed that somehow. I'll add that in the next couple of days.

from imageproc.

softprops avatar softprops commented on August 18, 2024

Man. You guys set a high bar for oss friendliness and turnarounds. Keep it up

from imageproc.

theotherphil avatar theotherphil commented on August 18, 2024

Thanks! Sadly the tint function above is rubbish, but I'll add a fixed version tomorrow

from imageproc.

softprops avatar softprops commented on August 18, 2024

I'm not sure how compatible it is typewise with this crate but I thought this may be of use http://www.piston.rs/image/image/imageops/colorops/fn.index_colors.html

from imageproc.

theotherphil avatar theotherphil commented on August 18, 2024

imageproc is based on that image crate (the small overlap between this crate and image::imagops will be removed eventually). So the traits and concrete types used are identical. However, it doesn't look like that function gains us too much - implementing our own ColorMap impl for either tinting or duotone would be the same amount of work as writing the functions directly.

from imageproc.

theotherphil avatar theotherphil commented on August 18, 2024

Ok, I may have been a little optimistic when I claimed I'd get this done in a couple of days. Getting aesthetically pleasing results looks like it will require a lot of flexibility in the tinting/blending functions, and writing something decent for this would take a lot longer than I have free this week (and to get the brightness of the tinted image to match the input image we'll need HSL support, which isn't currently available).

In the meantime, I've written a prototype implementation of tint and colour blending and pushed to a branch of my repo: theotherphil@0d9eccb.

Example tinting output:

bphant

Example blending output:

blendphant

Hopefully this at least gives you a starting point to play around with. I wouldn't count on anything in this area getting released soon, but I'm happy to help with any problems you have getting this to work for your use cases.

from imageproc.

theotherphil avatar theotherphil commented on August 18, 2024

One obvious problem with the current code is that we do non-trivial work for every pixel. So if you need this to be fast we can tweak the code to use a LUT as you suggested.

from imageproc.

softprops avatar softprops commented on August 18, 2024

@theotherphil you are a magician! That looks great

from imageproc.

theotherphil avatar theotherphil commented on August 18, 2024

Glad you like it. I've added an example with these colour effects in 1667547, so will close this issue now. (Bonus of this code over that in theotherphil/imageproc/effects is that it actually produces the output shown above, instead of getting the colour gradient wrong.)

cargo run --example color_effects <your_file_here>

It'll create a tinted.png and gradient.png file in the same directory as the input file you provide.

from imageproc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.