跳转到主要内容

PYNQ在PS端进行图片缩放

judy 提交于

本文转载自:<span id="profileBt"><a href="https://mp.weixin.qq.com/s/LXH7mm4srLpA2SjThkRX7g"&gt; 硬码农二毛哥微信公众号</a></span>

在完成PYNQ环境搭建后(zynq7035单板创建PYNQ镜像V2.6),本文介绍如何在PS端进行图片缩放。

<strong>导入图片</strong>

进入jupyter后,使用updload,将图片导入到\192.168.2.99\xilinx\jupyter_notebooks中。
<center><img src="http://xilinx.eetrend.com/files/2021-11/%E5%8D%9A%E5%AE%A2/100555107-22…; alt=""></center>

<strong>导入Bit文件</strong>
将工程文件夹impl_1中的.bit文件,改名为system.bit。将sources_1\bd\system\hw_handoff中的.hwh和.tcl文件,改名为system.hwh和system.tcl。下载bit文件时需要用到这三个文件,PS端运行程序暂时不用。
<center><img src="http://xilinx.eetrend.com/files/2021-11/%E5%8D%9A%E5%AE%A2/100555107-22…; alt=""></center>

<strong>进行图片缩放</strong>
在jupyter中新建ipynb文件,点击New-Python3。
<center><img src="http://xilinx.eetrend.com/files/2021-11/%E5%8D%9A%E5%AE%A2/100555107-22…; alt=""></center>

在jupyter中进行图片缩放流程如下:

<li>导入库</li>

<li>读取图片并显示</li>

<li>缩小图片并显示</li>

<li>计算缩小图片所用时间</li>

<strong>导入库文件</strong>

<pre>from PIL import Image
import numpy as np
from IPython.display import display
import cv2</pre>

<strong>进行图片格式转换</strong>
<pre>src = cv2.imread('2.jpg')
print(src.shape) //(288, 352, 3)
cv2.imwrite('2.png',src) //将jpg转换成png</pre>

<strong>读取图片并显示</strong>
<strong>创建图片对象</strong>
<pre>image_path = "2.png"
original_image = Image.open(image_path)
original_image.load()
input_array = np.array(original_image)</pre>

<strong>显示</strong>
<pre>input_image = Image.fromarray(input_array)
display(input_image)</pre>

<strong>原始图像大小</strong>
<pre>old_width, old_height = original_image.size
print("Image size: {}x{} pixels.".format(old_width, old_height)) //Image size: 352x288 pixels.</pre>

<strong>缩小图片并显示</strong>
<strong>图片缩小后大小28x28</strong>
<pre>resized_image = original_image.resize((28, 28))</pre>

<strong>显示缩小后图片</strong>
<pre>output_array = np.array(resized_image)
result = Image.fromarray(output_array)
cv2.imwrite('s2.png',output_array) //将缩小后图片保存
display(result)</pre>

<center><img src="http://xilinx.eetrend.com/files/2021-11/%E5%8D%9A%E5%AE%A2/100555107-22…; alt=""></center>

<strong>检测缩小后图片尺寸</strong>
<pre>width, height = resized_image.size
print("Resized image size: {}x{} pixels.".format(width, height)) //Resized image size: 28x28 pixels.</pre>

<strong>计算缩小图片所用时间</strong>
<pre>%%timeit
resized_image = original_image.resize((new_width, new_height), Image.BILINEAR)</pre>

<strong>输出</strong>
<pre>100 loops, best of 3: 10.6 ms per loop</pre>

参考: https://github.com/Xilinx/PYNQ-HelloWorld