TypeError:memoryview:需要一个类似字节的对象,而不是python 3.5中的'JpegImageFile'

时间:2019-02-26 18:21:37

标签: python

以下代码给我一个错误:

%matplotlib inline

错误是:

def save(self): self.filePath, _ = QFileDialog.getOpenFileName(self, "Open File ", "", "JPEG(*.JPEG *.jpeg *.JPG *.jpg)") img =Image.open(self.filePath) conn.execute("INSERT INTO DriverInfo(driverImg)VALUES(?)", repr(memoryview(img))) conn.commit()

1 个答案:

答案 0 :(得分:0)

要获取组成图像的字节,请执行以下操作:

from io import BytesIO
img = Image.open('1x1.jpg')

# Create a buffer to hold the bytes
buf = BytesIO()

# Save the image as jpeg to the buffer
img.save(buf, 'jpeg')

# Rewind the buffer's file pointer
buf.seek(0)

# Read the bytes from the buffer
image_bytes = buf.read()

# Close the buffer
buf.close()