Ejemplo n.º 1
0
def simImage(imag, imag2):
    """
    Retorna la similtud entre dos imatges (un nombre de pixels)
    >>> simImage(('1', [[255,0,255],[255,0,255],[255,0,255]]),('1',[[255,255,0,255,255,255],[255,0,255,255,255,255],[0,255,255,255,255,255]]))
    6
    >>> simImage(('1', [[255, 0], [255, 255], [255, 255]]),('1', [[255, 0], [255, 0], [255, 255]]))
    5
    >>> simImage(('1', [[255, 0], [255, 255], [255, 255]]),('RGB', [[(255,255,255), (0,0,0)], [(255,255,255), (0,0,0)], [(255,255,255), (255,255,255)]]))
    5
    """
    widthImg = img.get_w(imag)  #Amplada de la imatge 1 (nombre retallat)
    widthImg2 = img.get_w(imag2)  #Amplada de la imatge 2 (patro a comparar)
    if (widthImg == widthImg2):  #Si tenen la mateixa amplada
        return sim(
            imag, imag2
        )  #Retorna directament la similitud entre les imatges comparant els pixels
    else:  #Si no tenen la mateixa amplada cal probar totes les combinacions i agafar la que te mes pixels en comu
        diference = abs(widthImg2 - widthImg)  #La diferencia d'amplada
        if (
                widthImg > widthImg2
        ):  #Si la imatge 1 es mes gran que la imatge 2 passara la 2 com a smallImag i la 1 com a bigImag
            return simBiggerImage(imag2, imag, diference)
        elif (
                widthImg2 > widthImg
        ):  #Si la imatge 2 es mes gran que la imatge 1 passara la 1 com a smallImag i la 2 com a bigImag
            return simBiggerImage(imag, imag2, diference)
Ejemplo n.º 2
0
def split_digit(i):
    """
    Agafa una imatge en blanc i negre i retorna una tupla (D,R) on D és la imatge amb el dígit de més a l¡esquerre i R la resta de la imatge
    
    >>> split_digit(('1',[[255,0,0,0,255,255],[0,0,255,0,255,0],[0,0,0,0,255,0]]))
    (('1', [[255, 0, 0, 0, 255], [0, 0, 255, 0, 255], [0, 0, 0, 0, 255]]), ('1', [[255], [0], [0]]))
    """
    matriu = img.matrix(i)
    h = img.get_h(i)
    w = img.get_w(i)
    c = 0
    while c < w:
        j = 0
        compare = 0
        while j < h:
            if matriu[j][c] == 255:
                compare += 1
            j += 1
        if compare == h:
            W = (c + 1)
            D = img.subimg(i, 0, 0, W, h)
            R = img.subimg(i, (W + 1), 0, (w - W), h)
            return (D, R)
        else:
            c += 1
    return img.null()
Ejemplo n.º 3
0
def vtrim(i):
    """
    Donada una imatge img en blanc i negre, retorna l'imatge resultant de retallar-la verticalment
    >>> vtrim(('1',[[255,255,255,255],[255,0,255,255],[255,255,255,255],[255,255,0,255]]))
    ('1', [[255, 255], [0, 255], [255, 255], [255, 0]]
    """
    matriu = img.matrix(i)
    h = img.get_h(i)
    w = img.get_w(i)
    wo = ""
    wf = 1
    c = 0
    while c < w:
        f = 0
        while f < h:
            if wo == "" and matriu[f][c] == 0:
                wo = c
            if wo != "" and matriu[f][c] == 0:
                wf = c
            f += 1
        c += 1
    if wo == "":
        return img.null()
    else:
        img_n = img.subimg(i, wo, 0, wf, h)
        return img_n
Ejemplo n.º 4
0
def save(i, nomf):
    """
    Donada una imatge i un nom de fitxer, crea el fitxer imatge a
    partir de la matriu.
    """
    image = Image.new(img.format1(i), (img.get_w(i), img.get_h(i)))
    image.putdata([pixel for F in img.matrix(i) for pixel in F])
    image.save(nomf)
Ejemplo n.º 5
0
def show(i):
    """
    Donada una imatge, la mostra en un visualitzador a la terminal. 
    Principalment serveix per a depurar el projecte.
    """
    image = Image.new(img.format1(i), (img.get_w(i), img.get_h(i)))
    image.putdata([pixel for F in img.matrix(i) for pixel in F])
    image.show()
Ejemplo n.º 6
0
def sim(A,B):
    """
    >>> sim(('1',[[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,255,255,0]]),('1',[[255,255],[255,255],[255,255],[255,255]]))
    2
    """
    matriu_A=img.matrix(A)
    matriu_B=img.matrix(B)
    wa=img.get_w(A)
    wb=img.get_w(B)
    h=img.get_h(A)
    sim_f=0
    if wa>=wb:
        cnt=0
        while cnt<=(wa-wb):
            sim=0
            x=0
            while x<h:
                y=0
                while y<wb:
                    if matriu_A[x][y+cnt]==matriu_B[x][y]:
                        sim+=1
                    y+=1
                x+=1
            if sim>sim_f:
                sim_f=sim
            cnt+=1
    
    elif wb>wa:
        cnt=0
        while cnt<=(wb-wa):
            sim=0
            x=0
            while x<h:
                y=0
                while y<wa:
                    if matriu_B[x][y+cnt]==matriu_A[x][y]:
                        sim+=1
                    y+=1
                x+=1
            if sim>sim_f:
                sim_f=sim
            cnt+=1

    return sim_f
Ejemplo n.º 7
0
def luminancia(i):
    matriu = img.matrix(i)
    w = img.get_w(i)
    h = img.get_h(i)
    imatge_grey = img.white_grey(w, h)
    imatge_grey = imatge_grey[1]
    i = 0
    while i < h:
        j = 0
        while j < w:
            pixel = matriu[i][j]
            pixel_grey = (pixel[0] + pixel[1] + pixel[2]) / 3
            imatge_grey[i][j] = pixel_grey
            j += 1
        i += 1
    imatge = ('L', imatge_grey)
    return imatge
Ejemplo n.º 8
0
def scale(i, h):
    """
    Escala homogeniament una imatge 'i' fins que la seva alçada es 'h'
    >>> scale(('1',[[0,255,255,255],[255,255,255,255],[255,255,255,255],[255,255,255,255]]),2)
    ('1', [[0, 255], [255, 255]])
    """
    matriu = img.matrix(i)
    W = img.get_w(i)
    H = img.get_h(i)
    fh = float(H) / float(h)
    w = W / fh
    w = int(w)
    matriu_n = img.matrix(img.white_bw(w, h))
    f = 0
    while f < h:
        c = 0
        while c < w:
            matriu_n[f][c] = matriu[int(f * fh)][int(c * fh)]
            c += 1
        f += 1
    return ('1', matriu_n)
Ejemplo n.º 9
0
def htrim(i):
    """
    Donada una imatge img en blanc i negre, retorna la imatge resultant de retallar-la hortizontalment
    >>> htrim(('1',[[255,255,255,255],[255,0,255,255],[255,0,255,255],[255,255,255,255]]))
    ('1', [[255, 0, 255, 255], [255, 0, 255, 255]])
    """
    matriu = img.matrix(i)
    w = img.get_w(i)
    ho = ""
    hf = 1
    f = 0
    for fila in matriu:
        for columna in fila:
            if ho == "" and columna == 0:
                ho = f
            if ho != "" and columna == 0:
                hf = (f - ho + 1)
        f += 1
    if ho == "":
        return img.null()
    else:
        img_n = img.subimg(i, 0, ho, w, hf)
        return img_n