Difficulty with PIL - TypeError: expected str, bytes or os.PathLike object, not Image
Difficulty with PIL - TypeError: expected str, bytes or os.PathLike object, not Image
我需要将裁剪后的图像作为流或 URL 传递给 Azure Computer Vision API。到目前为止,为了测试,我裁剪图像并将其存储在文件夹中并读取它。现在我需要直接读取裁剪后的图像,而不是将其实际存储在我的内存中。这是我当前的代码。
img = Image.open(path)
img2 = img.crop((bbox[-1][0], bbox[-1][1], bbox[-1][4], bbox[-1][5]))
try:
img2.save("C:/Repos/Dataset/Image_1/temp.jpg")
except SystemError:
#other code
else:
width, height = img2.size
if (width >= 50 and height >= 50):
with open(img2, 'rb') as bg_image:
colour_analysis = computervision_client.analyze_image_in_stream(bg_image, remote_image_features)
bg_color = format(colour_analysis.color.dominant_color_background)
bgcol.append(bg_color)
我需要把它改成这样:
img = Image.open(path)
img2 = img.crop((bbox[-1][0], bbox[-1][1], bbox[-1][4], bbox[-1][5]))
try:
width, height = img2.size
if (width >= 50 and height >= 50):
with open(img2, 'rb') as bg_image:
colour_analysis = computervision_client.analyze_image_in_stream(bg_image, remote_image_features)
bg_color = format(colour_analysis.color.dominant_color_background)
bgcol.append(bg_color)
except:
#some code
我在上面的代码中遇到了这个错误:
with open(img2, 'rb') as bg_image:
TypeError:应为 str、bytes 或 os.PathLike 对象,而不是 Image
如何将 Image 对象转换为流?
Image.open()
为您提供 pillow.Image
未压缩数据 - 每个像素都是单独的项目。但是 open()
需要文件名或访问具有字节数据的类文件对象(压缩为 jpg
、png
等)。
您可以使用 io.BytesIO
在内存中创建类似文件的对象,将 pillow.image
保存到此文件(它将压缩它,即 png
或 jpg
) 然后用它来阅读。
import io
buffer = io.BytesIO() # create file in memory
img2.save(buffer, 'jpeg') # save in file in memory - it has to be `jpeg`, not `jpg`
buffer.seek(0) # move to the beginning of file
bg_image = buffer # use it without `open()`
我需要将裁剪后的图像作为流或 URL 传递给 Azure Computer Vision API。到目前为止,为了测试,我裁剪图像并将其存储在文件夹中并读取它。现在我需要直接读取裁剪后的图像,而不是将其实际存储在我的内存中。这是我当前的代码。
img = Image.open(path)
img2 = img.crop((bbox[-1][0], bbox[-1][1], bbox[-1][4], bbox[-1][5]))
try:
img2.save("C:/Repos/Dataset/Image_1/temp.jpg")
except SystemError:
#other code
else:
width, height = img2.size
if (width >= 50 and height >= 50):
with open(img2, 'rb') as bg_image:
colour_analysis = computervision_client.analyze_image_in_stream(bg_image, remote_image_features)
bg_color = format(colour_analysis.color.dominant_color_background)
bgcol.append(bg_color)
我需要把它改成这样:
img = Image.open(path)
img2 = img.crop((bbox[-1][0], bbox[-1][1], bbox[-1][4], bbox[-1][5]))
try:
width, height = img2.size
if (width >= 50 and height >= 50):
with open(img2, 'rb') as bg_image:
colour_analysis = computervision_client.analyze_image_in_stream(bg_image, remote_image_features)
bg_color = format(colour_analysis.color.dominant_color_background)
bgcol.append(bg_color)
except:
#some code
我在上面的代码中遇到了这个错误: with open(img2, 'rb') as bg_image: TypeError:应为 str、bytes 或 os.PathLike 对象,而不是 Image
如何将 Image 对象转换为流?
Image.open()
为您提供 pillow.Image
未压缩数据 - 每个像素都是单独的项目。但是 open()
需要文件名或访问具有字节数据的类文件对象(压缩为 jpg
、png
等)。
您可以使用 io.BytesIO
在内存中创建类似文件的对象,将 pillow.image
保存到此文件(它将压缩它,即 png
或 jpg
) 然后用它来阅读。
import io
buffer = io.BytesIO() # create file in memory
img2.save(buffer, 'jpeg') # save in file in memory - it has to be `jpeg`, not `jpg`
buffer.seek(0) # move to the beginning of file
bg_image = buffer # use it without `open()`