JuliaIO / PNGFiles.jl

FileIO.jl integration for PNG files
MIT License
20 stars 12 forks source link

MethodError when used with FileIO #59

Closed mfiano closed 2 years ago

mfiano commented 2 years ago

It took me a few hours (as a relative beginner) to finally get a MWE, but this is how it errors:

mfiano@workstation ~$ julia
                   _
       _       _ _(_)_     |  Documentation: https://docs.julialang.org
      (_)     | (_) (_)    |
       _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
      | | | | | | |/ _` |  |
      | | |_| | | | (_| |  |  Version 1.8.0 (2022-08-17)
     _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
    |__/                   |

    (@v1.8) pkg> st
    Status `~/.data/julia/environments/v1.8/Project.toml`
      [3da002f7] ColorTypes v0.11.4
      [5789e2e9] FileIO v1.15.0
      [82e4d734] ImageIO v0.6.6

    (v1.8) julia> using ColorTypes, FileIO, ImageIO

    (v1.8) julia> x = Array{ColorTypes.RGB}(undef, 4, 4)
    4×4 Array{RGB,2} with eltype RGB:
     #undef  #undef  #undef  #undef
     #undef  #undef  #undef  #undef
     #undef  #undef  #undef  #undef
     #undef  #undef  #undef  #undef

    (v1.8) julia> fill!(x, ColorTypes.RGB(1.0,1.0,1.0))
    4×4 Array{RGB,2} with eltype RGB:
     RGB{Float64}(1.0,1.0,1.0)  RGB{Float64}(1.0,1.0,1.0)  RGB{Float64}(1.0,1.0,1.0)  RGB{Float64}(1.0,1.0,1.0)
     RGB{Float64}(1.0,1.0,1.0)  RGB{Float64}(1.0,1.0,1.0)  RGB{Float64}(1.0,1.0,1.0)  RGB{Float64}(1.0,1.0,1.0)
     RGB{Float64}(1.0,1.0,1.0)  RGB{Float64}(1.0,1.0,1.0)  RGB{Float64}(1.0,1.0,1.0)  RGB{Float64}(1.0,1.0,1.0)
     RGB{Float64}(1.0,1.0,1.0)  RGB{Float64}(1.0,1.0,1.0)  RGB{Float64}(1.0,1.0,1.0)  RGB{Float64}(1.0,1.0,1.0)

    (v1.8) julia> save("img.png", x)
    Errors encountered while save File{DataFormat{:PNG}, String}("img.png").
    All errors:
    ===========================================
    MethodError: no method matching __get_bit_depth(::Type{Any})
    Closest candidates are:
      __get_bit_depth(::Type{FixedPointNumbers.Normed{T, 8}}) where T at ~/.data/julia/packages/PNGFiles/Ee6Hz/src/utils.jl:10
      __get_bit_depth(::Type{FixedPointNumbers.Normed{T, 16}}) where T at ~/.data/julia/packages/PNGFiles/Ee6Hz/src/utils.jl:11
      __get_bit_depth(::Type{FixedPointNumbers.Normed{T, N}}) where {T, N} at ~/.data/julia/packages/PNGFiles/Ee6Hz/src/utils.jl:12
      ...
    ===========================================
    ArgumentError: Package ImageMagick [6218d12a-5da1-5696-b52f-db25d2ecc6d1] is required but does not seem to be installed:
     - Run `Pkg.instantiate()` to install all recorded dependencies.

    ===========================================
    ArgumentError: Argument does not support conversion to png.
    ===========================================

    Fatal error:
    ERROR: MethodError: no method matching __get_bit_depth(::Type{Any})
    Closest candidates are:
      __get_bit_depth(::Type{FixedPointNumbers.Normed{T, 8}}) where T at ~/.data/julia/packages/PNGFiles/Ee6Hz/src/utils.jl:10
      __get_bit_depth(::Type{FixedPointNumbers.Normed{T, 16}}) where T at ~/.data/julia/packages/PNGFiles/Ee6Hz/src/utils.jl:11
      __get_bit_depth(::Type{FixedPointNumbers.Normed{T, N}}) where {T, N} at ~/.data/julia/packages/PNGFiles/Ee6Hz/src/utils.jl:12
      ...
    Stacktrace:
      [1] _get_bit_depth(img::Matrix{RGB})
        @ PNGFiles ~/.data/julia/packages/PNGFiles/Ee6Hz/src/utils.jl:4
      [2] _save(png_ptr::Ptr{Nothing}, info_ptr::Ptr{Nothing}, image::Matrix{RGB}; compression_level::Int64, compression_strategy::Int64, filters::Int64, file_gamma::Nothing, background::Nothing)
        @ PNGFiles ~/.data/julia/packages/PNGFiles/Ee6Hz/src/io.jl:426
      [3] save(fpath::String, image::Matrix{RGB}; compression_level::Int64, compression_strategy::Int64, filters::Int64, file_gamma::Nothing, background::Nothing)
        @ PNGFiles ~/.data/julia/packages/PNGFiles/Ee6Hz/src/io.jl:366
      [4] save(fpath::String, image::Matrix{RGB})
        @ PNGFiles ~/.data/julia/packages/PNGFiles/Ee6Hz/src/io.jl:340
      [5] invokelatest(::Any, ::Any, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
        @ Base ./essentials.jl:729
      [6] invokelatest(::Any, ::Any, ::Vararg{Any})
        @ Base ./essentials.jl:726
      [7] (::LazyModules.LazyFunction)(::String, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
        @ LazyModules ~/.data/julia/packages/LazyModules/d9Be6/src/LazyModules.jl:29
      [8] (::LazyModules.LazyFunction)(::String, ::Vararg{Any})
        @ LazyModules ~/.data/julia/packages/LazyModules/d9Be6/src/LazyModules.jl:27
      [9] save(f::File{DataFormat{:PNG}, String}, image::Matrix{RGB}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
        @ ImageIO ~/.data/julia/packages/ImageIO/xMHN9/src/ImageIO.jl:70
     [10] save(f::File{DataFormat{:PNG}, String}, image::Matrix{RGB})
        @ ImageIO ~/.data/julia/packages/ImageIO/xMHN9/src/ImageIO.jl:69
     [11] #invokelatest#2
        @ ./essentials.jl:729 [inlined]
     [12] invokelatest
        @ ./essentials.jl:726 [inlined]
     [13] action(call::Symbol, libraries::Vector{Union{Base.PkgId, Module}}, file::Formatted, args::Matrix{RGB}; options::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
        @ FileIO ~/.data/julia/packages/FileIO/DtNtF/src/loadsave.jl:219
     [14] action
        @ ~/.data/julia/packages/FileIO/DtNtF/src/loadsave.jl:196 [inlined]
     [15] action(call::Symbol, libraries::Vector{Union{Base.PkgId, Module}}, sym::Symbol, file::String, args::Matrix{RGB}; options::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
        @ FileIO ~/.data/julia/packages/FileIO/DtNtF/src/loadsave.jl:185
     [16] action
        @ ~/.data/julia/packages/FileIO/DtNtF/src/loadsave.jl:185 [inlined]
     [17] #save#20
        @ ~/.data/julia/packages/FileIO/DtNtF/src/loadsave.jl:129 [inlined]
     [18] save(file::String, args::Matrix{RGB})
        @ FileIO ~/.data/julia/packages/FileIO/DtNtF/src/loadsave.jl:125
     [19] top-level scope
        @ REPL[5]:1
    Stacktrace:
     [1] handle_error(e::MethodError, q::Base.PkgId, bt::Vector{Union{Ptr{Nothing}, Base.InterpreterIP}})
       @ FileIO ~/.data/julia/packages/FileIO/DtNtF/src/error_handling.jl:61
     [2] handle_exceptions(exceptions::Vector{Tuple{Any, Union{Base.PkgId, Module}, Vector}}, action::String)
       @ FileIO ~/.data/julia/packages/FileIO/DtNtF/src/error_handling.jl:56
     [3] action(call::Symbol, libraries::Vector{Union{Base.PkgId, Module}}, file::Formatted, args::Matrix{RGB}; options::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
       @ FileIO ~/.data/julia/packages/FileIO/DtNtF/src/loadsave.jl:228
     [4] action
       @ ~/.data/julia/packages/FileIO/DtNtF/src/loadsave.jl:196 [inlined]
     [5] action(call::Symbol, libraries::Vector{Union{Base.PkgId, Module}}, sym::Symbol, file::String, args::Matrix{RGB}; options::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
       @ FileIO ~/.data/julia/packages/FileIO/DtNtF/src/loadsave.jl:185
     [6] action
       @ ~/.data/julia/packages/FileIO/DtNtF/src/loadsave.jl:185 [inlined]
     [7] #save#20
       @ ~/.data/julia/packages/FileIO/DtNtF/src/loadsave.jl:129 [inlined]
     [8] save(file::String, args::Matrix{RGB})
       @ FileIO ~/.data/julia/packages/FileIO/DtNtF/src/loadsave.jl:125
     [9] top-level scope
       @ REPL[5]:1

    (v1.8) julia>
Drvi commented 2 years ago

Hi! The problem is that your x is not concretely typed, RGB struct is parametrized on the type of the of the individual r, g, and b elements, basically:

struct RGB{T}
    r::T
    g::T
    b::T
end

What you are missing is the type parameter -- with x = Array{ColorTypes.RGB{Float64}}(undef, 4, 4) would've worked.

By omitting the type parameter, you created an array which could hold many different types of RGBs, but that is rarely a good idea and not something that is supported by PNGFiles.jl.