villares / material-aulas

Material para ensino introdutório de programação com Python em um contexto visual
https://abav.lugaralgum.com/material-aulas/
97 stars 70 forks source link

dúvida trab final #124

Closed ftgarrafoli closed 2 years ago

ftgarrafoli commented 2 years ago

Oi Alê, blz?

Estou tentando fazer em python o exemplo de "copying neighbors" (pág.316 a 318) do livro "Processing Creative Coding and Generative Art in Processing 2", mas por alguma razão o código não altera a imagem de referência...poderia ajudar?

from random import choice

def setup():
    global img
    img = loadImage("urban.jpg")
    size (1466, 568) # tamanho da imagem em pixels

def draw():
    image(img, 0, 0)
    loadPixels()
    # seleção do pixel - coordenadas (x, y)
    x = int(random(1466))
    y = int(random(568))
    # seleção de um pixel vizinho
    vizinhos = [(x-1,y-1), (x,y-1), (x+1,y-1), 
                (x-1,y), (x+1,y), 
                (x-1,y+1), (x,y+1), (x+1,y+1)]
    func = choice(vizinhos)
    if func == (x-1,y-1):
        newX = x - 1
        newY = y - 1
    if func == (x,y-1):
        newX = x
        newY = y - 1
    if func == (x+1,y-1):
        newX = x + 1
        newY = y - 1
    if func == (x-1,y):
        newX = x - 1
        newY = y
    if func == (x+1,y):
        newX = x + 1
        newY = y
    if func == (x-1,y+1):
        newX = x - 1
        newY = y + 1
    if func == (x,y+1):
        newX = x
        newY = y + 1
    if func == (x+1,y+1):
        newX = x + 1
        newY = y + 1  
    print x, y, newX, newY
    c = img.get(newX, newY) # busca a cor do pixel vizinho
    set(x, y, c) # transfere a cor do vizinho para o pixel sorteado
    updatePixels()
villares commented 2 years ago

Salve!

Olha, o image() no draw estava apagando tudo... até aí tudo bem Agora o estranho é que o loadPixels() estava apagando tudo também (e eu não entendi o motivo) Como você não esta usando loadPixels e udatePixels (pois não está consultando os pixels da tela em forma de um array/lista) eu desliguei eles.

Testei aqui com a imagem do exemplo do IDE, o LoadDisplayImage :


from random import choice

def setup():
    global img
    img = loadImage("moonwalk.jpg")    # Load the image into the program
    #size(640, 360) # tamanho da imagem em pixels
    size(200, 100)  # testando uma regiao menor
    #image(img, 0, 0)  # se quiser a imagem no fundo...
    img.loadPixels()  # para poder usar o img.pixels depois... o update precisa só se alterar pixels

def draw():

    #loadPixels()  # equivalente ao img.loadPixels() mas para a tela
    # seleção do pixel - coordenadas (x, y)
    x = int(random(width))
    y = int(random(height))
    # seleção de um pixel vizinho
    vizinhos = [(x-1,y-1), (x,y-1), (x+1,y-1), 
                (x-1,y), (x+1,y), 
                (x-1,y+1), (x,y+1), (x+1,y+1)]
    func = choice(vizinhos)
    if func == (x-1,y-1):
        newX = x - 1
        newY = y - 1
    if func == (x,y-1):
        newX = x
        newY = y - 1
    if func == (x+1,y-1):
        newX = x + 1
        newY = y - 1
    if func == (x-1,y):
        newX = x - 1
        newY = y
    if func == (x+1,y):
        newX = x + 1
        newY = y
    if func == (x-1,y+1):
        newX = x - 1
        newY = y + 1
    if func == (x,y+1):
        newX = x
        newY = y + 1
    if func == (x+1,y+1):
        newX = x + 1
        newY = y + 1  
    #print x, y, newX, newY
    # c = img.get(newX, newY) # busca a cor do pixel vizinho
    c = img.pixels[x + y * img.height]  # isso é pra ser um pouco mais rápido que o img.get(x, y)... 
    #stroke(c)
    #point(x, y)
    set(x, y, c)  # transfere a cor do vizinho para o pixel sorteado
    #updatePixels()
ftgarrafoli commented 2 years ago
from random import choice

def setup():
    global img
    img = loadImage("aurora.jpg")
    size (256, 256) # tamanho da imagem em pixels
    #image(img, 0, 0)
    #img.loadPixels()
    noSmooth()

def draw():
    image(img, 0, 0)
    img.loadPixels()
    # seleção do pixel - coordenadas (x, y)
    for _ in range(1000):
        x = int(random(width))
        y = int(random(height))
        # seleção de um pixel vizinho
        vizinhos = [(x-1,y-1), (x,y-1), (x+1,y-1), 
                    (x-1,y), (x+1,y), 
                    (x-1,y+1), (x,y+1), (x+1,y+1)]
        func = choice(vizinhos)
        if func == (x-1,y-1):
            newX = x - 1
            newY = y - 1
        if func == (x,y-1):
            newX = x
            newY = y - 1
        if func == (x+1,y-1):
            newX = x + 1
            newY = y - 1
        if func == (x-1,y):
            newX = x - 1
            newY = y
        if func == (x+1,y):
            newX = x + 1
            newY = y
        if func == (x-1,y+1):
            newX = x - 1
            newY = y + 1
        if func == (x,y+1):
            newX = x
            newY = y + 1
        if func == (x+1,y+1):
            newX = x + 1
            newY = y + 1  
        #print x, y, newX, newY
        c = img.get(newX, newY) # busca a cor do pixel vizinho
        #c = img.pixels[x + y * img.height] # isso é pra ser um pouco mais rápido que o img.get(x, y)... 
        #stroke(c)
        #point(x, y)
        img.set(x, y, c) # transfere a cor do vizinho para o pixel sorteado
    img.updatePixels()
villares commented 2 years ago

Salve Fabio!

Estava olhando o seu trabalho, gostei bastante do resultado. Olhando com mais calma o funcionamento, repare que você não precisas dos ifs :)

image

villares commented 2 years ago
"""

O código executa a troca de cor de um pixel determinado randômicamente com um de seus 8 vizinhos - conforme exemplo 
do item "copying neighbors" (pág.316 a 318) do livro "Processing Creative Coding and Generative Art in Processing 2"

"""

from random import choice

def setup():
    global img
    img = loadImage("aurora.jpg")
    size (256, 256) # tamanho da imagem em pixels
    noSmooth()

def draw():
    image(img, 0, 0)
    img.loadPixels()
    # seleção do pixel - coordenadas (x, y)
    for _ in range(1000):
        x = int(random(width))
        y = int(random(height))
        # seleção de um pixel vizinho
        vizinhos = [(x-1,y-1), (x,y-1), (x+1,y-1), 
                    (x-1,y), (x+1,y), 
                    (x-1,y+1), (x,y+1), (x+1,y+1)]
        vizinho_sorteado = choice(vizinhos)
        newX, newY = vizinho_sorteado
        c = img.get(newX, newY) # busca a cor do pixel vizinho
        #c = img.pixels[x + y * img.height] # isso é pra ser um pouco mais rápido que o img.get(x, y)... 
        img.set(x, y, c) # transfere a cor do vizinho para o pixel sorteado
    img.updatePixels()
ftgarrafoli commented 2 years ago

Boa noite Alê!

Nossa assim o código ficou muito mais sintético, menos é mais!

Vou alterar no meu arquivo aqui, obrigado!

Abraços

Le ven. 19 nov. 2021 à 18:35, Alexandre B A Villares < @.***> a écrit :

"""

O código executa a troca de cor de um pixel determinado randômicamente com um de seus 8 vizinhos - conforme exemplo do item "copying neighbors" (pág.316 a 318) do livro "Processing Creative Coding and Generative Art in Processing 2"

"""

from random import choice

def setup():

global img

img = loadImage("aurora.jpg")

size (256, 256) # tamanho da imagem em pixels

noSmooth()

def draw():

image(img, 0, 0)

img.loadPixels()

# seleção do pixel - coordenadas (x, y)

for _ in range(1000):

    x = int(random(width))

    y = int(random(height))

    # seleção de um pixel vizinho

    vizinhos = [(x-1,y-1), (x,y-1), (x+1,y-1),

                (x-1,y), (x+1,y),

                (x-1,y+1), (x,y+1), (x+1,y+1)]

    vizinho_sorteado = choice(vizinhos)

    newX, newY = vizinho_sorteado

    c = img.get(newX, newY) # busca a cor do pixel vizinho

    #c = img.pixels[x + y * img.height] # isso é pra ser um pouco mais rápido que o img.get(x, y)...

    img.set(x, y, c) # transfere a cor do vizinho para o pixel sorteado

img.updatePixels()

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/villares/material-aulas/issues/124#issuecomment-974488091, or unsubscribe https://github.com/notifications/unsubscribe-auth/AWFWIEZRCLTJOQUJQP7GAILUM27KJANCNFSM5HRBXHLQ .

villares commented 2 years ago

Prezado @ftgarrafoli vou encerrar essa issue aqui... se você tiver ideias para explicar com mais detalhes a manipulação de pixels em imagems (assunto que acho que está bem fraco no meu material didático) a gente pode abrir uma nova issue, eu ficaria agradecido com a contribuição!