I'm currently working on a little project in which I'm trying to generate a random image of a set size with a set palette. Everything generates fine and dandy, but zooming in on the image shows the pixels are interpolated in a way that doesn't look great. I would prefer if the pixels had hard edges, as with nearest neighbor interpolation. How would I go about doing that? See code below:
#!/usr/bin/env
import random
from PIL import Image
colors = ["#ffffff", "#898d90", "#000000", "#cf0530", "#2450a4", "#7eed56", "#ffd635", "#6134e1",
"#ffa800", "#6d482f", "#ff3881", "#51e9f4", "#fff8b8", "#94b3ff", "#158d62", "#515252" ]
def main():
size = width, height = 128, 128
image = Image.new( "RGB", size)
fillRand(image, size)
image.show()
del image
def randColor():
color = random.choice(colors)
return color
def hex_to_rgb(hex):
return tuple(int(hex.lstrip('#')[i:i 2], 16) for i in (0, 2, 4))
def fillRand(image, size):
for x in range(size[0]):
for y in range(size[1]):
pixel_access_object = image.load()
pixel_access_object[x,y] = (hex_to_rgb(randColor()))
if ( __name__ == "__main__"):
main()
Thank you in advance!
P.S. - Python isn't my main language and I'm rusty in general, so apologies if my code is wack.
CodePudding user response:
I think that's a problem with your image viewer. I used ImageGlass, and there the pixels have crisp edges. A possible solution would be to make your pixels bigger, so you color 16x16 pixels the same color, instead of one. Then the interpolation woulden't be as visible.