In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to modify the picture resolution by Python. I hope you will get something after reading this article. Let's discuss it together.
Environmental dependence
For ffmpeg environment installation, please refer to: windows ffmpeg installation and deployment
Ffmpy installation:
Pip install ffmpy-I https://pypi.douban.com/simple code
No nonsense, go to the code.
#! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 2021-12-11 21V @ Author: swordsman A Liang ALiang# @ Site: # @ File: image_tool.py import osimport uuidfrom ffmpy import FFmpeg # resize the picture def change_size (image_path: str, output_dir: str, width: int Height: int): ext = os.path.basename (image_path). Strip (). Split ('.') [- 1] if ext not in ['png',' jpg']: raise Exception ('format error') _ result_path = os.path.join (output_dir,' {}. {} '.format (uuid.uuid1 (). Hex) Ext)) ff = FFmpeg (inputs= {'{} '.format (image_path): None}, outputs= {_ result_path:'-vf scale= {}: {} '.format (width, height)}) print (ff.cmd) ff.run () return _ result_path
Code description:
1. The input parameters of change_size method are: image address, output directory address, width to be modified, height to be modified.
2. The only verified image formats are png and jpg. If you need to add them, add them yourself.
3. In order to avoid duplicate output file names, use uuid as the file name.
Verify it.
The prepared pictures are as follows:
Execute the code:
If _ _ name__ = ='_ _ main__': print (change_size ('data/1234.jpg',' data/', 1280))
Execution result:
E:\ ProgramData\ Anaconda3\ envs\ pytorch\ python.exe C:/Users/yi/PycharmProjects/test/image_tool.pyffmpeg-I data/123.jpg-vf scale=1280:720 data/709ad7cc5a8a11ec82c82c4d54eea02b.jpgffmpeg 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-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.100Input # 0, image2, from 'data/123.jpg': Duration: 00from 00.04, start: 0.000000, bitrate: 170762 kb/s Stream # 0:0: Video: mjpeg (Progressive), yuvj444p (pc, bt470bg/unknown/unknown), 1920x1080, 25 tbr, 25 tbn, 25 tbcStream mapping: Stream # 0:0-> # 0:0 (mjpeg (native)-> mjpeg (native)) Press [Q] to stop, [?] For help [swscaler @ 000001d3c41b6c00] deprecated pixel format used, make sure you did set range correctlyOutput # 0, image2, to 'data/709ad7cc5a8a11ec82c82c4d54eea02b.jpg': Metadata: encoder: Lavf58.45.100 Stream # 0:0: Video: mjpeg, yuvj444p (pc), 1280x720, Qizh2-31,200 kb/s, 25 fps, 25 tbn 25 tbc Metadata: encoder: Lavc58.91.100 mjpeg Side data: cpb: bitrate max/min/avg: 0 fps=0.0 200000 buffer size: 0 vbv_delay: 1 fps=0.0 Lsize=N/A time=00:00:00.04 bitrate=N/A speed=0.28x video:106kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead 7.8 Lsize=N/A time=00:00:00.04 bitrate=N/A speed=0.28x video:106kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknowndata/709ad7cc5a8a11ec82c82c4d54eea02b.jpg
Effect picture:
After reading this article, I believe you have a certain understanding of "how to modify picture resolution by Python". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.