'bytes' object has no attribute 'save'

I’m trying to use io to convert an image from .bmp to .gif. My python code is below. Further below is the error.

import cv2
import io
from io import BytesIO

with open(“output.bmp”, “rb”) as img:
imageToFilter = img.read()

with BytesIO() as image:
imageToFilter.save(image, format = “GIF”)
image.seek(0)
my_gif = Image.open(image)

myImage2 = smoothImage(my_gif)
cv2.imwrite(“output1.bmp”, myImage2)

Traceback (most recent call last):
File “test.py”, line 15, in
imageToFilter.save(image, format = “GIF”)
AttributeError: ‘bytes’ object has no attribute ‘save’

In what you’re trying to do, BytesIO works like open. So like with BytesIO(b'some binary image data') as image. But you’re not opening anything, you’re doing with BytesIO() as image, i.e. youre aliasing the actual BytesIO object as image, not the thing you’re opening using the BytesIO object.

I have two functions. One of them does object recognition (i.e. detect a person and draw rectangle around them). The other one applies a filter to the person’s image. The problem is that the first function only accepts .bmp files while the latter accepts .gif files. So I want to convert the image from .bmp to .gif after running it through the first function before running it through the second.

I get that, and there may be further changes needed, but what I said still stands: you can’t use save unless you have something to save: BytesIO doesn’t have a save attribute, you need something there to save, that’s the thing that will have a save attribute, at the minute you just have the BytesIO object itself