Drawing with color on Grayscale images

I have a grayscale image and I’d like to be able to draw a colored polygon on it with ImageDraw but am running into an error. Here is a MWE:

using TestImages, ImageDraw, Colors
img = testimage("cameraman")
draw(img,Polygon([(1,1),(30,1),(50,1),(30,30)]),colorant"indianred")

I suspect this is because the image is grayscale but the polygon is colored. If this is the case, is there a way around this that doesn’t involve converting the grayscale image to another colorspace?

error is

MethodError: no method matching draw!(::Array{Gray{FixedPointNumbers.Normed{UInt8,8}},2}, ::Polygon, ::RGB{FixedPointNumbers.Normed{UInt8,8}})
Closest candidates are:
  draw!(::AbstractArray{T<:Colorant,2}, ::Polygon, !Matched::T<:Colorant) where T<:Colorant at C:\Users\C7045\.julia\packages\ImageDraw\OAT7P\src\paths.jl:32
  draw!(::AbstractArray{T<:Colorant,2}, ::Drawable) where T<:Colorant at C:\Users\C7045\.julia\packages\ImageDraw\OAT7P\src\core.jl:137
  draw!(::AbstractArray{T<:Colorant,2}, !Matched::Point, !Matched::T<:Colorant) where T<:Colorant at C:\Users\C7045\.julia\packages\ImageDraw\OAT7P\src\core.jl:176

What type are you hoping draw to return? Why is it not an option to do draw!(convert.(RGB, img), ...?

1 Like

I have a large number of large grayscale images and was hoping to avoid that for efficiency purposes, but after looking into the definition of draw, it looks like it is unavoidable since it is overwriting the color information in img itself as opposed to displaying a polygon over img.