OpenCV (cv2) can be used to extract data from images and do operations on them. We demonstrate some examples of that below:Related courses:
We can extract the width, height and color depth using the code below:
Access pixel data
We can access the pixel data of an image directly using the matrix, example:
To iterate over all pixels in the image you can use:
You can modify the pixels and pixel channels (r,g,b) directly. In the example below we remove one color channel:
To change the entire image, you’ll have to change all channels: m[py][px], m[py][px], m[py][px].
You can save a modified image to the disk using:
The constructor of Pixmap takes the image path as parameter:
pixmap = QPixmap(os.getcwd() + '/logo.png')
This image needs to be in the same directory as your program. The QPixmap widget supports png and jpeg. Example code below.
PyQT load image in Pixmap
We create a standard QWidget as we have done before. Then we add the QPixmap widget inside which will load the image. The Pixmap is attached to a label which is drawn to the screen.