Skip to content

Different png RGBA values using Julia, C or Python #48

@t-bltg

Description

@t-bltg

Per https://discourse.julialang.org/t/reading-png-rgb-channels-julia-vs-python/73599.

Reproducer at https://github.com/t-bltg/PngPixel.jl.

Xref: JuliaIO/ImageIO.jl#41 (comment), @johnnychen94.
For reference, the image used here is a scaled version of toucan.png from TestImages.jl: $ convert toucan.png -scale 5% img.png.

Tested with #47 but still fails on 1.6.5 or 1.7.1 (official binaries):

(@v1.6) pkg> st
      Status `~/.julia/environments/v1.6/Project.toml`
  [3da002f7] ColorTypes v0.11.0
  [6218d12a] ImageMagick v1.2.2
  [f57f5aa1] PNGFiles v0.3.12 `https://github.com/JuliaIO/PNGFiles.jl.git#paletted_images_fix`
  [295af30f] Revise v3.2.1

julia> versioninfo()
Julia Version 1.6.5
Commit 9058264a69 (2021-12-19 12:30 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, sandybridge)

julia> using PNGFiles, Downloads

julia> (x->(Int(x.r.i), Int(x.g.i), Int(x.b.i), Int(x.alpha.i)))(PNGFiles.load(Downloads.download("https://aws1.discourse-cdn.com/business5/uploads/julialang/original/3X/5/c/5c5cdaa477dcae516c7183c3e710ef65a0bb6ab0.png"))[3,3])
(233, 173, 4, 244)  # correct

julia> (x->(Int(x.r.i), Int(x.g.i), Int(x.b.i), Int(x.alpha.i)))(PNGFiles.load(Downloads.download("https://raw.githubusercontent.com/t-bltg/PngPixel.jl/master/img.png"))[3,3])
(245, 214, 39, 244)  # please use this url, somehow, the .png files are different in size ^^^^^^^^^^^^^^^^

@Drvi , this is getting weird, maybe I changed the file when uploading to discourse, sorry about the confusion:

$ identify -verbose img.png | grep Filesize
  Filesize: 481B
$ convert img.png txt:- | grep '2,2:'
2,2: (59881,44461,1028,62708)  #E9AD04F4  rgba(233,173,4,0.956863)
$ identify -verbose 5c5cdaa477dcae516c7183c3e710ef65a0bb6ab0.png | grep Filesize
  Filesize: 329B
$ convert 5c5cdaa477dcae516c7183c3e710ef65a0bb6ab0.png txt:- | grep '2,2:'
2,2: (59881,44461,1028,62708)  #E9AD04F4  srgba(233,173,4,0.956863)

OK, so this a Srgba vs rgba thing, let's use the SRGB -> linear transformation (https://en.wikipedia.org/wiki/SRGB#Transformation) (sorry i'm not very familiar with images theory, is that correct ?)

julia> using PNGFiles, Downloads
julia> lin(x) = x > .04045 ? ((x + .055) / 1.055)^2.4 : x / 12.92
julia> img = PNGFiles.load(Downloads.download("https://raw.githubusercontent.com/t-bltg/PngPixel.jl/master/img.png"))
julia> round.(Int, 255lin.([img[3,3].r, img[3,3].g, img[3,3].b]))
3-element Vector{Int64}:
 233
 171  # NOK
   5  # NOK

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions