0

Вот мой код, но это не тот результат.

def newImg():
    image = Image.open('image.jpg')
 
    w, h = image.size
    
    pixel = []
    for x in range(w):
        for y in range(h):
            r, g, b = image.getpixel((x, y))
            pixel.append([r, g, b])
    
    avr = [sum(x)//len(x) for x in zip(*pixel)]
    img = Image.new('RGB', (w, h), (0,0,0,255))
    
    for i in range(0,w,4):
        for a in range(0,h,4):
            try:
                img.putpixel((i,a), (255,255,255,255))
                img.putpixel((i+1,a), (255,255,0,255))
                img.putpixel((i,a+1), (255,0,255,255))
                img.putpixel((i+1,a+1), (0,255,255,255))
            except:
                pass
    return img

wallpaper = newImg()
wallpaper.show()

Надо, чтобы через каждые n пикселей создавался новый квадрат из n пикселей.

UPD По такой схеме скрипт должен рисовать пиксели, тут 4 пикселя

Пример работы скрипта

1 ответ 1

0

Я неправильно поставил вопрос, я хотел пиксельную картинку. Вот решение:

from PIL import Image

def pixelate(image, pixel_size=8):

    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)

    return image

image = Image.open('image.jpg').convert('RGB')

image_pixelate = pixelate(image, pixel_size=16)
image_pixelate.save('output.jpg'.format(16))
image_pixelate.show()

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.