15

I've been handed a list of files from the backend of an application that are supposed to be jpeg files. However for the life of me I haven't been able to convert them into PIL image objects. When I call

str(curimg)

I get back:

<type 'str'>

. I have tried using open(), .read, io.BytesIO(img.read() and also doing nothing to it, but it keeps seeing it as a string. When i print the string, I get unrecognizable characters. Does anyone know how to tell python how to intepret this string as a jpeg and convert it into a pill image where I can call .size and np.array on?

mt88
  • 2,855
  • 8
  • 24
  • 42
  • 1
    Does [this question](http://stackoverflow.com/questions/1664861/how-to-create-an-image-from-a-string-in-python) have the answer you seek? – Octopus Aug 07 '15 at 17:30
  • Did you try [`Image.fromstring()`](http://effbot.org/zone/pythondoc-compact.htm#Image.fromstring-function) ? – Anand S Kumar Aug 07 '15 at 17:33
  • ^^ The only thing is I dont know how to get the size that fromstring() needs? All i'm given is a string. – mt88 Aug 07 '15 at 17:38

4 Answers4

25
from PIL import Image
import io
Image.open(io.BytesIO(image))

Note:

If image is on the web; you need to download it first.

import requests
image = requests.get(image_url).content  #download image from web

And then pass it to io module.

io.BytesIO(image)

If image is in your hd; you can open directly with PIL.

Image.open('image_file.jpg')  #image in your HD
Diego Suarez
  • 901
  • 13
  • 16
13

You should be able to pass a StringIO object to PIL and open it that way.

ie:

from PIL import Image
import StringIO
tempBuff = StringIO.StringIO()
tempBuff.write(curimg)
tempBuff.seek(0) #need to jump back to the beginning before handing it off to PIL
Image.open(tempBuff)
Ian St.John
  • 311
  • 3
  • 6
  • 4
    I'm using python 3.6 and I am getting this error `"StringIO cannot be used to open an image. " ValueError: StringIO cannot be used to open an image. Binary data must be used instead.` if I use this method – Suraj S Jain Apr 29 '20 at 17:58
2

For me, none of the solutions above worked.

I finally managed to read the string properly like this:

from PIL import Image
img = Image.frombytes('RGB', (640, 480), img_str, 'raw')

To test it, you can do something like

image = Image.open("some.png")
print(image.mode, image.size) # OUT: 'RGB' (640, 480)
image = Image.frombytes('RGB', (640, 480), image.tobytes(), 'raw')
image.show()
VanDavv
  • 836
  • 2
  • 13
  • 38
  • I just came across this. I am having a similar issue. If you still have a fresh knowledge of this, can you explain some things to me please? – CCCC Sep 16 '21 at 08:05
  • I don't use PIL anymore, but I can try to help – VanDavv Sep 17 '21 at 08:59
  • Hello @Vandavv, thanks for your willingness to help. I've been able to resolve it. The pictures now display. But I'm having another issue. I wanna update the retrieved image which is displayed in jpg format. How do I go about it plz? The image label (i.e the qpixmap label) is self.passlb So in my sql update statement, I tried: ```sql = (my update statement here) values(self.fullname.text(), self.passlb.pixmap() ...)``` It didn't work. Any clue about the method I should use in updating a qpixmap label? – CCCC Sep 17 '21 at 21:50
  • 1
    @VanDavv, thank you so much! I have a PpmImageFile object but couldn't figure out how to convert it. Got some clue [here](https://stackoverflow.com/questions/65597315/how-to-return-a-list-of-pil-image-files-from-fastapi-response) but couldn't figure out everything. – CaTx Oct 12 '22 at 13:15
0

@CEO (per this comment) I don't know how what role SQL plays here, and I'm not exactly sure what you're trying to achieve, but I recall I had some issues and this is what works for my case, hope it helps

    frame = self._rawNode.display_frame.copy()
    width = int(self.customLayout.geometry().width())
    height = int(frame.shape[0] * (width / frame.shape[1]))
    display_frame = cv2.cvtColor(cv2.resize(frame, (width, height)), cv2.COLOR_BGR2RGB)
    qImg = QtGui.QImage(display_frame.data, width, height, 3 * width, QtGui.QImage.Format_RGB888)
    self.pixmap = QtGui.QPixmap(qImg)
    self.Imagelabel.setPixmap(self.pixmap)
VanDavv
  • 836
  • 2
  • 13
  • 38
  • hello @vanDavv, I've resolved it. It's fine now. I'm presently having a different issue I'm trying to resolve but so unfortunate I've been denied from asking questions here. – CCCC Sep 21 '21 at 11:20
  • No worries, happy to help whenever I can – VanDavv Sep 23 '21 at 07:26