Torchvision save image creates a messed up image

Hello I am trying to convert avi files into separate png files. My code is here:

for i in tqdm(range(num_videos)):
    if not os.path.isdir(args.VideoDir):
        print("Input folder does not exist!")
        break
    if not os.path.isdir(args.OutputDir):
        os.mkdir(args.OutputDir)
    path = args.VideoDir + videos[i]

    path = args.VideoDir + videos[i]
    images = io.read_video(path)[0]
    if images.shape[0] < args.numframes:
        for n in range(images.shape[0]):
            folder_path = args.OutputDir + "scene_" + str(1000 + i)
            if not os.path.isdir(folder_path):
                os.mkdir(folder_path)
            image = images[n]
            image = torch.reshape(image, (image.shape[2], image.shape[0], image.shape[1]))
            image_path = folder_path + "/" + "col_high_" + str(n).zfill(4) + ".png"
            utils.save_image(image.float(), fp=image_path)
    else:
        for n in range(args.numframes):
            folder_path = args.OutputDir + "scene_" + str(1000 + i)
            if not os.path.isdir(folder_path):
                os.mkdir(folder_path)
            image = images[n]
            image = image.view(image.shape[2], image.shape[0], image.shape[1])
            image_path = folder_path + "/" + "col_high_" + str(n).zfill(4) + '.png'
            utils.save_image(image.type(torch.float32), image_path)
    if not args.keep_video:
        os.remove(path)

The output image ends up being totally wrong. I have tried to do the same with mp4 files but the result doesn’t change. Here is an example image:
col_high_0012
Any help would be appreciated. Thanks!

The reshape or view operation will interleave the image in case you want to transform the memory layout from channels-last to channels-first.
Use tensor.permute instead, which permutes the dimensions.

1 Like

I tried that but it is still giving me messed up images.

Could you upload an example image here so that we could reproduce it?

col_high_0000
Here you go.