Meghdeep Ray Meghdeep Ray - 1 year ago 227
Python Question

Python3 Django1.9 PIL Issue involving Saving Image File with io.BytesIO - Works in Terminal but not in the Server

Python : 3.4.3

Django : 1.9.7

Exception Type: TypeError

Exception Value: descriptor 'fileno' of '_io._IOBase' object needs an argument

Exception Location: /usr/lib/python3/dist-packages/PIL/ in _save, line 454

This is the code I have tested in the terminal -

import urllib.request
from PIL import Image
from io import BytesIO

url = ''
i =
img_file = BytesIO(), 'JPEG')

The code works perfectly well in the terminal but as soon as it's tested on the Django server it gives me these errors -

File "PATH/", line 124, in pil_to_django, 'JPEG')
File "/usr/lib/python3/dist-packages/PIL/", line 1468, in save
save_handler(self, fp, filename)
File "/usr/lib/python3/dist-packages/PIL/", line 579, in _save
ImageFile._save(im, fp, [("jpeg", (0,0)+im.size, 0, rawmode)], bufsize)
File "/usr/lib/python3/dist-packages/PIL/", line 454, in _save
fh = fp.fileno()
TypeError: descriptor 'fileno' of '_io._IOBase' object needs an argument

The code running in the server is in
which is called from

def pil_to_django(image, format="JPEG"):
img_file = io.BytesIO, 'JPEG')
return ContentFile(img_file.getvalue())

pil_image =
django_file = pil_to_django(pil_image)

Answer Source

You forgot to instantiate BytesIO class. Change img_file = io.BytesIO to img_file = io.BytesIO()

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download