Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PIL creating a fuzzy image

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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 16×16 pixels the same color, instead of one. Then the interpolation woulden’t be as visible.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading