在Python中迭代Google云存储对象

2024-05-16 11:44:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个云存储桶中有大约40000个图像,我需要使用Python检索它们,以便将这些图像制作成视频。目前我在Python中使用for循环,40k图像需要2-3个小时。我如何使用多重处理来加快速度

当前伪代码:

import cv2
from google.cloud import storage

frameSize = (1920, 1080)
storage_client = storage.Client()
bucket_name = "name-of-my-bucket"
out = cv2.VideoWriter('output_video.mp4',cv2.VideoWriter_fourcc(*'MPEG'), 2, frameSize, True)

blobs = storage_client.list_blobs(bucket_name)

for blob in blobs:
    image_string = blob.download_as_text()

    # Pre-processing the base64 string
    image_string = image_string.split(",", 1)[1]

    # Decoding base64 string
    nparr = np.frombuffer(base64.b64decode(image_string), np.uint8)

    image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
    image = cv2.rotate(image, cv2.ROTATE_180)

    # Writing image to VideoWriter
    out.write(image)


out.release()

Tags: name图像imageimportclientforstringbucket