You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Type Error when saving jpg image (Pillow 3) #10633
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
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?
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?
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.
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.
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.
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.
Activity
dopplershift commentedon Feb 28, 2018
Interesting. Works for me on Python 3.6 and Pillow 5.0.
ImportanceOfBeingErnest commentedon Mar 1, 2018
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?
[-]Type Error when saving jpg image[/-][+]Type Error when saving jpg image (Pillow 3)[/+]ImportanceOfBeingErnest commentedon Mar 1, 2018
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 ofsixthat can be used?QuLogic commentedon Mar 1, 2018
The minimum supported
sixis 1.10, though I'm not sure why that's not mentioned inINSTALL.rst.The minimum Pillow version in the
INSTALL.rstis 2.0, but it doesn't appear that we enforce that in code anywhere.anntzer commentedon Mar 2, 2018
Given that pillow is, strictly speaking, not a dependency, I don't think we should error out if a too-old pillow is installed.
QuLogic commentedon Mar 2, 2018
But we shouldn't enable the jpg saver if it's not a new enough Pillow.
anntzer commentedon Mar 2, 2018
I think it's just a matter of passing the figure size as ints, not floats (haven't checked, may be totally off).
anntzer commentedon Mar 4, 2018
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.
tacaswell commentedon Mar 12, 2018
👍 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).
ImportanceOfBeingErnest commentedon Apr 26, 2018
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_jpgandprint_tifwith 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.
timhoffm commentedon Apr 26, 2018
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.