Skip to content

Type Error when saving jpg image (Pillow 3) #10633

Closed

Description

Bug report

Bug summary

Saving a jpg file via plt.savefig("plot.jpg") fails with a type error in matplotlib 2.2.0rc1. Did the required pillow version change? The docs say it's 2.0 or higher.

Code for reproduction

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt

plt.plot([1,3,1])
plt.savefig("plot.jpg")

Actual outcome

Traceback (most recent call last):
  File "D:\*******\GI_savefigjpg.py", line 16, in <module>
    plt.savefig("plot.jpg")
  File "c:\******\python-2.7.10.amd64\lib\site-packages\matplotlib\pyplot.py", line 710, in savefig
    res = fig.savefig(*args, **kwargs)
  File "c:\******\python-2.7.10.amd64\lib\site-packages\matplotlib\figure.py", line 2033, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "c:\******\python-2.7.10.amd64\lib\site-packages\matplotlib\backend_bases.py", line 2261, in print_figure
    **kwargs)
  File "c:\******\python-2.7.10.amd64\lib\site-packages\matplotlib\backends\backend_agg.py", line 582, in print_jpg
    return background.save(filename_or_obj, format='jpeg', **options)
  File "c:\******\python-2.7.10.amd64\lib\site-packages\PIL\Image.py", line 1665, in save
    save_handler(self, fp, filename)
  File "c:\******\python-2.7.10.amd64\lib\site-packages\PIL\JpegImagePlugin.py", line 691, in _save
    ImageFile._save(im, fp, [("jpeg", (0, 0)+im.size, 0, rawmode)], bufsize)
  File "c:\******\python-2.7.10.amd64\lib\site-packages\PIL\ImageFile.py", line 484, in _save
    e = Image._getencoder(im.mode, e, a, im.encoderconfig)
  File "c:\******\python-2.7.10.amd64\lib\site-packages\PIL\Image.py", line 430, in _getencoder
    return encoder(mode, *args + extra)
TypeError: integer argument expected, got float

Expected outcome

No error should occur.

Matplotlib version

  • Operating system: Windows 8.1
  • Matplotlib version: 2.2.0rc1
  • Matplotlib backend: Agg, also tested with %matplotlib inline
  • Python version: 2.7.10
  • Other libraries:
    import PIL
    print PIL.VERSION          # prints 1.1.7
    print PIL.PILLOW_VERSION   # prints 3.0.0
    

Activity

  1. dopplershift commented on Feb 28, 2018

    @dopplershift
    Contributor

    Interesting. Works for me on Python 3.6 and Pillow 5.0.

  2. ImportanceOfBeingErnest commented on Mar 1, 2018

    @ImportanceOfBeingErnest
    MemberAuthor

    Correct, using Pillow 5.0 (all other versions the same as above) this error does not happen. What is the conclusion? Which version of Pillow should the requirements state?

  3. changed the title [-]Type Error when saving jpg image[/-] [+]Type Error when saving jpg image (Pillow 3)[/+] on Mar 1, 2018
  4. ImportanceOfBeingErnest commented on Mar 1, 2018

    @ImportanceOfBeingErnest
    MemberAuthor

    If someone updates the requirements, the following should also be taken care of: It seems that for some old versions of six (six 1.2) matplotlib cannot be imported correctly, it works fine with six >=1.10. What is the lowest version of six that can be used?

  5. QuLogic commented on Mar 1, 2018

    @QuLogic
    Member

    The minimum supported six is 1.10, though I'm not sure why that's not mentioned in INSTALL.rst.

    The minimum Pillow version in the INSTALL.rst is 2.0, but it doesn't appear that we enforce that in code anywhere.

  6. anntzer commented on Mar 2, 2018

    @anntzer
    Contributor

    Given that pillow is, strictly speaking, not a dependency, I don't think we should error out if a too-old pillow is installed.

  7. QuLogic commented on Mar 2, 2018

    @QuLogic
    Member

    But we shouldn't enable the jpg saver if it's not a new enough Pillow.

  8. anntzer commented on Mar 2, 2018

    @anntzer
    Contributor

    I think it's just a matter of passing the figure size as ints, not floats (haven't checked, may be totally off).

  9. added this to the v2.2.1 milestone on Mar 2, 2018
  10. anntzer commented on Mar 4, 2018

    @anntzer
    Contributor

    Actually it's the figure dpi which needs to be an int. A quick bisection shows that the first version of Pillow that supports passing floats as dpi is 3.4.0.
    Casting dpi to int would likely result in slightly incorrect image sizes (e.g. as of now, setting figure.dpi to 99, 99.5 and 100 all result in different image sizes, which would no longer be the case if we do cast to float), so I'd favor just documenting pillow >=3.4 as dependency.

  11. tacaswell commented on Mar 12, 2018

    @tacaswell
    Member

    👍 to changing the documented minimum to 3.4. checking the PIL version and warning for < 3.4 and then casting to int (as per @efiring 's suggestion).

  12. modified the milestones: v2.2.1, on Mar 16, 2018
  13. added
    🌱 Good first issueOpen a pull request against these issues if there are no active ones!
    on Mar 26, 2018
  14. ImportanceOfBeingErnest commented on Apr 26, 2018

    @ImportanceOfBeingErnest
    MemberAuthor

    While I would agree that casting to int in dependence of which version of pillow is installed may be useful in some rare cases, it may on the other hand be undesired to have a different output depending on the pillow version. In addition, I'm not sure if it is worth cluttering the code of print_jpg and print_tif with it. At the end one can simply argue:
    "Matplotlib's functionality can be enhanced to be able to save jpg and tif pictures by using pillow >= 3.4."
    This would be a question of documenting the correct version and checking for that version in the code header.

    This is what I did in PR #11132

    Maybe it's worth noting that if you want to use Pillow with Python 3.6 you'd need to use Pillow >= 4.0 anyways. And also that all python versions that matplotlib supports (2.7, 3.4, 3.5, 3.6) are supported by pillow 4.0 as well. So there should not be any restriction in requiring pillow >= 3.4 at all.

  15. timhoffm commented on Apr 26, 2018

    @timhoffm
    Member

    Usually, I'm pro backwards compatibility. However, in this case I support @ImportanceOfBeingErnest.

    It will only affect a very specific group of people: The ones who use an old pillow and matplotlib and then only update matplotlib. To make it work again, they simply have to update pillow as well (would automatically happen if it was a hard dependency). They don't have to touch their own code to fix this. Given this, I wouldn't clutter the code.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Documentation🌱 Good first issueOpen a pull request against these issues if there are no active ones!

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions