Distorting a photo with Python Pillow

How can I distort a photo, as shown below, by pixel spread, or narrow, or anything else?

Original image

Final result

Author: Kromster, 2019-04-20

1 answers

I was hooked on the question или вообще другое?.

And because I can show something different, how do you like pixelation?

from PIL import Image


def pixelate(image, pixel_size=9, draw_margin=True):
    margin_color = (0, 0, 0)

    image = image.resize((image.size[0] // pixel_size, image.size[1] // pixel_size), Image.NEAREST)
    image = image.resize((image.size[0] * pixel_size, image.size[1] * pixel_size), Image.NEAREST)
    pixel = image.load()

    # Draw black margin between pixels
    if draw_margin:
        for i in range(0, image.size[0], pixel_size):
            for j in range(0, image.size[1], pixel_size):
                for r in range(pixel_size):
                    pixel[i+r, j] = margin_color
                    pixel[i, j+r] = margin_color

    return image


if __name__ == '__main__':
    image = Image.open('image/input.jpg').convert('RGB')
    # image.show()

    image_pixelate = pixelate(image, draw_margin=False)
    image_pixelate.save('image/output_no_margin.jpg')
    # image_pixelate.show()

    image_pixelate = pixelate(image)
    image_pixelate.save('image/output.jpg')
    # image_pixelate.show()

    for size in (16, 32, 48):
        image_pixelate = pixelate(image, pixel_size=size)
        image_pixelate.save('image/output_{}.jpg'.format(size))
        # image_pixelate.show()

Results:

Pixel_size=9

enter a description of the image here


Pixel_size=16

enter a description of the image here


Pixel_size=32

enter a description of the image here


Pixel_size=48

enter a description of the image here


Pixel_size=9, draw_margin=False

enter a description of the image here

 4
Author: gil9red, 2019-04-24 14:50:21