前言
本文主要分享一个可以将图片或者视频模糊化的工具代码。技术路线主要是使用ffmpeg滤镜。
环境依赖
ffmpeg环境部署,依赖包下载,给大家介绍一个调用ffmpeg比较好的库ffmpy。安装命令:
pip install ffmpy -i https://pypi.douban.com/simple
代码
不废话上代码。
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/11/18 0:06 # @Author : 剑客阿良_ALiang # @Site : # @File : blur_tool.py import os import uuid from ffmpy import FFmpeg def blur_image(image_path: str, output_dir: str, level=10): ext = os.path.basename(image_path).strip().split('.')[-1] if ext not in ['png', 'jpg']: raise Exception('format error') ff = FFmpeg( inputs={ '{}'.format(image_path): None}, outputs={ os.path.join( output_dir, '{}.{}'.format( uuid.uuid4(), ext)): '-filter_complex "boxblur={}:1:cr=0:ar=0"'.format(level)}) print(ff.cmd) ff.run() return os.path.join(output_dir, '{}.{}'.format(uuid.uuid4(), ext)) def blur_video(video_path: str, output_dir: str, level=10): ext = os.path.basename(video_path).strip().split('.')[-1] if ext not in ['mp4', 'avi', 'flv']: raise Exception('format error') ff = FFmpeg( inputs={ '{}'.format(video_path): None}, outputs={ os.path.join( output_dir, '{}.{}'.format( uuid.uuid4(), ext)): '-filter_complex "boxblur={}:1:cr=0:ar=0"'.format(level)}) print(ff.cmd) ff.run() return os.path.join(output_dir, '{}.{}'.format(uuid.uuid4(), ext))
代码说明
1、blur_image和blur_video两个方法参数都是:图片/视频路径、输出文件夹目录、模糊等级。
2、模糊等级越高,模糊效果越大,默认10。
3、后缀校验按照需要加,我只是随便写了几种。
4、为了输出文件名不重复,用uuid作为文件名。
校验一下
下面是我准备的图片以及视频截图:
验证代码
if __name__ == '__main__': print( blur_image( 'C:\\Users\\yi\\Desktop\\1.jpg', 'C:\\Users\\yi\\Desktop\\')) print( blur_video( 'C:\\Users\\yi\\Desktop\\2.mp4', 'C:\\Users\\yi\\Desktop\\'))
执行结果
E:\ProgramData\Anaconda3\envs\pytorch\python.exe C:/Users/yi/PycharmProjects/test/blur_tool.py ffmpeg -i C:\Users\yi\Desktop\1.jpg -filter_complex boxblur=10:1:cr=0:ar=0 C:\Users\yi\Desktop\3dae4af4-8b9a-468c-b236-0e9dd32ea1b2.jpg ffmpeg version n4.3.1-20-g8a2acdc6da Copyright (c) 2000-2020 the FFmpeg developers built with gcc 9.3-win32 (GCC) 20200320 configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --enable-iconv --enable-zlib --enable-libxml2 --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvmaf --disable-vulkan --enable-libvorbis --enable-amf --enable-libaom --enable-avisynth --enable-libdav1d --enable-ffnvcodec --enable-cuda-llvm --disable-libglslang --enable-libass --enable-libbluray --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvpx --enable-libwebp --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librav1e --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libtwolame --enable-libvidstab --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzimg --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-libs=-lgomp libavutil 56. 51.100 / 56. 51.100 libavcodec 58. 91.100 / 58. 91.100 libavformat 58. 45.100 / 58. 45.100 libavdevice 58. 10.100 / 58. 10.100 libavfilter 7. 85.100 / 7. 85.100 libswscale 5. 7.100 / 5. 7.100 libswresample 3. 7.100 / 3. 7.100 libpostproc 55. 7.100 / 55. 7.100 Input #0, image2, from 'C:\Users\yi\Desktop\1.jpg': Duration: 00:00:00.04, start: 0.000000, bitrate: 103100 kb/s Stream #0:0: Video: mjpeg (Progressive), yuvj444p(pc, bt470bg/unknown/unknown), 1920x1080, 25 tbr, 25 tbn, 25 tbc Stream mapping: Stream #0:0 (mjpeg) -> boxblur boxblur -> Stream #0:0 (mjpeg) Press [q] to stop, [?] for help Output #0, image2, to 'C:\Users\yi\Desktop\3dae4af4-8b9a-468c-b236-0e9dd32ea1b2.jpg': Metadata: encoder : Lavf58.45.100 Stream #0:0: Video: mjpeg, yuvj444p(pc), 1920x1080, q=2-31, 200 kb/s, 25 fps, 25 tbn, 25 tbc Metadata: encoder : Lavc58.91.100 mjpeg Side data: cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: N/A frame= 1 fps=0.0 q=4.4 Lsize=N/A time=00:00:00.04 bitrate=N/A speed=0.288x video:167kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown C:\Users\yi\Desktop\c3a052c5-5cdc-4c74-8b3e-c1f1ca2173b1.jpg ffmpeg -i C:\Users\yi\Desktop\2.mp4 -filter_complex boxblur=10:1:cr=0:ar=0 C:\Users\yi\Desktop\06b58bbd-5218-4856-a2bb-ff22e20571c9.mp4 ffmpeg version n4.3.1-20-g8a2acdc6da Copyright (c) 2000-2020 the FFmpeg developers built with gcc 9.3-win32 (GCC) 20200320 configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --enable-iconv --enable-zlib --enable-libxml2 --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvmaf --disable-vulkan --enable-libvorbis --enable-amf --enable-libaom --enable-avisynth --enable-libdav1d --enable-ffnvcodec --enable-cuda-llvm --disable-libglslang --enable-libass --enable-libbluray --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvpx --enable-libwebp --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librav1e --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libtwolame --enable-libvidstab --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzimg --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-libs=-lgomp libavutil 56. 51.100 / 56. 51.100 libavcodec 58. 91.100 / 58. 91.100 libavformat 58. 45.100 / 58. 45.100 libavdevice 58. 10.100 / 58. 10.100 libavfilter 7. 85.100 / 7. 85.100 libswscale 5. 7.100 / 5. 7.100 libswresample 3. 7.100 / 3. 7.100 libpostproc 55. 7.100 / 55. 7.100 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'C:\Users\yi\Desktop\2.mp4': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 encoder : Lavf58.45.100 Duration: 00:00:10.02, start: 0.000000, bitrate: 2013 kb/s Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 1884 kb/s, 23.98 fps, 23.98 tbr, 24k tbn, 47.95 tbc (default) Metadata: handler_name : VideoHandler Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 123 kb/s (default) Metadata: handler_name : SoundHandler Stream mapping: Stream #0:0 (h264) -> boxblur (graph 0) boxblur (graph 0) -> Stream #0:0 (libx264) Stream #0:1 -> #0:1 (aac (native) -> aac (native)) Press [q] to stop, [?] for help [libx264 @ 0000015d4492aec0] using SAR=1/1 [libx264 @ 0000015d4492aec0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2 [libx264 @ 0000015d4492aec0] profile High, level 4.0, 4:2:0, 8-bit [libx264 @ 0000015d4492aec0] 264 - core 161 - H.264/MPEG-4 AVC codec - Copyleft 2003-2020 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=23 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00 Output #0, mp4, to 'C:\Users\yi\Desktop\06b58bbd-5218-4856-a2bb-ff22e20571c9.mp4': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 encoder : Lavf58.45.100 Stream #0:0: Video: h264 (libx264) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], q=-1--1, 23.98 fps, 24k tbn, 23.98 tbc (default) Metadata: encoder : Lavc58.91.100 libx264 Side data: cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default) Metadata: handler_name : SoundHandler encoder : Lavc58.91.100 aac frame= 240 fps= 36 q=-1.0 Lsize= 2109kB time=00:00:10.00 bitrate=1726.7kbits/s speed=1.52x video:1947kB audio:152kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.451836% [libx264 @ 0000015d4492aec0] frame I:13 Avg QP:14.73 size: 25810 [libx264 @ 0000015d4492aec0] frame P:78 Avg QP:19.21 size: 13141 [libx264 @ 0000015d4492aec0] frame B:149 Avg QP:19.57 size: 4245 [libx264 @ 0000015d4492aec0] consecutive B-frames: 11.2% 11.7% 18.8% 58.3% [libx264 @ 0000015d4492aec0] mb I I16..4: 38.8% 57.4% 3.8% [libx264 @ 0000015d4492aec0] mb P I16..4: 15.5% 28.5% 0.1% P16..4: 23.4% 2.2% 0.5% 0.0% 0.0% skip:29.8% [libx264 @ 0000015d4492aec0] mb B I16..4: 2.5% 1.5% 0.0% B16..8: 20.9% 0.9% 0.0% direct: 4.2% skip:70.0% L0:44.8% L1:53.6% BI: 1.6% [libx264 @ 0000015d4492aec0] 8x8 transform intra:59.9% inter:98.9% [libx264 @ 0000015d4492aec0] coded y,uvDC,uvAC intra: 11.9% 30.3% 5.0% inter: 2.1% 9.8% 0.1% [libx264 @ 0000015d4492aec0] i16 v,h,dc,p: 32% 25% 6% 38% [libx264 @ 0000015d4492aec0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 41% 21% 24% 2% 3% 2% 4% 2% 1% [libx264 @ 0000015d4492aec0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 42% 23% 12% 4% 8% 5% 4% 3% 1% [libx264 @ 0000015d4492aec0] i8c dc,h,v,p: 66% 18% 14% 3% [libx264 @ 0000015d4492aec0] Weighted P-Frames: Y:39.7% UV:23.1% [libx264 @ 0000015d4492aec0] ref P L0: 68.7% 3.0% 19.9% 8.0% 0.4% [libx264 @ 0000015d4492aec0] ref B L0: 85.4% 11.4% 3.2% [libx264 @ 0000015d4492aec0] ref B L1: 96.7% 3.3% [libx264 @ 0000015d4492aec0] kb/s:1592.89 [aac @ 0000015d4498ed00] Qavg: 3689.104 C:\Users\yi\Desktop\ffbe6a82-1220-447e-8dea-31a42be16ca4.mp4 Process finished with exit code 0
图片模糊后的效果
视频模糊后的截图
以上就是Python 图片视频模糊化实现案例的详细内容,更多关于图片视频模糊化的资料请关注脚本之家其它相关文章!