2

I'm post a jpg picture to my website (build on django), but I got "OSError at url XXXX/XXX broken data stream when reading image file" when I use pillow to deal with it

it happens when run the code in server :

if request.FILES:
    img = request.FILES['img']
    ftype = img.content_type.split('/')[1]
    image = Image.open(img)
    imagefit = ImageOps.fit(image, (200, 200), Image.ANTIALIAS)
    fpath = MEDIA_ROOT+'avatar/'+user.username+'.'+ftype
    getpath = 'avatar/'+user.username+'.'+ftype
    imagefit.save(fpath,ftype)

and the Traceback is:

view:
imagefit = ImageOps.fit(image, (200, 200), Image.ANTIALIAS) 
/PIL/ImageOps.py in fit:
(leftSide, topSide, leftSide + cropWidth, topSide + cropHeight) 
/PIL/Image.py in crop:
self.load() 
/PIL/ImageFile.py in load:
raise_ioerror(e) 
/PIL/ImageFile.py in raise_ioerror:
raise IOError(message + " when reading image file") 

message   'broken data stream'
error   -2

img <InMemoryUploadedFile: 169902.jpg (image/jpeg)>

ftype   'jpeg'
image   <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1650x2550 at 0xB5CCBECC>

I'm using ubuntu 13.10(32bit), python3, pillow2.4.0, and I've install libjpeg8-dev, python3-dev python3-imaging and re-install pillow (in virtualenv), but not fixed

stackniu
  • 31
  • 3
  • It seems that depending on the features used by your particular JPEG file, you need to either up- or downgrade `libjpeg`. [This answer](http://stackoverflow.com/a/7861423/1599111) indicates that *downgrading* to `libjpeg` **6b** could help (and reinstall `PIL` again). You could also try to find out what [features](http://en.wikipedia.org/wiki/Libjpeg#Version_roadmap) your JPEG file uses, e.g. with ImageMagick's [`identify`](http://www.imagemagick.org/script/identify.php). – Lukas Graf May 03 '14 at 14:13
  • already tried, but nothing changed... – stackniu May 03 '14 at 16:10

1 Answers1

2

Try installing libjpeg and then reinstall pillow:

sudo apt-get install libjpeg8 libjpeg8-dev

pip install --force-reinstall Pillow

If the version is unavailable try the following to find what versions are available:

apt-cache search libjpeg
Chris Montanaro
  • 16,948
  • 4
  • 20
  • 29