from PIL import Image 

PATH = "/home/cugur/Downloads/matf/dozs/2024/week3/data/yugo.jpg"

# vrednost piksela p (0-255) mnozi koeficijentom kontrasta k i dodaje osvetljenje b.
def contrast_brightness_util(p, k, b):
    return p * k + b

def contrast_brightness_v1(p):
    return contrast_brightness_util(p, 2, -75)

def contrast_brightness_v2(p):
    return contrast_brightness_util(p, 0.3, 100)

def contrast_brightness_v3(p):
    return contrast_brightness_util(p, 0.3, -100)

def contrast_brightness_v4(p):
    return contrast_brightness_util(p, 0.3, 100)

with Image.open(PATH) as img:

    img.load()

    # Convert image to grayscale
    img1 = img.convert("L")
    img1.show()

    img2 = img1.point(contrast_brightness_v1)
    img2.show()

    # Zadatak: eksperimentisati sa razlicitim vrednostima contrast-a i brightness-a
    img3 = img1.point(contrast_brightness_v2)
    img3.show()

    # Zasto je slika crna - objasnjenjenje: 
    # Kada smo vrednost piksela koja je u rangu od 0 do 255 pomnozili sa kontrastom 0.3, time su svi pikseli 
    # sa slike uslu u opseg 0 od ~76. Kad tim pikselima oduzmemo 100, svi ce biti <0, sto je crna boja. 
    img4 = img1.point(contrast_brightness_v3)
    img4.show()

    img5 = img1.point(contrast_brightness_v4)
    img5.show()

    # Zadatak za vezbu: eksperimentisati sa raznim linearnim kombinacijama kontrasta i osvetljenja, ali na 
    # primeru slike 'river.jpg'. 


# Primer vezbanja: 
    
PATH2 = "/home/cugur/Downloads/matf/dozs/2024/week3/data/river.jpg"


def contrast_brightness_v5(p):
    return contrast_brightness_util(p, 3, -80)

with Image.open(PATH2) as img:
    
    img.load()

    img1 = img.convert("L")
    img1.show() 

    img2 = img1.point(contrast_brightness_v5)
    img2.show()