-
Notifications
You must be signed in to change notification settings - Fork 93
Closed
Labels
Description
Python's Pillow image framework doesn't work properly with pyfakefs. When trying to save an image on the fake filesystem, the following exception is raised:
Traceback (most recent call last):
File "/home/krasi/dev/code/tests/test.py", line 160, in test_pyfakefs_and_PIL
img.save('/some_target_folder/target_image.jpg')
File "/home/krasi/programs/anaconda3/envs/tensorflow3/lib/python3.6/site-packages/PIL/Image.py", line 2102, in save
save_handler(self, fp, filename)
File "/home/krasi/programs/anaconda3/envs/tensorflow3/lib/python3.6/site-packages/PIL/JpegImagePlugin.py", line 766, in _save
ImageFile._save(im, fp, [("jpeg", (0, 0) + im.size, 0, rawmode)], bufsize)
File "/home/krasi/programs/anaconda3/envs/tensorflow3/lib/python3.6/site-packages/PIL/ImageFile.py", line 528, in _save
s = e.encode_to_file(fh, bufsize)
OSError: [Errno 9] Bad file descriptor
Here's a sample PyTest test to reproduce:
import os
from PIL import Image
def test_pyfakefs_and_PIL(fs):
# Create some image
img = Image.new('RGB', (60, 30), color = 'red')
# Create target folder
os.makedirs('/some_target_folder', exist_ok=True)
assert os.path.exists('/some_target_folder')
# Try to save it
img.save('/some_target_folder/target_image.jpg')
assert os.path.exists('/some_target_folder/target_image.jpg')
Note that the issue reproduces only when saving an image as JPEG (i.e. with '.jpg' extension) and doesn't reproduce when saving the image as PNG.
Environment:
Linux-4.15.0-96-generic-x86_64-with-debian-buster-sid (18.04.2 LTS (Bionic Beaver) )
Python 3.6.6 |Anaconda custom (64-bit)| (default, Jun 28 2018, 17:14:51) [GCC 7.2.0]
pyfakefs 4.0.2
Pillow 7.0.0
To install Pillow:
pip install Pillow
Potentially related StackOverflow issues: