detail_adder.py

Created by mathieu-croslacoste

Created on January 03, 2025

1.26 KB

Slow script that filters images:

blur() and add() do pretty much the same thing, fast_add() too but is less good and faster.

contrast() lessen the number of colors by rounding their values.

b_w() turns black and white

col() adds the given color to the image

lin() create spaced lines.


from kandinsky import*
def blur():
 for x in range(321):
  for y in range(222):
   s=[0]*3
   for i in range(9):
    v=get_pixel(x-1+i%3,y-1+i//3);c=2 if i!=0 else 1
    for j in(0,1,2):s[j]+=c*v[j]
   set_pixel(x,y,(s[0]//17,s[1]//17,s[2]//17))
def contrast():
 for x in range(321):
  for y in range(222):
   s=list(get_pixel(x,y))
   for i in(0,1,2):s[i]=round(s[i]/15)*15
   set_pixel(x,y,s)
def add():
 for x in range(321):
  for y in range(222):
   s=[0]*3
   for i in range(9):
    v=get_pixel(x-1+i%3,y-1+i//3);c=4 if i==4 else 2 if i%3 else.65
    for j in(0,1,2):s[j]+=c*v[j]
   set_pixel(x,y,(s[0]//17,s[1]//17,s[2]//17))
def fast_add():
 for x in range(321):
  for y in range(222):
   s=[0]*3
   for i in range(4):
    v=get_pixel(x+[0,-1,1][i%3],y+[0,1,-1][i%3==0+i==3])
    for j in(0,1,2):s[j]+=v[j]
   set_pixel(x,y,(s[0]//4,s[1]//4,s[2]//4))
def b_w():
 for x in range(321):
  for y in range(222):
   c=0;v=get_pixel(x,y)
   for j in(0,1,2):c+=v[j]
   set_pixel(x,y,(c//3,)*3)
def col(d,i=0):
 for x in range(321):
  for y in range(222):v=get_pixel(x,y);c=v[i];set_pixel(x,y,(c//d[0],c//d[1],c//d[2]))
def lin(w,d=0,c=(0,)*3):
 for p in range((222*(d==1)+320*(d==0))//int(w*2)):fill_rect(2*p*w*(d==0),2*p*w*(d==1),320*(d==1)+w*(d==0),222*(d==0)+w*(d==1),c)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.