程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Two lines of Python code to realize video negative effects

編輯:Python

One 、 introduction

For the pictures in the video , Sometimes for special requirements such as special effect processing, we want to carry out the effect similar to negative photo .

Negative film (Negative Film) It's an image after exposure and development , The light and shade are opposite to the subject , Its color is the complementary color of the subject , It needs to be printed on the photo before it can be restored to a positive image . Take black and white movies , On the negative film, people's hair is white , In fact, white clothes are black on the film ; Color film , The color on the film is just complementary to the actual color of the scene , Such as : The clothes that are actually red are cyan on the film . Negative film, whether black and white or color, is the most commonly used film for photography . What we usually say is the negative film used to develop photos .

This article introduces through Python+Moviepy Two lines of code to realize the video anti color processing, so that each frame is the negative effect of the original picture .

Two 、 Realize the case of video anti color processing

In order to achieve video anti color processing , The sample code is as follows :

from moviepy.editor import *
clip = VideoFileClip(r"F:\video\scenery.mp4")
clipInvert_colors = clip.fx(vfx.invert_colors)
clipInvert_colors.write_videofile (r"F:\video\scenery_invert_colors.mp4")

The above four lines of code realize Moviepy Module loading 、 Then read in the video file 、 Invert the video 、 Output the inverted video to the result video file . The above four lines of code can also be reduced to the following two lines :

from moviepy.editor import *
VideoFileClip(r"F:\video\scenery.mp4").fx(vfx.invert_colors).write_videofile (r"F:\video\scenery_invert_colors.mp4")

The output color reversal video is as follows :

You can see the inverted video, which has a magical feeling .

3、 ... and 、 Background knowledge

3.1、moviepy brief introduction

To achieve video editing , The old ape used it moviepy library .MoviePy It's a video editor Python modular , Can be used for basic video operations ( Like cutting 、 Connect 、 Title insertion )、 Video synthesis ( Also known as nonlinear editing )、 Video processing or creating advanced effects . It can read and write the most common video formats , Include GIF.MoviePy The video that can be processed is ffmpeg Format , The old ape understands that the supported file types include at least :*.mp4 *.wmv *.rm *.avi *.flv *.webm *.wav *rmvb.

MoviePy Very simple installation , Use pip When installing , Please point the site to the domestic mirror site , Otherwise, the download will be slow or unable to download , The old ape uses the mirror image of Tsinghua University , Instruction is :

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple moviepy

3.2、 The code uses related functions to introduce

The related functions involved in the above code include VideoFileClip、fx、invert_colors、write_videofile, among :

  • VideoFileClip It's actually a class , Used to load video from a video file into memory
  • fx The function is Moviepy Editing base class Clip Methods ,fx Methods are used to execute functions specified by parameters , And returns the execution result of the function corresponding to the parameter
  • invert_colors Invert the color of the pixel . The specific reversal method is , For standard clips , use 255 Minus each RGB Value , For mask clips , use 1.0 Minus the original value . Color reversal is also called negative conversion
  • write_videofile Used to output clip content to a video file

Four 、 Summary

This paper introduces the use of Python+Moviepy Two lines of code to achieve video color reversal method , Video color reversal is similar to negative processing in photo development .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved