Skip to content

Instantly share code, notes, and snippets.

@say4n
Created October 24, 2017 17:34
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 say4n/535713dc6b3b7b9abbd2c0281dcf0140 to your computer and use it in GitHub Desktop.
Save say4n/535713dc6b3b7b9abbd2c0281dcf0140 to your computer and use it in GitHub Desktop.
Pixelate an image without changing it's resolution!
#!/usr/bin/env python3
import matplotlib.pyplot as plt
import numpy as np
import PIL
import PIL.Image as Image
plt.axis('off')
dname, iname, fmt = "/Users/Photos/", "avatar", "jpg"
img = Image.open(dname + iname + "." + fmt)
scl = 8
shape = np.array(img).shape
output = img.resize((shape[0]//scl, shape[1]//scl), resample=PIL.Image.LANCZOS)
output = output.resize(shape[:2], resample=PIL.Image.BOX)
plt.imshow(output)
plt.show()
output.save(dname + iname + "-pixelated.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment