使用 Protobuf 在 Python 中处理图像旋转

聖光之護
发布: 2025-09-26 19:08:01
原创
269人浏览过

使用 protobuf 在 python 中处理图像旋转

本文档旨在指导开发者如何使用 Protobuf 定义的图像接口,在 Python 中实现图像的旋转功能。文章将详细介绍如何将 Protobuf 中以 bytes 类型存储的图像数据转换为可操作的矩阵形式,并提供完整的代码示例,帮助读者理解和应用图像旋转的实现方法。

在使用 Protobuf 进行图像处理时,一个常见的挑战是如何将以 bytes 类型传递的图像数据转换为可操作的格式,例如二维矩阵,以便进行旋转等操作。本文将详细介绍如何使用 Python 处理 Protobuf 定义的图像数据,并实现图像的旋转。

理解 Protobuf 图像定义

首先,我们需要理解 Protobuf 中图像的定义。根据提供的 .proto 文件,Image 消息包含以下字段:

  • color: 布尔类型,指示图像是彩色(RGB)还是灰度图像。
  • data: 字节类型,存储图像的原始数据。
  • width: 整数类型,图像的宽度。
  • height: 整数类型,图像的高度。

彩色图像的 data 字段以 RGB 三元组的形式逐行存储,而灰度图像则以单字节形式逐行存储。

立即学习Python免费学习笔记(深入)”;

将 Bytes 数据转换为矩阵

关键在于将 bytes 类型的 data 字段转换为 Python 中可操作的矩阵形式。以下代码展示了如何实现这一转换:

import grpc
import image_pb2
import image_pb2_grpc

from concurrent import futures

# gRPC service implementation
class ImageService(image_pb2_grpc.ImageServiceServicer):

    def RotateImage(self, request, context):

        # Ensure that the number of bytes matches expection: width*height*bytes(color)
        # Where bytes(color) = 1 (false) and 3 (true)
        got = request.image.width * request.image.height * (3 if request.image.color else 1)
        want = len(request.image.data)

        if got != want:
            context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
            context.set_details("Image data size does not correspond to width, height and color")
            return request.image

        # If there's no rotation to perform, shortcut to returning the provided image
        if request.rotation == image_pb2.ImageRotateRequest.NONE:
            return request.image

        # Convert the image to a matrix
        matrix = []
        current = 0
        for y in range(request.image.height):
            row = []
            for x in range(request.image.width):
                if request.image.color:
                    # True (RGB) requires 3 bytes (use tuple)
                    pixel = (
                        request.image.data[current],
                        request.image.data[current+1],
                        request.image.data[current+2],
                    )
                    current += 3
                else:
                    # False (Grayscale) requires 1 byte
                    pixel = request.image.data[current]
                    current += 1

                row.append(pixel)
            # Append row
            matrix.append(row)

        print(matrix)

        if request.rotation == image_pb2.ImageRotateRequest.NINETY_DEG:
            print("Rotating: 090")
            matrix = list(zip(*matrix[::-1]))

        if request.rotation == image_pb2.ImageRotateRequest.ONE_EIGHTY_DEG:
            print("Rotating: 180")
            matrix = list(zip(*matrix[::-1]))
            matrix = list(zip(*matrix[::-1]))

        if request.rotation == image_pb2.ImageRotateRequest.TWO_SEVENTY_DEG:
            print("Rotating: 270")
            # Rotate counterclockwise
            matrix = list(zip(*matrix))[::-1]

        # Flatten the matrix
        pixels = []
        for y in range(request.image.height):
            for x in range(request.image.width):
                    if request.image.color:
                        pixels.extend(matrix[y][x])
                    else:
                        pixels.append(matrix[y][x])


        print(f"Result: {pixels}")

        # Revert the flattened matrix to bytes
        data = bytes(pixels)

        # Return the rotated image in the response
        return image_pb2.Image(
            color=request.image.color,
            data=data,
            width=request.image.width,
            height=request.image.height,
        )


# gRPC server setup
def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    image_pb2_grpc.add_ImageServiceServicer_to_server(ImageService(), server)
    server.add_insecure_port('[::]:50051')
    server.start()
    server.wait_for_termination()

if __name__ == '__main__':
    serve()
登录后复制

这段代码首先检查 data 字段的长度是否与图像的宽度、高度和颜色模式相符。如果不符,则返回错误。然后,它根据图像的颜色模式,将 data 字段转换为二维矩阵。彩色图像的每个像素由一个包含三个字节的元组表示,而灰度图像的每个像素由一个字节表示。

图像旋转

将图像数据转换为矩阵后,可以使用 Python 的列表操作进行旋转。以下代码展示了如何旋转矩阵:

        if request.rotation == image_pb2.ImageRotateRequest.NINETY_DEG:
            print("Rotating: 090")
            matrix = list(zip(*matrix[::-1]))

        if request.rotation == image_pb2.ImageRotateRequest.ONE_EIGHTY_DEG:
            print("Rotating: 180")
            matrix = list(zip(*matrix[::-1]))
            matrix = list(zip(*matrix[::-1]))

        if request.rotation == image_pb2.ImageRotateRequest.TWO_SEVENTY_DEG:
            print("Rotating: 270")
            # Rotate counterclockwise
            matrix = list(zip(*matrix))[::-1]
登录后复制

这段代码使用了 zip(*matrix[::-1]) 技巧来旋转矩阵。[::-1] 用于反转矩阵的行,zip(*...) 用于转置矩阵。

图酷AI
图酷AI

下载即用!可以免费使用的AI图像处理工具,致力于为用户提供最先进的AI图像处理技术,让图像编辑变得简单高效。

图酷AI 57
查看详情 图酷AI

将矩阵转换回 Bytes 数据

旋转矩阵后,需要将其转换回 bytes 类型,以便将其存储在 Protobuf 的 data 字段中。以下代码展示了如何实现这一转换:

        # Flatten the matrix
        pixels = []
        for y in range(request.image.height):
            for x in range(request.image.width):
                    if request.image.color:
                        pixels.extend(matrix[y][x])
                    else:
                        pixels.append(matrix[y][x])


        print(f"Result: {pixels}")

        # Revert the flattened matrix to bytes
        data = bytes(pixels)
登录后复制

这段代码将矩阵扁平化为一个列表,然后使用 bytes() 函数将其转换为 bytes 类型。

示例

以下是一些使用 grpcurl 工具测试图像旋转服务的示例:

灰度图像旋转 180 度

# Want: [[1,2,3],[4,5,6],[7,8,9]]
# Byte: 010203040506070809
# B64: AQIDBAUGBwgJ
DATA="AQIDBAUGBwgJ"
COLOR=false

REQUEST="
{
    \"rotation\": 2,
    \"image\": {
        \"color\": ${COLOR},
        \"data\": \"${DATA}\",
        \"width\": 3,
        \"height\": 3
    }
}"

grpcurl \
-plaintext \
-proto image.proto \
-d "${REQUEST}" \
localhost:50051 \
ImageService/RotateImage \
| jq -r .data \
| base64 --decode \
| xxd -g 3
登录后复制

预期输出:

00000000: 090807 060504 030201
# Want: [[9,8,7],[6,5,4],[3,2,1]]
登录后复制

彩色图像旋转 180 度

# Want: [[010101,020202,030303], ... ]
# Byte: 010101020202030303...
# B64: AQEBAgICAwMDBAQEBQUFBgYGBwcHCAgICQkJ
DATA="AQEBAgICAwMDBAQEBQUFBgYGBwcHCAgICQkJ"
COLOR=true

REQUEST="
{
    \"rotation\": 2,
    \"image\": {
        \"color\": ${COLOR},
        \"data\": \"${DATA}\",
        \"width\": 3,
        \"height\": 3
    }
}"

grpcurl \
-plaintext \
-proto image.proto \
-d "${REQUEST}" \
localhost:50051 \
ImageService/RotateImage \
| jq -r .data \
| base64 --decode \
| xxd -g 3
登录后复制

预期输出:

00000000: 090909 080808 070707 060606 050505 04
00000010: 040403 030302 020201 0101
# Want [[090909, 080808, 070707], ... ]
登录后复制

注意事项

  • 在处理图像数据时,务必确保 data 字段的长度与图像的宽度、高度和颜色模式相符。
  • 图像旋转算法可能会改变图像的宽度和高度。在更新 Image 消息时,请确保正确设置宽度和高度字段。
  • 为了提高代码的可测试性,可以将矩阵处理逻辑提取到单独的函数中。

总结

本文档介绍了如何使用 Protobuf 定义的图像接口,在 Python 中实现图像的旋转功能。通过将 bytes 类型的 data 字段转换为可操作的矩阵形式,并使用 Python 的列表操作进行旋转,可以轻松实现图像的旋转。希望本文档能够帮助读者理解和应用图像旋转的实现方法。

以上就是使用 Protobuf 在 Python 中处理图像旋转的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号