Reading, displaying and speeding up gifs with python [OpenCV]

It's a complete note. .. I feel like I use it often (?)

Read

scikit-video version

--This is a safety measure

from skvideo.io import vread
gif = vread("sample.gif")
# print(gif.shape) # => (Example): (21, 600, 600, 3)

――But this is slow, so I think ↓ is better

OpenCV version

--Fast

import cv2
import numpy as np

# faster than `vread` in `skvideo.io`
def vread(path, T=30):
    cap = cv2.VideoCapture(path)
    gif = [cap.read()[1][:,:,::-1] for i in range(T)]
    gif = np.array(gif)
    cap.release()
    return gif

gif = vread("sample.gif", T=21)
# print(gif.shape) # => (Example): (21, 600, 600, 3)

――The only drawback is that you have to give the number of frames.

--Example of normal writing without giving the number of frames (not list contents) ↓

import cv2

filepath = "data/raw/GH010005-0.mp4"
cap = cv2.VideoCapture(filepath)

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret:
        cv2.imshow("Frame", frame)
        cv2.waitKey(1)
    else:
        cap.release()
cv2.destroyAllWindows()

display

Window display

import cv2
cv2.namedWindow("frame", cv2.WINDOW_NORMAL)

for t in range(len(gif)):
    cv2.imshow("frame", gif[t])
    cv2.waitKey(50) #It will not be displayed unless you wait for a while

cv2.destroyAllWindows()

output (1).gif

Display on jupyter

import matplotlib.pyplot as plt
%matplotlib inline

plt.figure(figsize=(21,9))
for t in range(21):
    plt.subplot(3,7,t+1)
    plt.imshow(gif[t])
plt.show()

Screenshot from 2019-11-24 20-56-10.png

writing

import moviepy.editor as mpy
def npy_to_gif(npy, filename):
    clip = mpy.ImageSequenceClip(list(npy), fps=10)
    clip.write_gif(filename)

(Comparison of loading time)

--OpenCV wins!

Screenshot from 2019-11-24 21-02-39.png

that's all!

(Gif borrowed from here https://miro.medium.com/max/1200/1*LnQ5sRu-tJmlvRWmDsdSvw.gif)

Recommended Posts

Reading, displaying and speeding up gifs with python [OpenCV]
Shining life with Python and OpenCV
Neural network with OpenCV 3 and Python 3
Reading and writing NetCDF with Python
Reading and writing CSV with Python
Capturing images with Pupil, python and OpenCV
Reading and writing JSON files with Python
Hello World and face detection with OpenCV 4.3 + Python
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
Reading and writing fits files with Python (memo)
Feature matching with OpenCV 3 and Python 3 (A-KAZE, KNN)
Example of reading and writing CSV with Python
Binarization with OpenCV / Python
I tried speeding up Python code including if statements with Numba and Cython
Tips for speeding up python code correctly with numba
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
A note on speeding up Python code with Numba
[Python] Webcam frame size and FPS settings with OpenCV
Programming with Python and Tkinter
Encryption and decryption with Python
"Apple processing" with OpenCV3 + Python3
Python and hardware-Using RS232C with Python-
Image editing with python OpenCV
Camera capture with Python + OpenCV
Reading .txt files with Python
Face detection with Python + OpenCV
python with pyenv and venv
Regarding speeding up python (memo)
Using OpenCV with Python @Mac
Works with Python and R
See the power of speeding up with NumPy and SciPy
Try to bring up a subwindow with PyQt5 and Python
Convert video to black and white with ffmpeg + python + opencv
[Python] Easy reading of serial number image files with OpenCV
Communicate with FX-5204PS with Python and PyUSB
Environment construction of python and opencv
Automatic image interpolation with OpenCV and Python (Fast Marching Method, Navier-Stokes)
Robot running with Arduino and python
Install Python 2.7.9 and Python 3.4.x with pip.
[Python] Using OpenCV with Python (Image Filtering)
AM modulation and demodulation with python
[Python] font family and font with matplotlib
Scraping with Node, Ruby and Python
[Python] Using OpenCV with Python (Image transformation)
Scraping with Python, Selenium and Chromedriver
Create a striped illusion with gamma correction for Python3 and openCV3
Scraping with Python and Beautiful Soup
How to make a surveillance camera (Security Camera) with Opencv and Python
Make a simple OMR (mark sheet reader) with Python and OpenCV
JSON encoding and decoding with python
Easy Python + OpenCV programming with Canopy
[GUI with Python] PyQt5-Drag and drop-
Draw a watercolor illusion with edge detection in Python3 and openCV3
Python CSV file reading and writing
Try face recognition with python + OpenCV
Roughly speed up Python with numba
Cut out face with Python + OpenCV
Face recognition with camera with opencv3 + python2.7
I played with PyQt5 and Python3
Load gif images with Python + OpenCV
Find image similarity with Python + OpenCV