将QImage转换为numpy array


最近在做手写体识别,需要将QT中手写的数字转换成像训练集一样图片。因此需要将QImage转换为numpy array。

前言

笔者使用的是PyQt,但是对QT和Python之间数据之间的转换不太熟悉。查了很长时间,也没有找到详细的说明,最后在stackoverflow中查到了转换方法,但是说的也不清楚。

终于,经过查阅QT的参考手册终于明白了转换过程。

详细过程

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

image = self.canvas_label.canvas.toImage()
size = image.size()
s = image.bits().asstring(size.width() * size.height() * image.depth() // 8)  # format 0xffRRGGBB

arr = np.fromstring(s, dtype=np.uint8).reshape((size.height(), size.width(), image.depth() // 8))

new_image = Image.fromarray(array)

# convert to gray
new_image.convert("L")
new_image.thumbnail((28, 28))
plt.imshow(new_image, cmap='gray')
plt.show()

1. 将QImage转换为字符串

笔者的原图是通过QPixmap绘制的一幅RGB图。之后将其转换为QImage。

通过s = image.bits().asstring(size.width() * size.height() * image.depth() // 8)将图像数据转换成字符串。

参数是图像中字节数,字节数等于图像宽度 × 图像高度 × 通道数,即$bytes = width height channels$

需要注意的是通道数,查看QT的手册知道QT的RGB图像的格式是0xFFRRGGBB,其实就是将Alpha通道全部置为了0xFF。

之前以为只有3个通道,所以一直有问题。QImage.depth()可以返回图像深度的比特数,对于RGB图QImage.depth()返回值为32,所以整除8之后就是通道数。

2. 将字符串转换为Numpy array

之后使用np.fromstring()即可通过字符串构造numpy array。

到这里QImage转换为numpy array的任务就完成了。之后需要将原图进行灰度处理和压缩。

3. 灰度处理 & 压缩


文章作者: Xu Yuan
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Xu Yuan !
评论
 上一篇
MapReduce[翻译] MapReduce[翻译]
摘要MapReduce是一个编程模型,是一个与处理和生成大数据集相关的实现。用户指定一个处理键值对的 Map 函数生成一个中间键值对的集合,指定一个 Reduce 函数来合并有相同中间键的所有中间值。许多现实中的任务都可以用这种模型表达,
2020-03-01
下一篇 
Load Mnist database of handwritten digit via Python Load Mnist database of handwritten digit via Python
The handwritten digits recognition may be your first project when you get started with Tensorflow or Pytorch, and the da
2020-02-20
  目录