squarify

Howdy fellas.

It has been years since my Minecraft addiction but sometimes I still need some colorful pixelation, and as my fight to make my GPU work and installing Steam (you will know when I win as I won’t post here anymore), I don’t have delicious retro like games to satisfy me.

Then I’ll make my own pixel like stuff.

Well that would be easy and I could just do it with a photoshop filter. I want something more fun.

So here it is. What I do is just draw a square randomly in the canvas with the color of the average color that area should have been. As I draw more squares I lower the size slowly. The color of the square jumbles slightly by randomly increasing or decreasing each RGB value.

The resulting images have a soothing pixelation effect. But it seems to work better with colorful and contrasty images, rather than images with small details.

I hope you enjoy them.

 

 

#! /usr/bin/env python
import random
from scipy import misc
import matplotlib.pyplot as plt
import matplotlib.patches as patches

def get_average_color((x,y),n,image):
  return  image[x:x+n,y:y+n].mean((0,1))/255
  #I know one liners are as fun as evil but this one slices and averages at the same time.

def FILTER(f,n=10000,Wmin=5,Wmax=300,alpha=0.6,change=0.2):
  im 	= misc.imread(f)
  x,y,_ = im.shape
  fig1	= plt.figure(figsize=(x/100.,y/100.))
  ax1 	= fig1.add_subplot(111)
  ax1.set_xlim(0,x)
  ax1.set_ylim(0,y)
  ax1.axis('off')
  def R(x):
    x = x*random.choice([1+change,1-change])
    if x >1:return 1  #if the resulting RGB value is larger than one then return 1 otherwise there will be an error plotting
    return x
  for i in range(n)[::-1]: #I inverted the range so I can use the i value for setting the size of the square
    if i%1000==0: # just prints status and saves a sample of the process
      print i
      fig1.savefig('sample.png', dpi=100, bbox_inches='tight')
    Width = int(i/float(n)*(Wmax-Wmin))+Wmin
    X = lambda : random.randint(0,x-Width-1)
    Y = lambda : random.randint(0,y-Width-1)
    X = (X()+X())/2   # I was too lazy to use a standard distriburion so I just average two homogeneus distributions
    Y = (Y()+Y())/2   # I like the result so I kept it rather than fidgeting with a nice standard deviation for the gausian distribution
    r, g, b = get_average_color((X,Y), Width, im)
    ax1.add_patch(
    		patches.Rectangle(
        		(x-X,y-Y),   # (x,y)
        		Width,          # width
        		Width,          # height
                        color=(R(r),R(g),R(b)),
                        alpha=alpha
    		        )
                )
  out = f+'_OUT.png'
  print out
  fig1.savefig(out, dpi=100, bbox_inches='tight')
FILTER('1080p_IMG_20151004_150928.jpg',n=10000)     #just change the filename and you are clear to go
Pin on Pinterest0Buffer this pageEmail this to someoneShare on Facebook0Share on Google+0Flattr the authorDigg thisPrint this pageTweet about this on TwitterShare on LinkedIn0Share on Reddit0Share on StumbleUpon0Share on Tumblr0

Leave a Reply