Skip to content

Instantly share code, notes, and snippets.

@Nikolaj-K
Last active December 27, 2019 16:35
Show Gist options
  • Save Nikolaj-K/1f201a30e58878af1122be918a617e52 to your computer and use it in GitHub Desktop.
Save Nikolaj-K/1f201a30e58878af1122be918a617e52 to your computer and use it in GitHub Desktop.
Script from the raw image processing video
# This is the script for the video explaining it here:
#
# https://youtu.be/QrJ9LyA-Z-U
import numpy as np
import matplotlib.pyplot as plt
COLOR_STEPS = 5
FILENAME = "holo.jpg"
def rgb_to_gray(pix):
WEIGHTS = [0.299, 0.587, 1.0 - (0.299 + 0.587)]
return sum(w * pix[k] for k, w in enumerate(WEIGHTS))
def gradient(pix, other_pix):
return other_pix - pix
def floor_step(pix):
"""See Plot[{x, 2^5 * Floor[x/2^5]}, {x, 0, 2^8}] on WolframAlpha."""
MAX = 2**8 - 1
coarseness = MAX / COLOR_STEPS
return [coarseness * np.floor(val / coarseness) for val in pix]
if __name__=='__main__':
# Load image.
img = plt.imread(FILENAME)
print('Data type: {}\nDimensions: {}\n'.format(type(img), img.shape), img[30][7:11])
# Write to copies of the image.
# Note: The member function .copy() gives a writable instance of the previously loaded image.
# Note Be aware of the data types here and the operations you can sacely do on them!
img_2, img_3, img_4 = img.copy(), img.copy(), img.copy()
for i, row in enumerate(img):
for j, pix in enumerate(row):
img_2[i, j] = rgb_to_gray(pix)
img_3[i, j] = floor_step(pix)
other_pix = img[i, (j + 1) % img.shape[1]] # the pixel on the right of i, j
img_4[i, j] = gradient(pix, other_pix)
if not i % 100:
print("row number {}".format(i))
# Plot all.
images = [img, img_2, img_3, img_4]
fig = plt.figure()
for k, image in enumerate(images):
fig.add_subplot(1, len(images), 1 + k)
plt.imshow(image)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment