python png
Python hosting: Host, run, and code Python in the cloud!
OpenCV (cv2) can be used to extract data from images and do operations on them. We demonstrate some examples of that below:
Related courses:Image properties
We can extract the width, height and color depth using the code below:
import cv2 |
Access pixel data
We can access the pixel data of an image directly using the matrix, example:
import cv2 |
To iterate over all pixels in the image you can use:
import cv2 |
Image manipulation
You can modify the pixels and pixel channels (r,g,b) directly. In the example below we remove one color channel:
import cv2 |
To change the entire image, you’ll have to change all channels: m[py][px][0], m[py][px][1], m[py][px][2].
Save image
You can save a modified image to the disk using:
cv2.imwrite('filename.png',m) |
Leave a Reply:
im.shape of an image read by opencv returns h,w,bpp,not the other way
Thanks! I had made a typo, all updated now.