修改图片\视频的分辨率和格式

目录

1.修改图片格式

2.修改图片分辨率

3.改变视频格式

4.改变视频分辨率

5.裁剪视频

6.文件夹文件的批量操作


背景:对于素材的测试,经常需要测试不同格式的图片/视频和不同分别分辨率的图片/视频,为了测等价类边界值则需要的素材格式和分辨率非常苛刻,想要便捷的获取到自己需要的测试数据,则必须自己去创造这些数据。

1.修改图片格式

'''修改图片格式'''
# 入参为fileName文件名称和newFormat新格式
def format(fileName,newFormat):
    im = Image.open(fileName)
    newName = fileName.split('.')[0]+'.'+newFormat
    image = im.convert('RGB')
    image.save(newName)
    # os.remove(fileName)
format('a.jpg','png')

效果图如下:


运行后可将图片格式修改,这种方法不是单单修改文件后缀,而是修改格式,所以图片大小是会改变的。

2.修改图片分辨率

'''修改图片分辨率'''

def resolution(fileName,size):
    image = Image.open(fileName)
    resized_image = image.resize(size,Image.ANTIALIAS)
    resized_image.save(fileName.split('.')[0] + '_1.' + fileName.split('.')[-1])
resolution("a.png",(500,500))

 效果图如下:

 运行后可将图像进行拉伸或压缩,图片的像素修改了,图片大小也会改变的。

 

3.改变视频格式

'''改变视频格式'''

# #.mp4 *.wmv *.rm *.avi *.flv *.webm *.wav *.rmvb *.mpg  *.mov
def format(fileName, newFormat):
    os.system(f'ffmpeg -i {fileName}  {fileName.split(".")[0]}.{newFormat}')

videoFormat.format('a.avi', 'mp4')

 效果图如下:


视频改变格式后会进行重新抽帧作为前导图,所以封面可能是不一样,但是内容一样。 

注:ffmpeg需要下载并加入到path路径下,下载地址如下:
https://ffmpeg.org/
下载教程和放入path路径地址如下:
https://blog.youkuaiyun.com/yinshipin007/article/details/130650738

4.改变视频分辨率

'''修改分辨率'''

# 视频地址、输出目录、宽度、高度,码率
def resolution(video_name: str, output_dir: str, size:tuple, bit_rate=2000):
    ext = os.path.basename(video_name).strip().split('.')[-1]
    # if ext not in ['mp4']:
    #     raise Exception('format error')
    newVideoName = os.path.join(
        output_dir, '{}.{}'.format(
            uuid.uuid1().hex, ext))
    ff = FFmpeg(inputs={'{}'.format(video_name): None}, outputs={
        newVideoName: '-s {}*{} -b {}k'.format(size[0],size[1], bit_rate)})
    ff.run()
    return newVideoName

videoFormat.resolution('a.mp4','',(500,500))

效果图如下:

5.裁剪视频

  入参:文件名,裁剪长度

    '''裁剪视频'''

    def cropping(fileName,length,deposit=None):
        result = subprocess.Popen(
            ['ffprobe', '-v', 'error', '-show_entries', 'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1',fileName], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        output = result.stdout.read()  # 输出视频的时长信息print('视频时长:', float(output))
        videoTime = float(output)

        if(videoTime>length):
            clip = VideoFileClip(fileName).subclip(0,length)
            new_file =  '1.mp4'
            clip.write_videofile(new_file)
        else:
            return

        if(videoTime>2*length):
            clip = VideoFileClip(fileName).subclip(length,2*length)
            new_file =  '2.mp4'
            clip.write_videofile(new_file)
        else:
            return

        if(videoTime>3*length):
            clip = VideoFileClip(fileName).subclip(2*length,3*length)
            new_file =  '3.mp4'
            clip.write_videofile(new_file)
        else:
            return

        if(videoTime>4*length):
            clip = VideoFileClip(fileName).subclip(3*length,4*length)
            new_file =  '4.mp4'
            clip.write_videofile(new_file)
        else:
            return

        if(videoTime>5*length):
            clip = VideoFileClip(fileName).subclip(4*length,5*length)
            new_file =  '5.mp4'
            clip.write_videofile(new_file)
        else:
            return

        if(videoTime>6*length):
            clip = VideoFileClip(fileName).subclip(5*length,6*length)
            new_file =  '6.mp4'
            clip.write_videofile(new_file)
        else:
            return

        if(videoTime>7*length):
            clip = VideoFileClip(fileName).subclip(6*length,7*length)
            new_file =  '7.mp4'
            clip.write_videofile(new_file)
        else:
            return

        if(videoTime>8*length):
            clip = VideoFileClip(fileName).subclip(7*length,8*length)
            new_file =  '8.mp4'
            clip.write_videofile(new_file)
        else:
            return

        if(videoTime>9*length):
            clip = VideoFileClip(fileName).subclip(8*length,9*length)
            new_file =  '9.mp4'
            clip.write_videofile(new_file)
        else:
            return

        if(videoTime>10*length):
            clip = VideoFileClip(fileName).subclip(9*length,10*length)
            new_file =  '10.mp4'
            clip.write_videofile(new_file)
        else:
            return

cropping('a.mp4',12)

效果图如下

 把原始图片裁剪为每个12s的视频,且最多裁剪10个视频。可根据自己需要进行简单修改代码。
视频的长度虽然是一样的,但是大小还是有些差异的。

6.文件夹文件的批量操作

上面这些操作如果想大量操作,数据源可以从文件夹中获取,如:
 

for root, dirs, files in os.walk(file_in):
    for file in files:
        print(file)
        #对文件的操作

《修改图片\视频的分辨率和格式》篇结束,欢迎去我的主页查看其它技术类文章~

<think>好的,我现在需要帮用户解决如何使用FFmpeg修改图片分辨率的问题。首先,我得回忆一下FFmpeg的基本用法,特别是处理图片的部分。用户可能对FFmpeg不太熟悉,所以需要详细但清晰的步骤。 首先,确定用户的需求:修改图片分辨率。这可能涉及到缩放图片到特定尺寸,或者调整宽高比。FFmpeg处理图片通常使用`-vf scale`滤镜,这是关键点。但需要注意保持宽高比的问题,如果用户不希望图片变形的话。 接下来,基本命令的结构应该是怎样的?比如输入文件、输出文件、缩放参数。然后,可能需要解释各个参数的含义,比如`-i input.jpg`是输入,`-vf scale=宽度:高度`是缩放滤镜。这里要醒用户替换实际的文件名尺寸。 然后,考虑是否需要保持宽高比。如果用户只指定宽度或高度,另一边可以自动计算,比如`scale=640:-1`,但要注意某些情况下可能需要使用强制宽高比的参数,比如`force_original_aspect_ratio`。这时候可能需要给出示例,比如缩小到640宽,高度按比例调整,或者强制调整为指定尺寸,即使拉伸。 另外,图片格式的支持也很重要。FFmpeg默认可能输出为视频格式,所以输出文件的后缀名必须是图片格式,比如.jpg或.png。同时,可以到调整输出质量参数,比如JPEG的压缩质量,这对用户可能有帮助。 还有,用户可能想批量处理多张图片。这时候可以使用通配符,比如`*.jpg`,但需要确保输出文件名合适,可能使用`%03d`这样的格式来生成序列文件名。不过要注意不同平台下通配符的使用可能有所区别,需要醒用户根据系统调整。 最后,检查是否有常见的错误,比如滤镜语法是否正确,路径是否有空格需要引号包裹,输出目录是否存在等。可能还需要醒用户安装最新版的FFmpeg,避免版本问题导致的不兼容。 总结步骤:基本命令、保持宽高比、强制尺寸、调整质量、批量处理。每个步骤给出示例,并解释参数的作用。确保用户能逐步跟随操作,避免技术术语过多,但保持准确性。</think>以下是如何使用FFmpeg修改图片分辨率的分步说明: $$ \text{基础公式:输出分辨率} = \text{scale=目标宽度:目标高度} $$ 1. **基础命令格式** ```bash ffmpeg -i input.jpg -vf "scale=目标宽度:目标高度" output.jpg ``` - `input.jpg`:原图路径 - `scale=640:480`:将分辨率改为640x480像素 - `output.jpg`:输出文件路径 2. **保持宽高比(常用场景)** ```bash # 按宽度等比缩放(高度自动计算) ffmpeg -i input.jpg -vf "scale=640:-1" output.jpg # 按高度等比缩放(宽度自动计算) ffmpeg -i input.jpg -vf "scale=-1:480" output.jpg ``` - `-1`表示自动计算对应边长度 3. **强制指定分辨率(可能变形)** ```bash ffmpeg -i input.jpg -vf "scale=800:600:force_original_aspect_ratio=disable" output.jpg ``` - 通过`force_original_aspect_ratio=disable`参数关闭宽高比保护 4. **高质量缩放(抗锯齿)** ```bash ffmpeg -i input.jpg -vf "scale=1920:1080:flags=lanczos" output.jpg ``` - `flags=lanczos`使用Lanczos插值算法,升缩放质量 5. **批量处理(多张图片)** ```bash ffmpeg -pattern_type glob -i "*.jpg" -vf "scale=1024:768" output_%03d.jpg ``` - `-pattern_type glob`:支持通配符匹配 - `output_%03d.jpg`:生成序列文件名(如output_001.jpg) **注意事项:** - 输出文件扩展名(如.jpg/.png)决定编码格式 - 建议保留`-q:v 2`参数控制JPEG质量(范围2-31,值越小质量越高) ```bash ffmpeg -i input.jpg -vf "scale=1280:720" -q:v 2 output.jpg ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值