Python 3.6.6 |Anaconda custom (64-bit)| (default, Oct 9 2018, 12:34:16)

Type "copyright", "credits" or "license" for more information.


IPython 7.0.1 -- An enhanced Interactive Python.


In [1]: def filtra(img, filtro):

   ...: return [ [ filtro(c) for c in riga] for riga in img ]


In [2]: import image as im

Traceback (most recent call last):


File "<ipython-input-2-b01fa460936e>", line 1, in <module>

import image as im


ModuleNotFoundError: No module named 'image'



In [3]:


In [3]: import image as im


In [4]: trecime = im.load('3cime.png')


In [5]: im.visd(trecime)



In [6]: g = filtra(trecime, lambda p: ( sum(p)//3, sum(p)//3, sum(p)//3))


In [7]: im.visd(g)



In [8]: def contrasto(canale, fattore):

   ...: diff = canale - 128


In [9]: def contrasto(canale, fattore):

   ...: diff = canale - 128


In [10]: def contrasto(canale, fattore):

    ...: diff = canale - 128

    ...: diff *= fattore


In [11]: def contrasto(canale, fattore):

    ...: diff = canale - 128

    ...: diff *= fattore

    ...: return min(255, max(0, round(128 + diff)))


In [12]: def contrasto(canale, fattore):

    ...: diff = canale - 128

    ...: diff *= fattore

    ...: return min(255, max(0, round(128 + diff)))


In [13]: con = filtra(img, lambda p: (contrasto(p[0], 2), contrasto(p[1], 2), contrasto(p[2],2) ))

Traceback (most recent call last):


File "<ipython-input-13-cb6db241b458>", line 1, in <module>

con = filtra(img, lambda p: (contrasto(p[0], 2), contrasto(p[1], 2), contrasto(p[2],2) ))


NameError: name 'img' is not defined



In [14]:


In [14]: con = filtra(trecime, lambda p: (contrasto(p[0], 2), contrasto(p[1], 2), contrasto(p[2],2) ))


In [15]: im.visd(con)



In [16]: im.visd(trecime)



In [17]: def scramble(img, s):

    ...: w = len(img[0])

    ...: h = len(img)

    ...: res = create(w, h)


In [18]: def scramble(img, s):

    ...: w = len(img[0])

    ...: h = len(img)

    ...: res = create(w, h)

    ...: for x in range(w):

    ...: for y in range(h):

    ...: X = x


In [19]: import random


In [20]: def scramble(img, s):

    ...: w = len(img[0])

    ...: h = len(img)

    ...: res = create(w, h)

    ...: for x in range(w):

    ...: for y in range(h):

    ...: X = x + random.randint(-s,s)


In [21]: def scramble(img, s):

    ...: w = len(img[0])

    ...: h = len(img)

    ...: res = create(w, h)

    ...: for x in range(w):

    ...: for y in range(h):

    ...: X = x + random.randint(-s,s)

    ...: Y = y + random.randint(-s,s)


In [22]: def scramble(img, s):

    ...: w = len(img[0])

    ...: h = len(img)

    ...: res = create(w, h)

    ...: for x in range(w):

    ...: for y in range(h):

    ...: X = x + random.randint(-s,s)

    ...: Y = y + random.randint(-s,s)

    ...: try:

    ...: res[y][x] = img[Y][X]

    ...: except:

    ...: pass


In [23]: def scramble(img, s):

    ...: w = len(img[0])

    ...: h = len(img)

    ...: res = create(w, h)

    ...: for x in range(w):

    ...: for y in range(h):

    ...: X = x + random.randint(-s,s)

    ...: Y = y + random.randint(-s,s)

    ...: try:

    ...: res[y][x] = img[Y][X]

    ...: except:

    ...: pass

    ...: return res


In [24]: s = scramble(trecime, 10)

Traceback (most recent call last):


File "<ipython-input-24-d7aef91d8bd6>", line 1, in <module>

s = scramble(trecime, 10)


File "<ipython-input-23-56ee47e91111>", line 4, in scramble

res = create(w, h)


NameError: name 'create' is not defined



In [25]:


In [25]: def create(w, h, colore=(0,0,0)):

    ...: return [ [ colore for _ in range(w) ]

    ...: for _ in range(h) ]


In [26]: s = scramble(trecime, 10)


In [27]: im.visd(s)



In [28]: def scramble(img, s):

    ...: w = len(img[0])

    ...: h = len(img)

    ...: res = create(w, h)

    ...: for x in range(w):

    ...: for y in range(h):

    ...: X = x + random.randint(-s,s)

    ...: X = min(X, w-1)

    ...: X = max(X, 0)

    ...: Y = y + random.randint(-s,s)

    ...: Y = min(Y, h-1)

    ...: Y = max(Y, 0)

    ...: res[y][x] = img[Y][X]

    ...: return res


In [29]: s = scramble(trecime, 10)


In [30]: im.visd(s)



In [31]: def filtro_immagine(img, filtro):

    ...: return [ [ filtro(c, x, y, img) for x, c in enumerate(riga) ]

    ...: for y, riga in enumerate(img) ]


In [32]: def random_pixel(c, x, y, img):

    ...: w = len(img[0])


In [33]: def random_pixel(c, x, y, img):

    ...: w = len(img[0])


In [34]: def random_pixel(c, x, y, img):

    ...: w = len(img[0])

    ...: h = len(img)


In [35]: def random_pixel(c, x, y, img):

    ...: w = len(img[0])

    ...: h = len(img)

    ...: X = x + random.randint(-10, 10)


In [36]: def random_pixel(c, x, y, img):

    ...: w = len(img[0])

    ...: h = len(img)

    ...: X = x + random.randint(-10, 10)

    ...: X = min(X, w-1)

    ...: X = max(X, 0)

    ...: Y = y + random.randint(-10,10)

    ...: Y = min(Y, h-1)

    ...: Y = max(Y, 0)

    ...: return img[Y][X]


In [37]: s1 = filtro_immagine(trecime, random_pixel)


In [38]: im.visd(s1)



In [39]: def random_pixel(c, x, y, img):

    ...: w = len(img[0])

    ...: h = len(img)

    ...: X = x + random.randint(-10, 10)

    ...: X = min(X, w-1)

    ...: X = max(X, 0)

    ...: Y = y + random.randint(-10,10)

    ...: Y = min(Y, h-1)

    ...: Y = max(Y, 0)

    ...: return img[Y][X]


In [40]: def draw_rect(img, x, y, w, h, colore):

    ...: w = len(img[0])


In [41]: def draw_rect(img, x, y, w, h, colore):

    ...: w = len(img[0])

    ...: h = len(img)

    ...: for X in range(x, x+w):

    ...: for Y in range(y+h):

    ...: try:

    ...: img[Y][X] = colore

    ...: except:

    ...: pass


In [42]: def draw_rect(img, x, y, w, h, colore):

    ...: w = len(img[0])

    ...: h = len(img)

    ...: for X in range(x, x+w):

    ...: for Y in range(y+h):

    ...: try:

    ...: img[Y][X] = colore

    ...: except:

    ...: pass

    ...: return img


In [43]: def draw_rect(img, x, y, w, h, colore):

    ...: w = len(img[0])

    ...: h = len(img)

    ...: for X in range(x, x+w):

    ...: for Y in range(y+h):

    ...: try:

    ...: img[Y][X] = colore

    ...: except:

    ...: pass


In [45]: def pixellata(img, s):

    ...: w = len(img[0])

    ...: h = len(img)

File "<tokenize>", line 3

h = len(img)

^

IndentationError: unindent does not match any outer indentation level



In [46]:


In [46]: def pixellata(img, s):

    ...: w = len(img[0])

    ...: h = len(img)

    ...: res = create(w,h)

    ...: for X in range(0, w, s):

    ...: for Y in range(0, h, s):

    ...: draw_rect(res, X, Y, s, s, img[Y][X])


In [47]: def pixellata(img, s):

    ...: w = len(img[0])

    ...: h = len(img)

    ...: res = create(w,h)

    ...: for X in range(0, w, s):

    ...: for Y in range(0, h, s):

    ...: draw_rect(res, X, Y, s, s, img[Y][X])

    ...: return res


In [48]: p = pixellata(trecime, 10)


In [49]: im.visd(p)



In [50]: def draw_rect(img, x, y, w, h, colore):

    ...: w = len(img[0])

    ...: h = len(img)

    ...: for X in range(x, x+w):

    ...: for Y in range(y, y+h):

    ...: try:

    ...: img[Y][X] = colore

    ...: except:

    ...: pass


In [51]: p = pixellata(trecime, 10)


In [52]: im.visd(p)



In [53]: def draw_rect(img, x, y, w, h, colore):

    ...:  for X in range(x, x+w):

    ...: for Y in range(y, y+h):

    ...: try:

    ...: img[Y][X] = colore

    ...: except:

    ...: pass


In [54]: p = pixellata(trecime, 10)


In [55]: im.visd(p)



In [56]: def pixellata_avg(img, s):

    ...: w = len(img[0])

    ...: h = len(img)

    ...: res = create(w,h)

    ...: for X in range(0, w, s):

    ...: for Y in range(0, h, s):

    ...: c = media_colori(img, X, Y, s, s)

    ...: draw_rect(res, X, Y, s, s, c)

    ...: return res


In [57]: def media_colori(img, x, y, w, h):

    ...: c = [0, 0, 0]


In [58]: def media_colori(img, x, y, w, h):

    ...: c = [0, 0, 0]


In [59]: def media_colori(img, x, y, w, h):

    ...: c = [0, 0, 0]


In [60]: def media_colori(img, x, y, w, h):

    ...: c = [0, 0, 0]

    ...: n = 0

    ...: for X in range(x, x+w):

    ...: for Y in range(y, y+h):

    ...: for canale in range(3):

    ...: try:

    ...: c[canale] += img[Y][X][canale]

    ...: n += 1

    ...: except:

    ...: pass


In [61]: def media_colori(img, x, y, w, h):

    ...: c = [0, 0, 0]

    ...: n = 0

    ...: for X in range(x, x+w):

    ...: for Y in range(y, y+h):

    ...: try:

    ...: for canale in range(3):

    ...: c[canale] += img[Y][X][canale]

    ...: n += 1

    ...: except:

    ...: pass


In [62]: def media_colori(img, x, y, w, h):

    ...: c = [0, 0, 0]

    ...: n = 0

    ...: for X in range(x, x+w):

    ...: for Y in range(y, y+h):

    ...: try:

    ...: for canale in range(3):

    ...: c[canale] += img[Y][X][canale]

    ...: n += 1

    ...: except:

    ...: pass

    ...: for canale in range(3):

File "<ipython-input-62-1411d521d6dc>", line 12

for canale in range(3):

^

SyntaxError: unexpected EOF while parsing



In [63]:


In [63]: def media_colori(img, x, y, w, h):

    ...: c = [0, 0, 0]

    ...: n = 0

    ...: for X in range(x, x+w):

    ...: for Y in range(y, y+h):

    ...: try:

    ...: for canale in range(3):

    ...: c[canale] += img[Y][X][canale]

    ...: n += 1

    ...: except:

    ...: pass

    ...: for canale in range(3):

    ...: c[canale) /= n

File "<ipython-input-63-a8948300d07c>", line 13

c[canale) /= n

^

SyntaxError: invalid syntax



In [64]:


In [64]: def media_colori(img, x, y, w, h):

    ...: c = [0, 0, 0]

    ...: n = 0

    ...: for X in range(x, x+w):

    ...: for Y in range(y, y+h):

    ...: try:

    ...: for canale in range(3):

    ...: c[canale] += img[Y][X][canale]

    ...: n += 1

    ...: except:

    ...: pass

    ...: for canale in range(3):

    ...: c[canale] /= n

    ...: c[canale] = min(255, max(0, round(c[canale])))


In [65]: def media_colori(img, x, y, w, h):

    ...: c = [0, 0, 0]

    ...: n = 0

    ...: for X in range(x, x+w):

    ...: for Y in range(y, y+h):

    ...: try:

    ...: for canale in range(3):

    ...: c[canale] += img[Y][X][canale]

    ...: n += 1

    ...: except:

    ...: pass

    ...: for canale in range(3):

    ...: c[canale] /= n

    ...: c[canale] = min(255, max(0, round(c[canale])))

    ...: return tuple(c)


In [66]: p3 = pixellata_avg(trecime, 10)


In [67]: im.visd(p3)



In [68]: im.visd(trecime)

    ...:



In [69]: im.visd(p)



In [70]: def luminosita_media(img, x, y, w, h):

File "<ipython-input-70-26847214da4b>", line 1

def luminosita_media(img, x, y, w, h):

^

SyntaxError: unexpected EOF while parsing



In [71]:


In [71]: def luminosita_media(img, x, y, w, h):

    ...: c = media_colori(img, x, y, w, h)

    ...: media = sum(c)//3


In [72]: def luminosita_media(img, x, y, w, h):

    ...: c = media_colori(img, x, y, w, h)

    ...: media = sum(c)//3

    ...: return media


In [73]: def luminosita_media(img, x, y, w, h):

    ...: c = media_colori(img, x, y, w, h)

    ...: media = sum(c)/3

    ...: return media


In [74]: def luminosita_media(img, x, y, w, h):

    ...: c = media_colori(img, x, y, w, h)

    ...: media = sum(c)/3

    ...: return media/255


In [75]: def mosaico_luminosita(img, s):

    ...: w = len(img[0])

    ...: h = len(img)

    ...: res = create(w, h)

    ...: for X in range(0, w, s):

    ...: for Y in range(0, h, s):

    ...: media = luminosita_media(img, X, Y, s, s)

    ...: s1 = round(s*media)


In [77]: def mosaico_luminosita(img, s):

    ...: w = len(img[0])

    ...: h = len(img)

    ...: res = create(w, h)

    ...: for X in range(0, w, s):

    ...: for Y in range(0, h, s):

    ...: media = luminosita_media(img, X, Y, s, s)

    ...: s1 = round(s*media)

    ...: ds = (s-s1)//2

    ...: draw_rect(res,X+ds, Y+ds, s1, s1, (255, 255, 255))

    ...: return res

File "<tokenize>", line 11

return res

^

IndentationError: unindent does not match any outer indentation level



In [78]:


In [78]: def mosaico_luminosita(img, s):

    ...: w = len(img[0])

    ...: h = len(img)

    ...: res = create(w, h)

    ...: for X in range(0, w, s):

    ...: for Y in range(0, h, s):

    ...: media = luminosita_media(img, X, Y, s, s)

    ...: s1 = round(s*media)

    ...: ds = (s-s1)//2

    ...: draw_rect(res,X+ds, Y+ds, s1, s1, (255, 255, 255))

    ...: return res


In [79]: m = mosaico_luminosita(trecime, 10)


In [80]: im.visd(m)



In [81]: m = mosaico_luminosita(trecime, 50)


In [82]: im.visd(m)



In [83]: m = mosaico_luminosita(trecime, 5)


In [84]: im.visd(m)



In [85]: def gradienteh(w, h, c0, c1):

File "<ipython-input-85-b04d3d1c27de>", line 1

def gradienteh(w, h, c0, c1):

^

SyntaxError: unexpected EOF while parsing



In [86]:


In [86]: def gradienteh(w, h, c0, c1):

    ...: img = create(w, h)


In [87]: def gradienteh(w, h, c0, c1):

    ...: img = create(w, h)

    ...: for x in range(w):

    ...: fattore = x/w


In [88]: def gradienteh(w, h, c0, c1):

    ...: img = create(w, h)

    ...: for x in range(w):

    ...: fattore = x/w


In [89]: def gradienteh(w, h, c0, c1):

    ...: img = create(w, h)

    ...: for x in range(w):

    ...: fattore = x/w

    ...: r0, g0, b0 = c0

    ...: r1, g1, b1 = c1

    ...: r = r0*(1-fattore) + r1*fattore


In [90]: def gradienteh(w, h, c0, c1):

    ...: img = create(w, h)

    ...: for x in range(w):

    ...: fattore = x/w

    ...: r0, g0, b0 = c0

    ...: r1, g1, b1 = c1

    ...: r = r0*(1-fattore) + r1*fattore

    ...: g = g0*(1-fattore) + g1*fattore

    ...: b = b0*(1-fattore) + b1*fattore

    ...: for y in range(h):

    ...: img[y][x] r, g, b

    ...: return img

    ...:

    ...:

    ...:

File "<ipython-input-90-f1f15296c299>", line 11

img[y][x] r, g, b

^

SyntaxError: invalid syntax



In [91]:


In [91]: def gradienteh(w, h, c0, c1):

    ...: img = create(w, h)

    ...: for x in range(w):

    ...: fattore = x/w

    ...: r0, g0, b0 = c0

    ...: r1, g1, b1 = c1

    ...: r = r0*(1-fattore) + r1*fattore

    ...: g = g0*(1-fattore) + g1*fattore

    ...: b = b0*(1-fattore) + b1*fattore

    ...: for y in range(h):

    ...: img[y][x] = r, g, b

    ...: return img

    ...:

    ...:

    ...:


In [92]: gh = gradienteh(500, 200, (255, 0, 0), (0, 0, 255))


In [93]: im.visd(gh)



In [94]: gh = gradienteh(500, 200, (255, 0, 0), (0, 0, 255))


In [95]: def gradienteh(w, h, c0, c1):

    ...: img = create(w, h)

    ...: for x in range(w):

    ...: fattore = x/w

    ...: r0, g0, b0 = c0

    ...: r1, g1, b1 = c1

    ...: r = int(r0*(1-fattore) + r1*fattore)

    ...: g = int(g0*(1-fattore) + g1*fattore)

    ...: b = int(b0*(1-fattore) + b1*fattore)

    ...: for y in range(h):

    ...: img[y][x] = r, g, b

    ...: return img


In [96]: gh = gradienteh(500, 200, (255, 0, 0), (0, 0, 255))


In [97]: im.visd(gh)



In [98]: