A matrix wallpaper with Python and PIL – 1

Let’s start trying to make a wallpaper with a matrix code kind of things using PIL to make interesting effects. Trying to make it with a program like inkscape could be a little hard, if you are not so skilled, because soon there will be too many suff to handle. Otherwise, we want to experiment new stuff coding, so here is an example of how to make it with python and PIL. The code is very basic, but it could be more complex if we spend some more time with it with some interesting ideas. Maybe we can add some other interesting effects in the future. For now, this is what we achieved.

It is something extremely basic. In fact, we made it with a couple of lines of code as you can se in this post below and in the video too.

The code is this:

from PIL import Image, ImageDraw, ImageFilter
from random import randint
i = Image.new("RGB", (800,600), "black")
d = ImageDraw.Draw(i)
w,h = i.size
for x in range(0, w, 8):
    for y in range(0, h, 8):
        bool = randint(0,1)
        d.text((x,y), str(bool), "lime") # left corner up (h=0);
i = i.filter(ImageFilter.SMOOTH)
i.save("base\\matrix.png")
i.show()

The filters

The filters that you can use with ImageFilter are these:

  • BLUR
  • CONTOUR
  • DETAIL
  • EDGE_ENHANCE
  • EDGE_ENHANCE_MORE
  • EMBOSS
  • FIND_EDGES
  • SHARPEN
  • SMOOTH
  • SMOOTH_MORE

For more information go to the official documentation page here.

For a quick cheat sheet about PIL go in my glitch page here.

The live coding video

Mod 1: opacity

Whit the following code the wallpaper changes its look, using different opacity for each 0 and 1.

from PIL import Image, ImageDraw, ImageFilter
from random import randint, random
i = Image.new("RGBA", (800,600), "black")
d = ImageDraw.Draw(i)
w,h = i.size
for x in range(0, w, 8):
    for y in range(0, h, 8):
        boolean = randint(0,1)
        bool2 = int(random()*100)
        d.text((x,y), str(boolean), (0,230,0,bool2)) # left corner up (h=0);
#i = i.filter(ImageFilter.SMOOTH)
i.save("base\\matrix2.png")
i

Mod 2: colors and numbers

from PIL import Image, ImageDraw, ImageFilter, ImageFont
from random import randint, random
i = Image.new("RGBA", (800,600), "black")
num =20
font = ImageFont.truetype("impact.ttf", num)
d = ImageDraw.Draw(i)
w,h = i.size
for x in range(0, w, num-8):
    for y in range(0, h, num-5):
        boolean = randint(0,10)
        bool2 = int(random()*100)
        d.text((x,y), str(boolean), (y//4,230-x//4,x//4,bool2), font=font) # left corner up (h=0);
i = i.filter(ImageFilter.SMOOTH)
i.save("base\\matrix3.png")
i

Mod 3: letters and numbers carnival

from PIL import Image, ImageDraw, ImageFilter, ImageFont
from random import randint, random, choice
i = Image.new("RGBA", (800,600), "black")
num = 28
font = ImageFont.truetype("impact.ttf", num)
d = ImageDraw.Draw(i)
w,h = i.size
for x in range(0, w, num//2):
    for y in range(0, h, num//4):
        boolean = choice("abcdefg123456ghtnjkdfpeodòwpekccndzwxqr")
        opacity = 255#int(random()*100)
        ca, cb, cc = [randint(0,255) for x in range(3)]
        d.text((x,y), str(boolean), (ca,cb,cc,opacity), font=font) # left corner up (h=0);
#i = i.filter(ImageFilter.SMOOTH)
#i = i.filter(ImageFilter.CONTOUR)
#i = i.filter(ImageFilter.SMOOTH)
i.save("base\\matrix3.png")
i

Mod 4: sparse

from PIL import Image, ImageDraw, ImageFilter, ImageFont
from random import randint, random, choice
i = Image.new("RGBA", (800, 300), "black")
num = 24
font = ImageFont.truetype("impact.ttf", num)
d = ImageDraw.Draw(i)
w,h = i.size
for x in range(0, w, num//2):
    for y in range(0, h, num//2):
        x1 = randint(10, 790)
        y1 = randint(10, 590)
        num = randint(12,28)
        boolean = choice("ab cdefg1 23456ghtnjkdfpeo dòwpek ccndz wxqr")
        opacity = 100 + int(random()*155)
        ca, cb, cc = [randint(0,255) for x in range(3)]
        d.text((x1,y1), str(boolean), (ca,cb,cc,opacity), font=ImageFont.truetype("impact.ttf", num)) # left corner up (h=0);
#i = i.filter(ImageFilter.SMOOTH)
# i = i.filter(ImageFilter.CONTOUR)
#i = i.filter(ImageFilter.SMOOTH)
i.save("base\\matrix3.png")
i

You can make a lot of effect changing some parameters in the code, so express your creativity making some incredible visual effect.

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.