Cast error details: Unable to cast Python instance to C++ type

I want to convert .png files to .jpeg files with random quality factor. But it returns this error:

image::write_file() Expected a value of type ‘str’ for argument ‘_0’ but instead found type ‘BufferedWriter’.
Position: 0
Value: <_io.BufferedWriter name=’/content/Results0.jpeg’>
Declaration: image::write_file(str _0, Tensor _1) → ()
Cast error details: Unable to cast Python instance to C++ type (compile in debug mode for details)

Here’s code:

import torch
import torchvision.transforms as T
import torchvision.io as io
import os
from PIL import Image
import random


PATH = '/content/input'
SAVE_PATH = '/content/Results'

img_files = [os.path.join(PATH, imgs) for imgs in os.listdir(PATH)]
j = 0
for i in img_files:
    
    nums = []
    image = Image.open(i)
    image = image.convert('RGB')
    image = T.ToTensor()(image)
    image = image.type(torch.uint8)
    
    r_num = random.randint(10, 96)
    nums.append(r_num)
  
    io.write_jpeg(input = image, filename = open(SAVE_PATH + f'{j}.jpeg', 'wb'), quality = r_num)
    j += 1