Comments (13)
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.
@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.
from imageproc.
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
from imageproc.
There's a bit more info under the image magic docs about the technique. http://www.imagemagick.org/Usage/color_mods/#duotone
from imageproc.
Cool, thanks. I missed that somehow. I'll add that in the next couple of days.
from imageproc.
Man. You guys set a high bar for oss friendliness and turnarounds. Keep it up
from imageproc.
Thanks! Sadly the tint function above is rubbish, but I'll add a fixed version tomorrow
from imageproc.
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.
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.
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:
Example blending output:
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.
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.
@theotherphil you are a magician! That looks great
from imageproc.
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)
- draw_hollow_polygon(_mut)
- 100% CPU usage during `imageproc::window::display_image` on Linux HOT 2
- Laplacian edge detection HOT 3
- Hint on how to support font fill transparency in `draw_text_mut()` HOT 1
- Add floodfill HOT 1
- `geometry::min_area_rect` should return `rect::Rect` instead of `[Point; 4]` HOT 1
- How to convert Canny to rgba for canvas drawing? HOT 3
- How to draw lines with different width? HOT 2
- Calculate the area of a contour
- Panicked while drawing huge ellipse. HOT 1
- Add generalizated convolution filter HOT 4
- Add Dithering [Willing to do it myself] HOT 2
- Interest for a parallel version of match_template? HOT 7
- Any reason for rand dependency to be at 0.7.3 HOT 2
- Testing requires nightly?
- Gaussian blur is loosing value HOT 1
- median_filter is two orders of magnitude slower than opencv::imgproc::median_blur. HOT 3
- Optimized separable filters HOT 1
- adaptive_threshold lacks the delta parameter HOT 2
- intersection_points function in hough.rs has bug if intersection point is in image corner. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from imageproc.