python有用代码合集 旋转图像/base64图像编码/json序列化/request爬取网页等

本文首发于个人博客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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Python中爬取data:image/png;base64图片,你可以使用以下步骤: 1. 导入所需的库,包括requests、base64和PIL(Python Imaging Library)。 2. 使用requests库发送HTTP请求,获取包含base64编码图片数据的响应。 3. 从响应中提取出base64编码的图片数据。 4. 使用base64库解码图片数据。 5. 将解码后的图片数据转换为PIL图像对象。 6. 可选:对图像进行进一步处理或保存。 下面是一个示例代码,演示了如何在Python中爬取data:image/png;base64图片: ```python import requests import base64 from PIL import Image from io import BytesIO # 发送HTTP请求获取响应 response = requests.get("图片URL") # 提取base64编码图片数据 image_data = response.content.split(b";base64,")[1] # 解码图片数据 decoded_image_data = base64.b64decode(image_data) # 将解码后的图片数据转换为PIL图像对象 image = Image.open(BytesIO(decoded_image_data)) # 可选:对图像进行进一步处理或保存 image.show() ``` 请将"图片URL"替换为你需要爬取的data:image/png;base64图片的URL。这样,你就可以成功地在Python中爬取并处理data:image/png;base64图片了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [python爬取图片遇见src乱码: data:image/png;base64](https://blog.csdn.net/weixin_39834780/article/details/114445179)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值