前言
本文提供将图片按照自定义尺寸进行裁剪的工具方法,一如既往的实用主义。
环境依赖
ffmpeg环境安装,可以参考我的另一篇文章:windows ffmpeg安装部署_阿良的博客-CSDN博客
本文主要使用到的不是ffmpeg,而是ffprobe也在上面这篇文章中的zip包中。
ffmpy安装:
pip install ffmpy -i https://pypi.douban.com/simple
代码
不废话,上代码。
#!/user/bin/env python # coding=utf-8 """ @project : csdn @author : 剑客阿良_ALiang @file : cut_out_pic_tool.py @ide : PyCharm @time : 2022-01-20 10:38:53 """ import os import uuid from ffmpy import FFmpeg # 图片裁剪 def cut_out_pic(image_path: str, output_dir: str, start_pix: tuple, size: tuple): ext = os.path.basename(image_path).strip().split('.')[-1] if ext not in ['png', 'jpg']: raise Exception('format error') result = os.path.join(output_dir, '{}.{}'.format(uuid.uuid1().hex, ext)) ff = FFmpeg(inputs={image_path: None}, outputs={result: '-vf crop={}:{}:{}:{} -y'.format(size[0], size[1], start_pix[0], start_pix[1])}) print(ff.cmd) ff.run() return result if __name__ == '__main__': cut_out_pic(r'C:\Users\huyi\Desktop\231.jpg', r'C:\Users\huyi\Desktop', (1000, 1000), (1000, 1000))
验证一下
准备的图片
执行结果
C:\Users\huyi\.conda\envs\python36\python.exe "C:\Program Files\JetBrains\PyCharm 2020.1.3\plugins\python\helpers\pydev\pydevconsole.py" --mode=client --port=3635
import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend(['D:\\spyder\\csdn', 'D:/spyder/csdn'])
PyDev console: starting.
Python 3.6.13 |Anaconda, Inc.| (default, Mar 16 2021, 11:37:27) [MSC v.1916 64 bit (AMD64)] on win32
runfile('D:/spyder/csdn/cut_out_pic_tool.py', wdir='D:/spyder/csdn')
ffmpeg -i C:\Users\huyi\Desktop\231.jpg -vf crop=1000:1000:1000:1000 -y C:\Users\huyi\Desktop\6e81cb7a79cb11ec96d7e454e8bf1461.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\huyi\Desktop\231.jpg':
Duration: 00:00:00.04, start: 0.000000, bitrate: 181614 kb/s
Stream #0:0: Video: mjpeg (Progressive), yuvj444p(pc, bt470bg/unknown/unknown), 2560x1440, 25 tbr, 25 tbn, 25 tbc
Stream mapping:
Stream #0:0 -> #0:0 (mjpeg (native) -> mjpeg (native))
Press [q] to stop, [?] for help
Output #0, image2, to 'C:\Users\huyi\Desktop\6e81cb7a79cb11ec96d7e454e8bf1461.jpg':
Metadata:
encoder : Lavf58.45.100
Stream #0:0: Video: mjpeg, yuvj444p(pc), 1000x1000, 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=6.7 Lsize=N/A time=00:00:00.04 bitrate=N/A speed=0.262x
video:65kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
结果图片
总结
这几天好忙,没啥好总结的。
分享:
一次次轮回,一次次抉择,一次次生死,对这尘世多少有了几分疲惫和厌倦,然而责任已经成为习惯,就算早已堪透,也不是那么容易就能放得下。——《花千骨》
如果本文对你有帮助的话,点个赞吧,谢谢!