Skip to content

Instantly share code, notes, and snippets.

@InputBlackBoxOutput
Created October 12, 2021 02:39
Show Gist options
  • Save InputBlackBoxOutput/712e11e13b8c748bbbab53214ae79076 to your computer and use it in GitHub Desktop.
Save InputBlackBoxOutput/712e11e13b8c748bbbab53214ae79076 to your computer and use it in GitHub Desktop.
Perform operations on pixels in an image
import cv2
import numpy as np
from PIL import Image
def process_pixels(img):
pixel_data = list(img.getdata())
pixels = np.array(pixel_data).flatten()
width = img.size[0]
height = img.size[1]
# Perform operations on pixels
print(pixels)
pixel_data = []
for index in range(0, len(pixels), 3):
pixel = []
for offset in range(3):
pixel.append(pixels[index + offset])
pixel_data.append(tuple(pixel))
img.putdata(pixel_data)
return img
img = Image.open('image.png')
img = process_pixels(img)
img.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment