예제 #1
0
def calc_hist(i):
    matriu = img.matrix(i)
    llista = [0] * 256
    for fila in matriu:
        for pixel in fila:
            llista[pixel] += 1
    return llista
예제 #2
0
def luminance_img(i):
    """
    Transforms a RGB image to a L image using luminance
    """
    mat = img.matrix(i)
    matlum = [[rgb_to_lum(p) for p in F] for F in mat]
    return img.img(matlum, 'L')
예제 #3
0
def split_digit(imag):
    """
    Reotrna una tupla amb la imatge del primer nombre retallat seguida de la resta de la imatge
    >>> split_digit(("1", [[255,255,0,255,255,0,0,0,255],[255,0,0,255,255,255,255,255,255],[255,255,0,255,255,0,0,0,255],[255,255,0,255,255,0,0,0,255]]))
    (('1', [[255, 0], [0, 0], [255, 0], [255, 0]]), ('1', [[255, 255, 0, 0, 0, 255], [255, 255, 255, 255, 255, 255], [255, 255, 0, 0, 0, 255], [255, 255, 0, 0, 0, 255]]))
    """
    matrix = img.matrix(imag)
    if (img.format(img.img(matrix)) != "1"):  #Nomes pot ser blanci negre
        raise Exception("The format must be black and white")

    start = findBlack(
        matrix)  #Troba on comenca el nombre (columna de pixels negres)
    if (
            start == None
    ):  #Si no es troba pixels negres es retornara una imatge nula, ja que no hi ha cap mes nombre per retallar
        return img.null()

    finish = findWhite(
        matrix, start
    )  #Troba on acaba el nombre, es a dir a la columna abans d'on nomes hi ha una columna blanca

    croppedNumber = img.subimg(
        imag, start, 0, finish - start, len(matrix)
    )  #Retalla el nombre --> Des don comenca el nombre, amb l'amplada que te  = Final - comencament
    endCropped = img.subimg(imag, finish + 1, 0,
                            len(matrix[0]) - finish + 1,
                            len(matrix))  #La resta del nombre
    return (croppedNumber, endCropped
            )  #Retorna la tupla  amb el nombre retallat i la  resta
예제 #4
0
파일: split.py 프로젝트: Horiol/PyPlate
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()
예제 #5
0
파일: transf.py 프로젝트: Horiol/PyPlate
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
예제 #6
0
def rgb_to_bn(i):
    L = luminance_img(i)
    H = histogram(L)
    discr = get_threshold(H)
    matl = img.matrix(L)
    matbw = [[(0 if greyval < discr else 255) for greyval in f] for f in matl]
#    bw_mean(BW)
    return img.img(matbw, '1')
예제 #7
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)
예제 #8
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()
예제 #9
0
def histogram(i):
    """
    Histogram of grey values of `i`
    """
    h = [0] * 256
    for f in img.matrix(i):
        for greyval in f:
            h[greyval] += 1
    return h
예제 #10
0
파일: match.py 프로젝트: Horiol/PyPlate
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
예제 #11
0
def sim(smallImag, bigImag, start=0):
    """
    Retorna la similitud entre 2 imatges
    >>> sim(('1', [[255, 255], [255, 255], [255, 255]]),('1', [[255, 255], [255, 255], [255, 255]]))
    6
    >>> sim(('1', [[255, 0], [255, 255], [255, 255]]),('1', [[255, 0], [255, 0], [255, 255]]))
    5
    >>> sim(('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
    >>> sim(('1', [[255, 0], [255, 255], [255, 255]]),('1', [[255, 0, 255], [255, 0, 255], [255, 255, 255]]),1)
    3
    >>> sim(('1', [[255, 0, 255], [255, 255, 255], [255, 255, 255]]),('1', [[255, 0, 255], [255, 0, 255], [255, 255, 255]]),0)
    8
    """
    compareWhitem1 = 255  #El pixel blanc
    if (
            img.format(smallImag) == "RGB"
    ):  #Si el format es RGB s'haura de comparar amb el pixel blanc en format RGB
        compareWhitem1 = (255, 255, 255)

    compareWhitem2 = 255  #S'aplica de la mateixa manera per la imatge 2
    if (img.format(bigImag) == "RGB"):
        compareWhitem2 = (255, 255, 255)

    m1 = img.matrix(smallImag)  # Agafa les matrius de les imatges
    m2 = img.matrix(bigImag)

    samePixels = 0  #Pixels iguals
    for row in range(len(m1)):
        for col in range(len(m1[0])):
            if (
                    m1[row][col] == compareWhitem1
                    and m2[row][col + start] == compareWhitem2
            ):  #Si el pixel es blanc a les dues imatges la similitud es suma 1 als pixels en comu
                samePixels += 1
            elif (
                    m1[row][col] != compareWhitem1
                    and m2[row][col + start] != compareWhitem2
            ):  #Si el pixel no es blanc en les dues (es a dir que es negre) tambe se suma 1
                samePixels += 1
    return samePixels
예제 #12
0
파일: transf.py 프로젝트: Horiol/PyPlate
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)
예제 #13
0
def rgb_to_bn(i):
    img_luminancia = luminancia(i)
    histograma = calc_hist(img_luminancia)
    matriu = img.matrix(img_luminancia)
    llindar = calc_llinda(histograma)
    matriu_bn = []
    for fila in matriu:
        fila_n = []
        for pixel in fila:
            if pixel < llindar:
                fila_n += [0]
            else:
                fila_n += [255]
        matriu_bn += [fila_n]
    return ("1", matriu_bn)
예제 #14
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
예제 #15
0
파일: transf.py 프로젝트: Horiol/PyPlate
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
예제 #16
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.format(i),(img.get_w(i),img.get_h(i)))
    image.putdata([pixel for F in img.matrix(i) for pixel in F])
    image.show()


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.format(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)

if __name__=="__main__":
	image = Image.open("matricules/matricula1.jpeg")
	pix = image.load()
	X = image.size[0]
	Y = image.size[1]
	data = [[pix[x,y] for x in range(X)] for y in range(Y)]
	imatge=img.i(data)
	imatge_bn=discret.rgb_to_bn(imatge)
	data=img.matrix(imatge_bn)
	show(('L',data))
	save(('L',data),"prova.jpeg")
	print(data)