moviepy 库中的 ImageSequenceClip 给了我例外:ImageSequenceClip 要求所有图像大小相同

问题描述 投票:0回答:1

这是我的职责

def create_image_sequence(images, fps=None, aspectRatio=None, resolution=None):
    '''
    Takes request.FILE images and makes video sequence it finnaly transfers to gif
    
    DONT FORGET TO DELETE GIF
    '''
    
    #STORING IMAGES INTO TMP FOLDER
    imagesTMPpaths = []
    
    for key, value in images.items():
        ext = photo_extension(value)
        
        tmp_image = tempfile.NamedTemporaryFile(delete=False, suffix=ext)
        tmp_image.write(io.BytesIO(value.read()).read())
        tmp_image.close()
        
        imagesTMPpaths.append(tmp_image.name)
        print(f'{key}: {value}')
    

    #GETTING SMALLEST WIDTH --------
    
    images_sizes = []
    
    for image_tmp_path in imagesTMPpaths:
        
        with Image.open(image_tmp_path) as img:
            
            width, height = img.size #returns duplets
            images_sizes.append([width*height, width, height])
        
    images_sizes.sort() #it automaticly sorts by its first value
    print('xxxxxxx\nSize resizing to: ', images_sizes[0], '\nxxxxxxx')
    
    #GETTING SMALLEST WIDTH end --------
    
    
    #RESIZE IMAGES TO SMALLEST ONE --------
    print('\nresizing proces\n-------')
    for image_tmp_path in imagesTMPpaths:
        #https://stackoverflow.com/a/4271003 (resize with black fillers/padding)
        
        with Image.open(image_tmp_path) as img:
            print('\noriginal size: ', img.size)

            imgModified = ImageOps.pad(Image.open(image_tmp_path), (images_sizes[0][1], images_sizes[0][2]), color='black')
            
            imgModified.save(image_tmp_path)
            print('modified size: ', imgModified.size)
            print('PASSED: ', imgModified.size == (images_sizes[0][1], images_sizes[0][2]), '\n')
        
        
    # Verify all images have been resized correctly
    for image_tmp_path in imagesTMPpaths:
        with Image.open(image_tmp_path) as img:
            img = Image.open(image_tmp_path)
            print('size: ', img.size)
            print('PASSED: ', img.size == (images_sizes[0][1], images_sizes[0][2]), '\n')
        
    
    print(imagesTMPpaths)
    print(type(fps))
    print(f'fps: --{fps}--')
    print(fps not in [None,''])
    
    
        
    # Creating image sequence clip
    if fps in [0, None, '']:
        fps = len(imagesTMPpaths)
    else:
        fps = int(fps)
    
    imageSequence = ImageSequenceClip(imagesTMPpaths, fps=fps)
    
    
        
    if resolution  not in [None,'no change']:
        width, height = map(int, resolution.split('x')) #split will split around X and map makes two outputs int
        print('Resolution allowed:', resolution)
        print(width, height)
        imageSequence = imageSequence.resize(newsize=(width, height))
        print("Resolution updated. New size:", imageSequence.size)
    
    # Adjust aspect ratio if specified
    if aspectRatio not in [None,'no change']:
        print('Aspect ratio allowed:', aspectRatio)
        x, y = map(int, aspectRatio.split(':'))
        
        imageSequence = imageSequence.resize(newsize=(imageSequence.size[0], int(imageSequence.size[0] * y / x)))
        print("Aspect ratio updated. New size:", imageSequence.size)
    
    
    #final GIF
    tmp_gif = tempfile.NamedTemporaryFile(delete=False, suffix='.gif')
    
    
    imageSequence.write_gif(tmp_gif.name, program = 'ffmpeg')#WRITING INTO --> tmp_gif,  when using same tmp_gif path!
    imageSequence.close()
    
    tmp_gif.close()
    
    for image_tmp_path in imagesTMPpaths:
        os.remove(image_tmp_path)
    
    return tmp_gif.name

一切正常,直到出现

imageSequence = ImageSequenceClip(imagesTMPpaths, fps=fps)
即使我在控制台中验证图像具有相同的宽度和长度后,它仍然会出现异常。

这是控制台的输出:

####-web-1  | 0: xddd.png
####-web-1  | 1: Candlera-7_c582e094-1714-43df-bc18-a56e8277bd91.webp
####-web-1  | xxxxxxx
####-web-1  | Size resizing to:  (111, 194) 
####-web-1  | xxxxxxx
####-web-1  | 
####-web-1  | original size:  (111, 194)
####-web-1  | modified size:  (111, 194)
####-web-1  | PASSED:  True
####-web-1  |
####-web-1  |
####-web-1  | original size:  (700, 700)
####-web-1  | modified size:  (111, 194)
####-web-1  | PASSED:  True
####-web-1  |
####-web-1  | size:  (111, 194)
####-web-1  | PASSED:  True
####-web-1  |
####-web-1  | size:  (111, 194)
####-web-1  | PASSED:  True

然后遇到异常:ImageSequenceClip 要求所有图像大小相同

正如您在代码中看到的,我尝试将图像大小调整为最小图像尺寸,但与原始图像的比例相同,所以我使用了这个:

imgModified = ImageOps.pad(Image.open(image_tmp_path), (images_sizes[0][1], images_sizes[0][2]), color='black')

之后,我在控制台中验证了两次,如果尺寸正确并且控制台的输出显示它是相同的。

我尝试擦除docker中的整个

tmp
文件夹,但这没有帮助我尝试重建整个容器,但没有帮助。

python python-3.x django moviepy
1个回答
0
投票

好吧,我认为我发布的这段代码很愚蠢,为什么要生成视频?像这样浪费宝贵的资源,我的傻屁股。[![在此处输入图像描述]

我只会使用我已有的枕头库,它在硬件上会轻得多。

funny gif

© www.soinside.com 2019 - 2024. All rights reserved.