我从应用程序后端收到了一个文件列表,它们应该是jpeg文件。但是我怎么也无法将它们转换为PIL图像对象。当我调用:
str(curimg)
我得到了回复:
<type 'str'>
我已经尝试使用open()、.read、io.BytesIO(img.read())以及什么也不做,但它仍然将其视为字符串。当我打印该字符串时,我得到了无法识别的字符。有人知道如何告诉Python将此字符串解释为JPEG并将其转换为PIL图像,以便我可以调用.size和np.array吗?
我从应用程序后端收到了一个文件列表,它们应该是jpeg文件。但是我怎么也无法将它们转换为PIL图像对象。当我调用:
str(curimg)
我得到了回复:
<type 'str'>
我已经尝试使用open()、.read、io.BytesIO(img.read())以及什么也不做,但它仍然将其视为字符串。当我打印该字符串时,我得到了无法识别的字符。有人知道如何告诉Python将此字符串解释为JPEG并将其转换为PIL图像,以便我可以调用.size和np.array吗?
from PIL import Image
import io
Image.open(io.BytesIO(image))
注意:
如果图片在网络上,您需要先下载它。
import requests
image = requests.get(image_url).content #download image from web
然后将其传递给io模块。
io.BytesIO(image)
如果图像已经在您的硬盘上,您可以直接使用PIL打开。Image.open('image_file.jpg') #image in your HD
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)
对我来说,以上解决方案都没有起作用。
我最终通过以下方法成功地正确读取了字符串:
from PIL import Image
img = Image.frombytes('RGB', (640, 480), img_str, 'raw')
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()
values(self.fullname.text(),
self.passlb.pixmap()
...)```
但它没有起作用。你知道我应该使用什么方法来更新qpixmap标签吗?
- CCCC@CEO(根据this的评论)我不知道SQL在这里扮演什么角色,也不确定你想要实现什么,但我记得我曾经遇到过一些问题,以下是解决方法,希望能对你有所帮助
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)
Image.fromstring()
吗? - Anand S Kumar