Last active
September 9, 2023 23:38
-
-
Save muiz6/6f24fca7d130b7d33ecc77a1958e831b to your computer and use it in GitHub Desktop.
Image Quantisation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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