Skip to content

Saving an image as .jpg also mutates (clamps to 0..1) the image #1072

Description

@giacomogallina

I have an image which contains values that are out of the allowed range 0.0...1.0, so I would expect that saving the image as .jpg would clamp its values in the allowed range. What also seems to happen is that the values of the image that I pass to the save function get clamped.
This was unexpected, since save does not have a ! to indicate that it might mutate its arguments. Is this behavior normal, or is it a bug?

julia> using Images

julia> using Colors

julia> cimg = RGB.(colormatch.(400 .+ 100 .* rand(4, 4))) .* 2
4×4 Matrix{RGB{Float64}}:
 RGB(0.854312, 0.0, 2.0)  RGB(0.859603, 0.0, 2.0)  RGB(0.0, 0.297911, 2.0)      RGB(0.71236, 0.0, 2.0)
 RGB(0.894001, 0.0, 2.0)  RGB(0.902251, 0.0, 2.0)  RGB(0.0, 1.31383, 1.38303)   RGB(0.0, 0.0, 2.0)
 RGB(0.961446, 0.0, 2.0)  RGB(0.821326, 0.0, 2.0)  RGB(0.470173, 0.0, 1.13316)  RGB(0.7587, 0.0, 1.77947)
 RGB(0.0, 0.627729, 2.0)  RGB(0.0, 0.814836, 2.0)  RGB(0.0, 1.25673, 1.46109)   RGB(1.00702, 0.0, 2.0)

julia> save("boh.jpg", cimg)
708

julia> cimg
4×4 Matrix{RGB{Float64}}:
 RGB(0.854312, 0.0, 1.0)  RGB(0.859603, 0.0, 1.0)  RGB(0.0, 0.297911, 1.0)  RGB(0.71236, 0.0, 1.0)
 RGB(0.894001, 0.0, 1.0)  RGB(0.902251, 0.0, 1.0)  RGB(0.0, 1.0, 1.0)       RGB(0.0, 0.0, 1.0)
 RGB(0.961446, 0.0, 1.0)  RGB(0.821326, 0.0, 1.0)  RGB(0.470173, 0.0, 1.0)  RGB(0.7587, 0.0, 1.0)
 RGB(0.0, 0.627729, 1.0)  RGB(0.0, 0.814836, 1.0)  RGB(0.0, 1.0, 1.0)       RGB(1.0, 0.0, 1.0)

Saving as .png instead raises a useful warning, and the resulting image gives an error if I try to open it with gnome's image viewer, but the image is not modified:

julia> cimg = RGB.(colormatch.(400 .+ 100 .* rand(4, 4))) .* 2
4×4 Matrix{RGB{Float64}}:
 RGB(0.322375, 0.0, 0.81094)  RGB(0.984288, 0.0, 2.0)  RGB(0.812989, 0.0, 2.0)     RGB(0.0, 0.649289, 2.0)
 RGB(0.0, 0.0, 2.0)           RGB(0.828919, 0.0, 2.0)  RGB(1.03181, 0.0, 2.0)      RGB(0.448978, 0.0, 1.08674)
 RGB(0.498063, 0.0, 1.19444)  RGB(0.0, 0.549037, 2.0)  RGB(0.0, 0.0, 2.0)          RGB(0.0, 1.50869, 1.13517)
 RGB(0.284016, 0.0, 2.0)      RGB(0.0, 0.564164, 2.0)  RGB(0.0, 1.17173, 1.58052)  RGB(0.0, 0.0, 2.0)

julia> save("boh.png", cimg)
┌ Warning: Mapping to the storage type failed; perhaps your data had out-of-range values?
│ Try `map(clamp01nan, img)` to clamp values to a valid range.
└ @ ImageMagick ~/.julia/packages/ImageMagick/KkM1b/src/ImageMagick.jl:180
188

julia> cimg
4×4 Matrix{RGB{Float64}}:
 RGB(0.322375, 0.0, 0.81094)  RGB(0.984288, 0.0, 2.0)  RGB(0.812989, 0.0, 2.0)     RGB(0.0, 0.649289, 2.0)
 RGB(0.0, 0.0, 2.0)           RGB(0.828919, 0.0, 2.0)  RGB(1.03181, 0.0, 2.0)      RGB(0.448978, 0.0, 1.08674)
 RGB(0.498063, 0.0, 1.19444)  RGB(0.0, 0.549037, 2.0)  RGB(0.0, 0.0, 2.0)          RGB(0.0, 1.50869, 1.13517)
 RGB(0.284016, 0.0, 2.0)      RGB(0.0, 0.564164, 2.0)  RGB(0.0, 1.17173, 1.58052)  RGB(0.0, 0.0, 2.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions