本文首发于个人博客https://kezunlin.me/post/77697d8d/,欢迎阅读最新内容!
python useful tools and code snippets
Guide
flip
import cv2
image = cv2.imread("demo.jpg")
# Flipped Horizontally 水平翻转
h_flip = cv2.flip(image, 1)
cv2.imwrite("demo-h.jpg", h_flip)
# Flipped Vertically 垂直翻转
v_flip = cv2.flip(image, 0)
cv2.imwrite("demo-v.jpg", v_flip)
# Flipped Horizontally & Vertically 水平垂直翻转
hv_flip = cv2.flip(image, -1)
cv2.imwrite("demo-hv.jpg", hv_flip)
rotate
def rotate_anti_90(image):
image = cv2.transpose(image)
image = cv2.flip(image, 0)
return image
def rotate_anti_180(image):
image = cv2.flip(image, 0)
image = cv2.flip(image, 1)
return image
def rotate_anti_270(image):
image = cv2.transpose(image)
image = cv2.flip(image, 1)
return image
def rotate(image, angle, center=None, scale=1.0):
# rotate by angle
(h, w) = image.shape[:2] # hwc
if center is None:
center = (w / 2., h / 2.)
M = cv2.getRotationMatrix2D(center, angle, scale)
rotated = cv2.warpAffine(image, M, (w, h))
return rotated
compression
cv2.imwrite(full_image_path, image, [int( cv2.IMWRITE_JPEG_QUALITY), 100]) # no compression for jpg
# [int(cv2.IMWRITE_PNG_COMPRESSION), 9] 0-9 for png
# [int( cv2.IMWRITE_JPEG_QUALITY), 100] 0-100 for jpg
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <script>(adsbygoogle = window.adsbygoogle || []).push({});</script>
get video info
import datetime
import cv2
from moviepy.editor import VideoFileClip
import numpy as np
def get_video_info(video_path):
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
return
frame_number = cap.get(cv2.CAP_PROP_FRAME_COUNT)
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
fps = int(cap.get(cv2.CAP_PROP_FPS))
cap.release() # release video capture
print("fps = ",fps)
print("frame_number = ",frame_number)
size = (w,h)
print("size = ",size)
# frame number MAY BE WRONG ! so video time may be also wrong!
duration = int(frame_number / fps)
print("seconds=",duration)
video_time = str(datetime.timedelta(seconds = duration))
print("video_time=",video_time)
print("-----------------------using VideoFileClip------------------")
clip = VideoFileClip(video_path)
duration = clip.duration
print("video duration is " str(duration) " seconds")
video_time = str(datetime.timedelta(seconds = int(duration)))
print("video_time=",video_time)
def clip_video():
clip = VideoFileClip("1.mp4")
starting_point = 120 # start at second minute
end_point = 420 # record for 300 seconds (120 300)
subclip = clip.subclip(starting_point, end_point)
subclip.write_videofile("/path/to/new/video.mp4")
numpy argmax
numpy argmax for 2-dim and 3-dim
import numpy as np
# for 2-dim
array = np.array([
[1,2,3],
[4,5,6],
[9,8,7],
[1,2,3],
[10,1,2]
])
print("array.shape=",array.shape)
result1 = array.argmax(axis=0) # hw axis-0 ===> h shape=(w,) value range[0,1,2,3,4]
result2 = array.argmax(axis=1) # hw axis-0 ===> w shape=(h,) value range[0,1,2]
print(result1)
print(result1.shape)
print(result2)
print(result2.shape)
output
('array.shape=', (5, 3))
[4 2 2]
(3,)
[2 2 0 2 0]
(5,)
# for 3-dim
array2 = np.array([
[3,2,1],
[6,5,4],
[7,8,9],
[1,2,3],
[1,1,10]
])
image = np.array([
array,ar