
本文档旨在指导开发者如何使用 Protobuf 定义的图像接口,在 Python 中实现图像的旋转功能。文章将详细介绍如何将 Protobuf 中以 bytes 类型存储的图像数据转换为可操作的矩阵形式,并提供完整的代码示例,帮助读者理解和应用图像旋转的实现方法。
在使用 Protobuf 进行图像处理时,一个常见的挑战是如何将以 bytes 类型传递的图像数据转换为可操作的格式,例如二维矩阵,以便进行旋转等操作。本文将详细介绍如何使用 Python 处理 Protobuf 定义的图像数据,并实现图像的旋转。
首先,我们需要理解 Protobuf 中图像的定义。根据提供的 .proto 文件,Image 消息包含以下字段:
彩色图像的 data 字段以 RGB 三元组的形式逐行存储,而灰度图像则以单字节形式逐行存储。
立即学习“Python免费学习笔记(深入)”;
关键在于将 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(*...) 用于转置矩阵。
旋转矩阵后,需要将其转换回 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], ... ]
本文档介绍了如何使用 Protobuf 定义的图像接口,在 Python 中实现图像的旋转功能。通过将 bytes 类型的 data 字段转换为可操作的矩阵形式,并使用 Python 的列表操作进行旋转,可以轻松实现图像的旋转。希望本文档能够帮助读者理解和应用图像旋转的实现方法。
以上就是使用 Protobuf 在 Python 中处理图像旋转的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号