# Convert to 32-bit RGBA with solid opaque alpha
# and get the pointer numpy will want.
#
# Cautions:
# 1. I think I remember reading that PyQt5 only has
# constBits() and PySide2 only has bits(), so you may
# need to do something like `if hasattr(...)` for
# portability.
#
# 2. Format_RGBX8888 is native-endian for your
# platform and I suspect this code, as-is,
# would break on a big-endian system.
im_in = im_in.convertToFormat(QImage.Format_RGBX8888)
ptr = im_in.constBits()
ptr.setsize(im_in.byteCount())
# Convert the image into a numpy array in the
# format PyOpenCV expects to operate on, explicitly
# copying to avoid potential lifetime bugs when it
# hasn't yet proven a performance issue for my uses.
cv_im_in = np.array(ptr, copy=True).reshape(
im_in.height(), im_in.width(), 4)
cv_im_in = cv.cvtColor(cv_im_in, cv.COLOR_BGRA2RGB)
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
class main_window(QDialog):
def __init__(self):
QDialog.__init__(self)
# Create a QTextBrowser
qtb_rgba = QTextBrowser(self)
# Create an empty list
li_pixels = list()
#!! QImage to list of pixels !! >>> RGB format
image = QImage()
load_image = image.load("test_img_3x3", ".jpg")
# Make sure you got an image named "test_img_3x3" .jpg extension , it needs to sit on your HDD,
# in the root from where you run the script
# create it with the size of 3x3 , do it in Paint, or any other 2d image manipulation software
# An assert error will occur if fails to open your image
assert load_image == True
# Find more data about our image
# In our case is obvios that we create with our hand an image .jpg with a size of 3x3
# Even so, the functions bellow will may help us sometimes
# Obtain image size
image_width = image.width()
image_height = image.height()
print("Your image size is:\nWidth: " + str(image_width) + " Height: " + str(image_height) + "\n" + str(image_width) + "x" + str(image_height))
# Get QRGB values
qrgb = image.pixel(0, 0)
print("QRGB Values: " + str(qrgb))
# Convert it to QColor
qrgb_to_QCol = QColor(qrgb)
# Once you got it converted in QColor you got a lot of freedom to choose from,
# you can convert it to some of the most popular formats like:
# CMYK, HSL, RGB, RGBA, magenta, or even individual chanels R , G , B + a lot of other formats, even Floats...
# Convert it to RGBA
rgba = qrgb_to_QCol.getRgb()
print("RGBA Values: " + str(rgba))
# In order to achieve our goal: list of pixels for the entire image
# we got many ways to achieve it, depending of your needs you may implelent it different
# I will continue by producing a list of quadruplets values,
# from left to right - top to bottom
# The Quadruplets value for the list will be, which mean that 0 will hold value 0 on list , 1 hold 1 and so on...
# y _ _
#x|0 1 2
#|3 4 5
#|6 7 8
# And the position will be:
# y _ _
#x|0.0 1.0 2.0
#|0.1 1.1 2.1
#|0.2 1.2 2.2
# So let`s do it
# Generate a list of numbers for width
li_num_width = []
for i in range(image_width):
li_num_width.append(i)
# Generate a list of numbers for height
li_num_height = []
for i in range(image_height):
li_num_height.append(i)
# List for x num
x = [li_num_width for i in range(len(li_num_height))]
print("\nX list is:\n" + str(x))
# List for y num
for i in range(len(li_num_height)):
y = [[i]*len(li_num_width) for i in range(len(li_num_height))]
print("\nY list is:\n" + str(y))
row_el_li = []
row_el_li_y = []
# Obtain list numbers for x
for i in range(len(li_num_height)):
row = x[i]
for i in range(len(li_num_width)):
row_el = row[i]
#print(row_el)
row_el_li.append(row_el)
print("\nRow Elements list x: \n" + str(row_el_li))
# Obtain list numbers for y
for i in range(len(li_num_height)):
row_y = y[i]
for i in range(len(li_num_width)):
row_el_y = row_y[i]
#print(row_el_y)
row_el_li_y.append(row_el_y)
print("\nRow Elements list y: \n" + str(row_el_li_y))
# Create a list, which eventualy will hold qrgb values, which is our goal
qrgb_li = []
# How many values will the list hold? or How many pixels in the image do we have?
num_pixels = len(li_num_width) * len(li_num_height)
print("\nNumber of Pixels:" + str(num_pixels))
for i in range(num_pixels):
ordered_qrgb = image.pixel(row_el_li[i], row_el_li_y[i])
qrgb_li.append(ordered_qrgb)
# One more step lets convert from QRGB list to RGBA list, which will lead us to the end of this tutorial
rgba_li = []
for i in range(len(qrgb_li)):
qrgb_li_to_QCol = QColor(qrgb_li[i])
rgba_set = qrgb_li_to_QCol.getRgb()
rgba_li.append(rgba_set)
print("\nList of Quadruplets RGBA Value: \n" + str(rgba_li))
#!! QImage to list of pixels End !!
qtb_rgba.setText(str(rgba_li))
#.......
self.setFixedSize(250, 300)
self.setWindowTitle("QImage to list of pixels")
#.......3D Sasu Catalin
app = QApplication(sys.argv)
dialog = main_window()
dialog.show()
sys.exit(app.exec_())
对于那些仍然需要知道如何做到这一点的人,我现在使用一些代码将像素数据从PyQt5获取到PyOpenCV,而无需保存和加载
由于PyOpenCV使用
numpy.array
代替OpenCVMat
,这应该可以回答这个问题我仍在快速试验原型,因此尚未对其进行优化,但下面介绍如何进行优化:
np.array
构造函数中删除copy=True
李>QImage.convertToFormat
可以生成的格式,相关OpenCV模块可以直接对其进行操作,并跳过cv.cvtColor
步骤。(我认为Format_BGR888
就是这种格式,但目前我的项目中仍然需要支持5.14之前的Qt版本。)See the youtube video
我希望我能理解你的需求,在这里我组装了一个应用程序。这将把QImage转换成四元组RGBA值的列表,我希望它至少能帮助您了解如何实现它,您可以根据自己的需要实现它,您可以使用矩阵/矩阵操纵库。如NumPy或任何其他矩阵操作库
但是,在本例中,我没有创建任何依赖项,因为我认为保持简单更好,如果您需要更复杂的内容,您可以深入了解
你需要一张图片,为了简单起见,我将提供3张图片,每张图片的大小和像素颜色都不同。图片1:
图2:
图3:
你也可以check my website to see the code
代码如下:
我希望这能帮助你,如果你需要更多关于这个主题的细节,你可以不时访问my website
相关问题 更多 >
编程相关推荐