# Images -- is it possible to change selected colors?

**URL:** <https://discourse.julialang.org/t/images-is-it-possible-to-change-selected-colors/103263>\
**Category:** Visualization\
**Tags:** images\
**Created:** [August 27, 2023, 7:56pm UTC](https://discourse.julialang.org/t/images-is-it-possible-to-change-selected-colors/103263 "2023-08-27T19:56:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [August 27, 2023, 7:56pm UTC](https://discourse.julialang.org/t/images-is-it-possible-to-change-selected-colors/103263/1 "2023-08-27T19:56:46Z")

</div>

Thanks for great presentation of Images.jl at JuliaCon 2023. I haven’t really used the package (I’m not doing much work on images), but I see that I can import raw files from several camera brands. Nice.

I’m curious about the following: is it possible to selectively change some color to another color? This can be done in, say, Adobe Lightroom. To illustrate my question, I had the following image created using Bing Image Creator, with command “grumpy professor with yellow bowtie drinking tequila in Santa Fe adobe bar in picasso style”.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/b/1bf5c0c2dfc09a72b152c647380d5c1022b55565.jpeg)

Suppose I now am very happy with the picture (AI generated with some random seed, so impossible to exactly recreate). However, I regret that I specified “yellow bowtie” and wish I instead had written “orange bowtie”.

**Question** : Is it possible to specify that I want to change the yellow color shades in the image to shades of orange?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 27, 2023, 8:45pm UTC](https://discourse.julialang.org/t/images-is-it-possible-to-change-selected-colors/103263/2 "2023-08-27T20:45:33Z")

</div>

You can play with this:

```julia
using TestImages, Plots

img = testimage("mandrill")
plot(img)

# Turn red nose yellow:
img[0.9 .≤ red.(img) .≤ 1] .= RGB(1, 1, 0)
plot(img)

# Turn yellow nose orange:
img[0.95 .≤ red.(img) .≤ 1 .&& 0.95 .≤ green.(img) .≤ 1] .= RGB(1, 165/256, 0)
plot(img)

```

PS:  
_In other examples you might need to constrain more using all 3 color channels, here it was intentionally made simpler._

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [August 27, 2023, 8:57pm UTC](https://discourse.julialang.org/t/images-is-it-possible-to-change-selected-colors/103263/3 "2023-08-27T20:57:23Z")

</div>

Will check tomorrow. What I really need is to change a _range_ of colors which are (e.g.) yellowish into a range of colors that are (e.g.) orangish.

Should this be done in an hsv (hsb) representation?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 27, 2023, 9:03pm UTC](https://discourse.julialang.org/t/images-is-it-possible-to-change-selected-colors/103263/4 "2023-08-27T21:03:56Z")

</div>

> [@BLI](#):
>
> change a _range_ of colors which are (e.g.) yellowish

I think you can define a range of colors that are yellowish in the same way as above by including the blue channel as well:  
`(0.95 .≤ red.(img) .≤ 1) .&& (0.95 .≤ green.(img) .≤ 1) .&& (0 .≤ blue.(img) .≤ 0.05)`

Better use the `@. ` macro to simplify the dots:

```julia
@. img[(0.95 ≤ red(img) ≤ 1) && (0.95 ≤ green(img) ≤ 1) && (0 ≤ blue(img) ≤ 0.05)] = RGB(1, 165/256, 0)

```

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [August 28, 2023, 10:14am UTC](https://discourse.julialang.org/t/images-is-it-possible-to-change-selected-colors/103263/5 "2023-08-28T10:14:34Z")

</div>

If selecting pixels based on color alone does not give you what you want, consider starting with a segmented image (e.g., ImageSegmentation) and then algorithmically change the color. E.g.,

```julia
function more_orange(col::AbstractRGB)
    r, g, b = red(col), green(col), blue(col)
    r *= 1.2
    if r > 1
        r, g, b = r/r, g/g, b/b
    end
    return oftype(col, RGB(r, g, b))
end

```

and then use `more_orange` on the pixels of the segment corresponding to the bowtie.

(Untested)

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [August 29, 2023, 1:53pm UTC](https://discourse.julialang.org/t/images-is-it-possible-to-change-selected-colors/103263/6 "2023-08-29T13:53:39Z")

</div>

OK – some progress, but no solution yet…

```julia
bowtie = convert.(HSL,copy(img[203:247,170:252]))

```

leads to

 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/8/683e41a342d542e98dfe133e5b5ab34e93471e1a.png)

Seems like bowtie colors have hue value between 55 and 65 or so…:

```julia
julia > idx_yellow = 55 .< hue.(bowtie) .< 65
5×83 BitMatrix:
 0 0 0 1 0 1 0 0 0 0 0 0 0 … 0 0 0 0 0 0 0 0 0 0 1 0
 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
 0 0 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 0
 0 0 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 0
 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
 0 0 1 1 1 1 1 1 1 1 1 1 1 … 1 1 1 1 1 1 1 1 1 1 1 0
 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0
 ⋮ ⋮ ⋮ ⋱ ⋮ ⋮     
 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0
 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 1 1 1 1 1 1 … 1 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

```

I think `:orange` has a hue value of around 38-40.

**Question** : how can I change the _hue_ part of cells given by `idx_bowtie` from their current value to value ca. 40?

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [August 29, 2023, 2:46pm UTC](https://discourse.julialang.org/t/images-is-it-possible-to-change-selected-colors/103263/7 "2023-08-29T14:46:53Z")

</div>

> [@BLI](#):
>
> `copy(img[203:247,170:252])`

The `copy` is redundant because `img[rngs...]` copies already (use `view` or `@view` if you want a view instead).

> how can I change the _hue_ part of cells given by `idx_bowtie` from their current value to value ca. 40?

The `more_orange` function above is a prototype, but if you’re handling the decision-making externally, we can simplify it further:

```julia
more_orange(col::HSV) = HSV(40, col.s, col.v)

bowtie_new = [idx_yellow[i] ? more_orange(bowtie[i]) : bowtie[i] for i in CartesianIndices(bowtie)]

```

(untested)

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [August 29, 2023, 3:24pm UTC](https://discourse.julialang.org/t/images-is-it-possible-to-change-selected-colors/103263/8 "2023-08-29T15:24:09Z")

</div>

OK… now it seems to work:

```julia
bowtie = convert.(HSV,img[203:247,170:252])
idx_yellow = 55 .< hue.(bowtie) .< 65

new_color(pixel::HSV,new_hue) = HSV(new_hue,pixel.s,pixel.v)
bowtie[idx_yellow] .= new_color.(bowtie[idx_yellow],40)

```

converts the above bowtie image to the following:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/9/39c7a1b25581cd074b53d2e792f672691494051c.png)
