# Cannot load image and do fftshift inside the same function

**URL:** <https://discourse.julialang.org/t/cannot-load-image-and-do-fftshift-inside-the-same-function/59663>\
**Category:** General Usage\
**Created:** [April 20, 2021, 1:26pm UTC](https://discourse.julialang.org/t/cannot-load-image-and-do-fftshift-inside-the-same-function/59663 "2021-04-20T13:26:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alberttxu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alberttxu/32/23652_2.png) [@alberttxu](https://discourse.julialang.org/u/alberttxu)\
**Post date:** [April 20, 2021, 1:26pm UTC](https://discourse.julialang.org/t/cannot-load-image-and-do-fftshift-inside-the-same-function/59663/1 "2021-04-20T13:26:42Z")

</div>

Hi all,

I encountered an error while trying to load an image and run an fftshift inside a function. My minimal working example is

```julia
using TestImages, FFTW

function f()
    img = testimage("mandrill")
    fftshift(img)
end

f()

```

which I have inside a test.jl file. When I include this in the REPL the first time, I get a MethodError. But the second time it works.

```julia
julia> include("test.jl");
ERROR: MethodError: no method matching size(::TiffImages.DenseTaggedImage{ColorTypes.RGB{FixedPointNumbers.N0f8}, 2, UInt32, Matrix{ColorTypes.RGB{FixedPointNumbers.N0f8}}})
The applicable method may be too new: running in world age 29644, while current world is 29651.
Closest candidates are:
  size(::TiffImages.DenseTaggedImage) at /Users/albertxu/.julia/packages/TiffImages/10dKE/src/types/dense.jl:24 (method too new to be called from this world context.)
  size(::AbstractArray{T, N}, ::Any) where {T, N} at abstractarray.jl:38
  size(::Union{LinearAlgebra.Adjoint{T, var"#s832"}, LinearAlgebra.Transpose{T, var"#s832"}} where {T, var"#s832"<:(AbstractVector{T} where T)}) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/adjtrans.jl:196
  ...
Stacktrace:
 [1] size
   @ ./abstractarray.jl:38 [inlined]
 [2] #24
   @ ~/.julia/packages/AbstractFFTs/JebmH/src/definitions.jl:363 [inlined]
 [3] ntuple
   @ ./ntuple.jl:19 [inlined]
 [4] fftshift(x::TiffImages.DenseTaggedImage{ColorTypes.RGB{FixedPointNumbers.N0f8}, 2, UInt32, Matrix{ColorTypes.RGB{FixedPointNumbers.N0f8}}}, dim::UnitRange{Int64}) (repeats 2 times)
   @ AbstractFFTs ~/.julia/packages/AbstractFFTs/JebmH/src/definitions.jl:363
 [5] f()
   @ Main ~/.julia/dev/SimpleCTF/test.jl:5
 [6] top-level scope
   @ ~/.julia/dev/SimpleCTF/test.jl:8

julia> include("test.jl");

julia>

```

However, calling either testimage or fftshift outside the function works as well.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [April 20, 2021, 1:41pm UTC](https://discourse.julialang.org/t/cannot-load-image-and-do-fftshift-inside-the-same-function/59663/2 "2021-04-20T13:41:44Z")

</div>

Ah, yeah, it looks like the image loading code inside `testimage` (which I think uses `ImageIO.jl` internally) does some magic to lazily load different packages to handle different image types.

I can think of two different ways to avoid this issue:

1. Use `invokelatest` to run functions that depend on `img` within the context of your function:

```julia
julia> using TestImages

julia> function f()
         img = testimage("mandrill")
         Base.invokelatest(size, img)
       end
f (generic function with 1 method)

julia> f()
(512, 512)

```

You can learn more about `invokelatest` here: [Essentials · The Julia Language](https://docs.julialang.org/en/v1/base/base/#Base.invokelatest)

1. Ensure that `TiffImages` is already loaded so that `testimage` doesn’t need to do its lazy loading in the background:

```julia
julia> using TestImages, TiffImages

julia> function f()
         img = testimage("mandrill")
         size(img)
       end
f (generic function with 1 method)

julia> f()
(512, 512)

```

I would also consider creating an issue over at [GitHub - JuliaImages/TestImages.jl: commonly used test images](https://github.com/JuliaImages/TestImages.jl) , since I’m sure you’re not the only person to have had this problem.

---

<div class="post-metadata">

**Author:** ![JohnnyChen94](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnnychen94/32/29979_2.png) [@JohnnyChen94](https://discourse.julialang.org/u/JohnnyChen94)\
**Post date:** [April 20, 2021, 4:12pm UTC](https://discourse.julialang.org/t/cannot-load-image-and-do-fftshift-inside-the-same-function/59663/3 "2021-04-20T16:12:56Z")

</div>

See also [https://github.com/JuliaIO/FileIO.jl/issues/283](https://github.com/JuliaIO/FileIO.jl/issues/283)
