2. Python is a very powerful language that can accomplish many
tasks such as image manipulation(The process of editing an
image is called image manipulation). Processing a video
means, performing operations on the video frame by frame.
Frames are nothing but just the particular instance of the
video in a single point of time.
3. Pillow is built on top of PIL (Python Image Library).
PIL is one of the important modules for image
processing in Python. Pillow supports a large number
of image file formats including BMP, PNG, JPEG, and
TIFF. It incorporates lightweight image processing
tools that aids in editing, creating and saving images.
Python Imaging Library
4. This method is used to display the image. For displaying
the image Pillow first converts the image to a .png format
(on Windows OS) and stores it in a temporary buffer and
then displays it.
6. To resize an image, you call
the resize() method on it,
passing in a two-integer tuple
argument representing the
width and height of the resized
image.
7. from PIL import Image
size = (40, 40)
img =
Image.open(r“pic1.png")
img1 = img.resize(size)
img1.show()
Example
8. Image rotation is done
by specific angles and
for that again specific
keywords need to
passed. You can rotate
image 90 degree, 45
degree, 180 degree etc.
Rotating Images
Example
from PIL import Image
# Open image using
Image module
n= Image.open(“girl.jpg”)
# Show actual image
n.show()
#show rotated image
n =n.rotate(45)
n.show()
10. It applies a blurring effect on to the image as
specified through a specific kernel or a
convolution matrix.
Syntax
filter(ImageFilter.BLUR)
Blurred Image
13. While using the save() method
Destination path must have
the image filename and
extension as well. The
extension could be omitted in
Destination path if the
extension is specified in the
format argument.
15. Show Image
Resize Image
Rotate Image
Blured Image
Pillow Library allow you to perform difference task such
show image, resize image, rotate image, blurred image
etc.
16. OpenCV VideoCapture
OpenCV provides the VideoCature() function
which is used to work with the Camera. We
can do the following task:
Read video, display video, and save video.
Capture from the camera and display it.
17. The cv2.imwrite() function is used to save the video
into the file. First, we need to create a VideoWriter
object. Then we should specify the FourCC code and
the number of frames per second (fps). The frame
size should be passed within the function.
Saving a Video
18. import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read() # Capture image frame-by-frame
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray) # Display the resulting frame
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
Example
19. import numpy as np
import cv2
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,0)
# write the flipped frame
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
Saving a Video
20. MoviePy
MoviePy is a Python module for video
editing, which can be used for basic
operations (like cuts, concatenations,
title insertions), video compositing
(a.k.a. non-linear editing), video
processing, or to create advanced
effects. It can read and write the most
common video formats, including GIF.
21. We will load the video and we will cut a clip
from the whole video then we will add text in
the video, in this example we have to install
ImageMagick otherwise it will not work.
Example
22. from moviepy.editor import *
clip = VideoFileClip("dsa_v.webm“)
# loading video dsa gfg intro video
# getting video for only starting 10 seconds
clip = clip.subclip(0, 10)
clip = clip.volumex(0.8) # Reduce the audio volume (volume x 0.8)
# Generate a text clip
txt_clip = TextClip(“RaginiTutorial", fontsize = 50, color = 'white‘)
txt_clip = txt_clip.set_pos('center').set_duration(10)
# Overlay the text clip on the first video clip
video = CompositeVideoClip([clip, txt_clip])# showing video
video.ipython_display(width = 280)
23. Python offers multiple libraries to ease
our work. Here we will learn how to
take a screenshot using Python.
Python provides a module
called pyscreenshot for this task. It is
only a pure Python wrapper, a thin
layer over existing backends.
Performance and interactivity are not
important for this library.
24. import pyscreenshot
# To capture the screen
image = pyscreenshot.grab()
#To display the captured
screenshot
image.show()
# To save the screenshot
image.save(“schreenshot2.png")
Example
25. Here is the simple Python
program to capture the part of
the screen. Here we need to
provide the pixel positions in
the grab() function. We need
to pass the coordinates in the
form of a tuple.
import pyscreenshot
image=pyscreenshot.grab(bbox=(10,10,500, 500))
image.show() # To view the screenshot
image.save(“screenshot1.png“)
Example