Skip to content

Pillow 8.3 and NumPy #5571

Closed
Closed

Description

@doublex

Throws exception with Pillow 8.3: TypeError: __array__() takes 1 positional argument but 2 were given

with PIL.Image.open(filepath) as img:
    numpy.array( img, dtype=numpy.float32 )

Activity

  1. radarhere commented on Jul 1, 2021

    @radarhere
    Member

    What version of Python are you using? What version of numpy?

  2. vgrabovets commented on Jul 1, 2021

    @vgrabovets

    Same problem here.
    Python 3.7.4
    numpy 1.21.0

    workaround:

    with PIL.Image.open(filepath) as img:
        numpy.array(img).astype(np.float32)
  3. t-vi commented on Jul 1, 2021

    @t-vi
    Contributor

    Works with just about anything. The problem is that pillow newly implemented __array__, but only without arguments. Numpy requires __array__ to take an optional dtype argument. This is used e.g. by numpy.array when the user passes a dtype argument. This should be with just about any numpy/python version.

  4. doublex commented on Jul 1, 2021

    @doublex
    Author

    fails with python 3.8 + 3.9, numpy 1.20 + 1.21

  5. t-vi commented on Jul 1, 2021

    @t-vi
    Contributor

    I'll send a patch.

  6. carmocca commented on Jul 1, 2021

    @carmocca

    We are seeing errors in 3.{6,7,8,9}

  7. ericpre commented on Jul 1, 2021

    @ericpre

    Same issue in https://github.com/hyperspy/hyperspy/runs/2961516889 for all python version >=3.6 using the imageio library:

                    # Apply palette
    >               frame_paletted = np.array(im, np.uint8)
    E               TypeError: __array__() takes 1 positional argument but 2 were given
    
    /opt/hostedtoolcache/Python/3.9.5/x64/lib/python3.9/site-packages/imageio/plugins/pillow.py:745: TypeError

    The python 3.6 build is with numpy 1.17.1

  8. changed the title [-]Pillow 8.3 and numpy[/-] [+]Pillow 8.3 and NumPy[/+] on Jul 1, 2021
  9. cgebbe commented on Jul 1, 2021

    @cgebbe

    same here: windows, python=3.6.8, numpy=1.19.5, Pillow=8.3.0

  10. seberg commented on Jul 1, 2021

    @seberg

    Just to note, there is a small chance that some details changed in NumPy 1.19.x, but I doubt it matters. NumPy itself actually does not care whether you do return the correct dtype here, it is more of a hint at the moment. But, it is probably good habit to just return the requested dtype anyway.

  11. added a commit that references this issue on Jul 1, 2021
  12. added a commit that references this issue on Jul 2, 2021
  13. IceTDrinker commented on Jul 5, 2021

    @IceTDrinker

    Any chance of a hotfix release ?

    Edit : Every torchvision user using the ToTensor transform (i.e. all the vision people) will encounter the issue (and their build systems)

  14. janosh commented on Jul 6, 2021

    @janosh

    Definitely needs a hot fix. This is all over the place.

  15. hugovk commented on Jul 6, 2021

    @hugovk
    Member

    There'll be an 8.3.1 soon, keep an eye on #5578. Thank you for your patience!

  16. radarhere commented on Jul 7, 2021

    @radarhere
    Member

    Pillow 8.3.1 has now been released with a fix for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions