unice
unice

Reputation: 2842

can PIL open an image using pyqt4 resource file?

Can PIL open an image using pyqt4 resource file?

from PIL import Image, ImageWin
import res_rc #resource file

image = Image.open(":/images/image.png")
dim = ImageWin.Dib(image)

I'm getting this error

IOError: [Errno 22] invalid mode ('rb') or filename :/images/image.png'

Upvotes: 1

Views: 563

Answers (1)

ekhumoro
ekhumoro

Reputation: 120638

To read an image file from a resource, open it with a QFile and pass the raw data to a file-like object that can be used by PIL:

from PyQt4.QtCore import QFile
from cStringIO import StringIO
from PIL import Image, ImageWin
import res_rc

stream = QFile(':/images/image.png')
if stream.open(QFile.ReadOnly):
    data = stream.readAll()
    stream.close()
    image = Image.open(StringIO(data))
    dim = ImageWin.Dib(image)

Note that resources are designed to be compiled into the application, and so they are strictly read-only.

Upvotes: 2

Related Questions