人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

Python 實現(xiàn)圖片色彩轉換案例

發(fā)布日期:2021-12-08 22:55 | 文章來源:源碼中國

本文提供將圖片色彩轉為黑白或者褐色風格。比較類似于我們在看動漫、影視作品中,當人物在回憶過程中,體現(xiàn)出來的畫面一般都是黑白或者褐色的。

環(huán)境依賴

ffmpeg環(huán)境依賴,ffmpy依賴安裝

pip install ffmpy -i https://pypi.douban.com/simple

代碼

不廢話,上代碼

#!/user/bin/env python
# coding=utf-8
"""
@project : csdn
@author  : 劍客阿良_ALiang
@file: image_change_color_tool.py
@ide : PyCharm
@time: 2021-11-19 15:10:22
"""
import os
import uuid
from ffmpy import FFmpeg
 
 
# 黑白圖
def black_and_white(image_path: str, output_dir: str):
 ext = _check_format(image_path)
 result = os.path.join(output_dir, '{}.{}'.format(uuid.uuid4(), ext))
 ff = FFmpeg(inputs={image_path: None},
 outputs={result: '-vf colorchannelmixer=.3:.4:.3:0:.3:.4:.3:0:.3:.4:.3 -y'})
 print(ff.cmd)
 ff.run()
 return result
 
 
# 褐色
def brown(image_path: str, output_dir: str):
 ext = _check_format(image_path)
 result = os.path.join(output_dir, '{}.{}'.format(uuid.uuid4(), ext))
 ff = FFmpeg(inputs={image_path: None},
 outputs={result: '-vf colorchannelmixer=.393:.769:.189:0:.349:.686:.168:0:.272:.534:.131 -y'})
 print(ff.cmd)
 ff.run()
 return result
 
 
def _check_format(image_path: str):
 ext = os.path.basename(image_path).strip().split('.')[-1]
 if ext not in ['png', 'jpg']:
  raise Exception('format error')
 return ext

代碼說明

1、兩個方法入參均為:圖片路徑、輸出目錄路徑。

2、為了避免文件名重復,使用uuid作為文件名。

3、圖片的后綴格式判斷只有兩種,如需增加,自行添加即可。

驗證一下

圖片準備,原圖如下:

驗證代碼

if __name__ == '__main__':
 print(black_and_white('C:/Users/huyi/Desktop/4.jpg', 'C:/Users/huyi/Desktop/'))
 print(brown('C:/Users/huyi/Desktop/4.jpg', 'C:/Users/huyi/Desktop/'))

執(zhí)行結果

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/image_change_color_tool.py', wdir='D:/spyder/csdn')
ffmpeg -i C:/Users/huyi/Desktop/4.jpg -vf colorchannelmixer=.3:.4:.3:0:.3:.4:.3:0:.3:.4:.3 -y C:/Users/huyi/Desktop/c7052ed8-d5db-4e84-9c01-88d661c967a5.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
  libavutil56. 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
  libswscale5.  7.100 /  5.  7.100
  libswresample3.  7.100 /  3.  7.100
  libpostproc 55.  7.100 / 55.  7.100
Input #0, image2, from 'C:/Users/huyi/Desktop/4.jpg':
  Duration: 00:00:00.04, start: 0.000000, bitrate: 101138 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 -> #0:0 (mjpeg (native) -> mjpeg (native))
Press [q] to stop, [?] for help
[swscaler @ 000001625402e2c0] deprecated pixel format used, make sure you did set range correctly
[swscaler @ 0000016253b14f80] deprecated pixel format used, make sure you did set range correctly
Output #0, image2, to 'C:/Users/huyi/Desktop/c7052ed8-d5db-4e84-9c01-88d661c967a5.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=9.2 Lsize=N/A time=00:00:00.04 bitrate=N/A speed=0.232x 
video:133kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
C:/Users/huyi/Desktop/c7052ed8-d5db-4e84-9c01-88d661c967a5.jpg
ffmpeg -i C:/Users/huyi/Desktop/4.jpg -vf colorchannelmixer=.393:.769:.189:0:.349:.686:.168:0:.272:.534:.131 -y C:/Users/huyi/Desktop/9282f36a-2361-4b0d-a115-56864c9d2337.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
  libavutil56. 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
  libswscale5.  7.100 /  5.  7.100
  libswresample3.  7.100 /  3.  7.100
  libpostproc 55.  7.100 / 55.  7.100
Input #0, image2, from 'C:/Users/huyi/Desktop/4.jpg':
  Duration: 00:00:00.04, start: 0.000000, bitrate: 101138 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 -> #0:0 (mjpeg (native) -> mjpeg (native))
Press [q] to stop, [?] for help
[swscaler @ 0000013b05762f80] deprecated pixel format used, make sure you did set range correctly
[swscaler @ 0000013b052c4fc0] deprecated pixel format used, make sure you did set range correctly
Output #0, image2, to 'C:/Users/huyi/Desktop/9282f36a-2361-4b0d-a115-56864c9d2337.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=9.5 Lsize=N/A time=00:00:00.04 bitrate=N/A speed=0.129x 
video:177kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
C:/Users/huyi/Desktop/9282f36a-2361-4b0d-a115-56864c9d2337.jpg

黑白圖效果

褐色圖效果

到此這篇關于Python 實現(xiàn)圖片色彩轉換案例的文章就介紹到這了,更多相關Python 圖片色彩轉換內容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持本站!

版權聲明:本站文章來源標注為YINGSOO的內容版權均為本站所有,歡迎引用、轉載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網站,禁止在非www.sddonglingsh.com所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內容來源于網友推薦、互聯(lián)網收集整理而來,僅供學習參考,不代表本站立場,如有內容涉嫌侵權,請聯(lián)系alex-e#qq.com處理。

相關文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務

7x24全年不間斷在線

專屬顧問服務

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務熱線

關注
微信

關注官方微信
頂部