Forum Archive

problems using Image.open with png file

DrNo152

The following code

from PIL import Image
img = Image.open('test.png')
img.show()

throws "IOError: broken data stream when reading image file". Reading the image from the camera roll works flawlessly, though:

import photos
img = photos.pick_image()
img.show()

The first image prints as

<PIL.PngImagePlugin.PngImageFile image mode=RGBA size 152x152 at 0xDFB438C>

the second one as

<Image.Image image mode=RGBA size 152x152 at 0xDFB460C>

But PIL.PngImagePlugin.PngImageFile is a subclass of Image.Image and things like img.show() should work in both cases. In fact, nothing seems to work with the first img.

A quick peruse of google seems to suggest that this might be caused by a libjpeg version problem.

PS: It used to work with Pythonista 1.3.

ccc

This code seems to work quite consistently:

import Image, photos
pngFileName = 'test.png'

def img_info(img):
    print(img)
    print(img.format, img.mode, img.size, img.palette, img.info)

img = photos.pick_image()
img_info(img)
img.save(pngFileName)  # write out the image as .png

img = Image.open(pngFileName)  # read in the image
img_info(img)
img.show()  # This might take a few seconds

Is the image.format of your image really set to 'PNG'?

Can you use the code above to create a new .png file that you can successfully .show()?

DrNo152

Yes, img.format is set to 'PNG'.

Your code, however, is a working fine and can be used to create a new .png file that can successfully be dealt with. Kind of weird, though. Why wouldn't it work if saved directly by a dropbox sync?

Thanks for the workaround, ccc.

ccc

If you create a gist of the Dropbox code then perhaps others in the Forum can discover why it is not working.