Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

enlarge image (may be pixelate but not blurry)

Tags:

python

I wrote a script that draws some images, usually not bigger than 50x50px. Then I want to display that image in a Tkinter window. But first I need to enlarge the image because 30x30px are too small for a user to see every single pixel generated by my script. So I wrote this:

multiplier = 4
image = np.full((height * multiplier, width * multiplier, 3), 0, dtype=np.uint8)

for r in range(height):
    for c in range(width):
        for i in range(multiplier):
            for j in range(multiplier):
                image[r * multiplier + i][c * multiplier + j] = original[r][c]

P.S. original was initialized the same way as image

Also I tried:

resize((width * multiplier ,height * multiplier), Image.ANTIALIAS)

but it's not an option, because it makes an image look blurry. So what would be the better solution?

Example image: enter image description here

like image 246
sovel2 Avatar asked Oct 28 '25 10:10

sovel2


1 Answers

I would suggest resizing with Nearest Neighbour resampling so you don't introduce any new, blurred colours - just ones already existing in your image:

import numpy as np
from PIL import Image

im = Image.open("snake.png").convert('RGB')
im = im.resize((200,200),resample=Image.NEAREST)
im.save("result.png")

enter image description here

You can go from a Pillow image to a numpy array with:

numpy_array = np.array(pillowImage)

and from numpy array to a Pillow image with:

pillow_image = Image.fromarray(numpyArray)
like image 113
Mark Setchell Avatar answered Oct 31 '25 07:10

Mark Setchell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!