在 Raspberry Pi 上使用 pypy 的 pypng 16 位 PNG 编码更快吗?
在我们的上一篇文章如何使用 pypng 将 Raspberry Pi 原始 10 位图像保存为 16 位 PNG中,我们研究了如何使用 pypng 库将 10 位原始 Raspberry Pi 相机图像保存为 16 位 PNG 文件。
但是,使用 CPython 3.7.3 保存单张图像需要约 26 秒。由于 pypy 可以为许多 Python 工作负载提供加速,我们尝试使用 pypy3 7.0.0(参见如何在 Raspberry Pi 上安装 pypy3)来加速 PNG 编码。
结果
pypng PNG 导出似乎是使用 pypy3 慢得多的工作负载之一。
- CPython 3.7.3:
编码耗时 24.22 秒 pypy37.0.0:编码耗时 266.60 秒
使用 pypy3 时编码慢了 10 倍以上!
因此我不建议使用 pypy3 来加速 pypng 编码工作负载,至少在 Raspberry Pi 上不要这样做!
完整示例
此示例源自我们之前在如何使用 pypng 将 Raspberry Pi 原始 10 位图像保存为 16 位 PNG上发布的完整示例:
is_pypng_benchmark.py
#!/usr/bin/env python3
import time
import picamera
import picamera.array
import numpy as np
import png
# 捕获图像
print("正在捕获图像...")
with picamera.PiCamera() as camera:
with picamera.array.PiBayerArray(camera) as stream:
camera.capture(stream, 'jpeg', bayer=True)
# 去马赛克数据并写入 rawimg
# (stream.array 包含未去马赛克的数据)
rawimg = stream.demosaic()
# 写入 PNG
print("正在写入 16 位 PNG...")
t0 = time.time()
with open('16bit.png', 'wb') as outfile:
writer = png.Writer(width=rawimg.shape[1], height=rawimg.shape[0], bitdepth=16, greyscale=False)
# rawimg 是 (w, h, 3) RGB uint16 数组
# 但 PyPNG 需要 (w, h*3) 数组
png_data = np.reshape(rawimg, (-1, rawimg.shape[1]*3))
# 将 10 位数据缩放为 16 位值(否则会显示为黑色)
# 注意:根据你的照片和设置,
# 它可能仍然显得相当暗!
png_data *= int(2**6)
writer.write(outfile, png_data)
t1 = time.time()
print(f"Encoding took {(t1 - t0):.2f} seconds")Check out similar posts by category:
Python, Raspberry Pi
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow