def show_edges(filename, direction='x', offset=0):
    '''Aplica um filtro Sobel à imagem, exibe e salva o resultado'''

    original = Image.open(in_file(filename)).convert('L')
    XSOBEL = ImageFilter.Kernel((3, 3), [-1, 0, 1, -2, 0, 2, -1, 0, 1], 1,
                                offset)
    YSOBEL = ImageFilter.Kernel((3, 3), [-1, -2, -1, 0, 0, 0, 1, 2, 1], 1,
                                offset)
    if direction == 'x':
        filtered = original.filter(XSOBEL)
    elif direction == 'y':
        filtered = original.filter(YSOBEL)
    else:
        vsobel = original.filter(XSOBEL)
        hsobel = original.filter(YSOBEL)
        w, h = original.size
        filtered = Image.new('L', (w, h))

        for i in range(w):
            for j in range(h):
                value = sqrt(
                    vsobel.getpixel((i, j))**2 + hsobel.getpixel((i, j))**2)
                value = int(min(value, 255))
                filtered.putpixel((i, j), value)

    # Mostrar as imagens lado a lado
    show_horizontal(original, filtered)
    filtered.save(
        out_file('{}_{}sobel_{}.jpg'.format(filename[:filename.index('.')],
                                            direction, offset)))
def show_box_blur(filename, r=1):
    '''Aplica um filtro BoxBlur à imagem, exibe e salva o resultado'''

    original = Image.open(in_file(filename))
    filtered = original.filter(ImageFilter.BoxBlur(r))

    #Mostrar as imagens lado a lado
    show_horizontal(original, filtered)
    filtered.save(
        out_file('{}_boxblur_{}.jpg'.format(filename[:filename.index('.')],
                                            r)))
Example #3
0
        for y in range(h):
            pxl = colored.getpixel((x,y))
            # média das coordenadas RGB
            lum = (pxl[0] + pxl[1] + pxl[2])//3
            img.putpixel((x,y), (lum, lum, lum))
    return img

# Escala de Cinza: https://youtu.be/_3VcRHwZpPU
def grayscale(colored):
    w, h = colored.size
    img = Image.new("RGB", (w, h))

    for x in range(w):
        for y in range(h):
            pxl = colored.getpixel((x,y))
            # média ponderada das coordenadas RGB
            lum = int(0.3*pxl[0] + 0.59*pxl[1] + 0.11*pxl[2])
            img.putpixel((x,y), (lum, lum, lum))
    return img
            

if __name__ == "__main__":
    img = Image.open(in_file("baloes-pb.jpg"))
    print(img.getpixel((100, 100)))
    print(img.getpixel((500, 300)))
    print(img.getpixel((300, 180)))

    baloes = Image.open(in_file("baloes.jpg"))
    pb_baloes = grayscale(baloes)
    pb_baloes.save(out_file("pb_baloes2.jpg"))
    
        for y in range(h):
            pxl = colored.getpixel((x,y))
            # média das coordenadas RGB
            lum = (pxl[0] + pxl[1] + pxl[2])//3
            img.putpixel((x,y), (lum, lum, lum))
    return img

# Escala de Cinza: https://youtu.be/_3VcRHwZpPU
def grayscale(colored):
    w, h = colored.size
    img = Image.new("RGB", (w, h))

    for x in range(w):
        for y in range(h):
            pxl = colored.getpixel((x,y))
            # média ponderada das coordenadas RGB
            lum = int(0.3*pxl[0] + 0.59*pxl[1] + 0.11*pxl[2])
            img.putpixel((x,y), (lum, lum, lum))
    return img
            

if __name__ == "__main__":
    img = Image.open(in_file("baloes.jpg"))
    # print(img.getpixel((100, 100)))
    # print(img.getpixel((500, 300)))
    # print(img.getpixel((300, 180)))

    # pb_baloes = grayscale(baloes)
    # pb_baloes.save(out_file("pb_baloes2.jpg"))
    grayscale(img).save(out_file("pb-baloes.jpg"))
    
Example #5
0
from PIL import Image
from utils import in_file
import os

# abre a imagem
image = Image.open(in_file("pensador.jpg"))
# exibe o valor do pixel na posição (500, 500)
print(image.getpixel((500, 500)))
# exibe a imagem
image.show()

# Vídeo: O que é uma imagem?
# https://youtu.be/T9V_axU6jU8