Skip to content

Instantly share code, notes, and snippets.

@muiz6
Last active September 9, 2023 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muiz6/6f24fca7d130b7d33ecc77a1958e831b to your computer and use it in GitHub Desktop.
Save muiz6/6f24fca7d130b7d33ecc77a1958e831b to your computer and use it in GitHub Desktop.
Image Quantisation
import numpy as np
from PIL import Image
IMG_PATH = "./assets/water.jpeg"
def quantise(pixelList, width, height):
pixels = np.array([(q(r), q(g), q(b)) for r, g, b in pixelList], dtype=np.uint8)
return pixels.reshape([height, width, 3])
def q(val):
remainder = val % 43
quotient = val // 43
return (
quotient * 43 if remainder < 43 // 2 or quotient == 5 else (quotient + 1) * 43
)
def main():
img = Image.open(IMG_PATH)
quantised_pixels = quantise(list(img.getdata()), img.width, img.height)
quantised_img = Image.fromarray(quantised_pixels, mode="RGB")
quantised_img.save("result.png")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment