Last active
April 20, 2022 12:32
-
-
Save danyshaanan/6754465 to your computer and use it in GitHub Desktop.
A Python script to pixelate an image and add a thin black margin between the simulated pixels.
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
from PIL import Image | |
backgroundColor = (0,)*3 | |
pixelSize = 9 | |
image = Image.open('input.png') | |
image = image.resize((image.size[0]/pixelSize, image.size[1]/pixelSize), Image.NEAREST) | |
image = image.resize((image.size[0]*pixelSize, image.size[1]*pixelSize), Image.NEAREST) | |
pixel = image.load() | |
for i in range(0,image.size[0],pixelSize): | |
for j in range(0,image.size[1],pixelSize): | |
for r in range(pixelSize): | |
pixel[i+r,j] = backgroundColor | |
pixel[i,j+r] = backgroundColor | |
image.save('output.png') |
from PIL import Image
import os
Open Image
img = Image.open("example2.png")
imgSmall = img.resize((64, 64)) #smaller size will generate more pixelate results
result = imgSmall.resize(img.size,Image.NEAREST)
Save
result.save('result.png')
P.S: This doest what I exactly wanted.
@qaixerabbas
Haven't checked but I'm pretty sure that it's due to the differrence of division operators in python 2.7 and 3 . Replace all 4 occurrences of /
with //
, so that it would be integer division again, and it should be OK
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Traceback (most recent call last):
File "C:\Users\user\Desktop\Codes\piexels.py", line 7, in
image = image.resize((image.size[0]/pixelSize, image.size[1]/pixelSize), Image.NEAREST)
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py", line 1873, in resize
return self._new(self.im.resize(size, resample, box))
TypeError: integer argument expected, got float
Do I need to change the returning values to int ?