J-Filters

Getting into image manipulation, I created a few filters to change the look and feel of digital images. Though the software is not the most efficient, the results are looking good.

For demonstration purposes I used an image from my post Stacking Stones.
The original image

This filter I call Cixel, because it combines square pixels with round circles.
The Cixel filter applied

Again working with circles, the Draw filter tries to imitate a drawn picture.
The Drawing filter applied

Using lines and a fixed point, thed Stretch filter stretches the image to the lower right corner.
The Stretch filter applied


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 21st of August, 2015
# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 21st of August, 2015
# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 21st of August, 2015


Cixel filter

# importing needed modules
import pygame, sys, time, math, os, random

""" CLASSES """
# dummy class for global variables
class dummy():
	pass

""" FUNCTIONS """
# quits the program
def quit():
	sys.exit()

""" TICK; RENDER """
# tick function
def tick():
	# handle events
	for event in pygame.event.get():
		# quit
		if event.type == pygame.QUIT:
			quit()
		
		if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
			if not os.path.exists(os.getcwd() + "/out"):
				try:
					os.mkdir(os.getcwd() + "/out")
				except:
					print "error: path"
			
			n = 0
			while os.path.exists(os.getcwd() + "/out/out" + str(n) + ".png"):
				n += 1
			
			try:
				pygame.image.save(main.IMG2, os.getcwd() + "/out/out" + str(n) + ".png")
			except:
				print "error: file"

# render function
def render():
	# fill
	main.SURF.fill([0, 0, 0])
	
	# Cixel filter (cixel -> circle-pixel)
	# apply filter
	n = 5
	for _ in range(0, main.WIDTH):
		x, y = main.I % main.WIDTH, main.I / main.WIDTH
		c0 = main.IMG.get_at([x, y])
		c = c0
		if x % (2*n) == 0 and y % (2*n) == 0:
			pygame.draw.circle(main.IMG2, c, [x, y], n)
		
		if main.I < main.WIDTH * main.HEIGHT - 1:
			main.I += 1
		else:
			pygame.display.set_caption(main.CAPTION + " (Done)")
	
	# blit and flip
	main.SURF.blit(main.IMG2, [0, 0])
	main.SCREEN.blit(main.SURF, [0, 0])
	pygame.display.flip()

""" INIT """
# initialize program
def init():
	# load image
	main.IMG = pygame.image.load(os.getcwd() + "/pic.jpg")
	r = main.IMG.get_rect()
	main.IMG = pygame.transform.scale(main.IMG, [r.width, r.height])
	r = main.IMG.get_rect()
	
	# variables
	main.WIDTH, main.HEIGHT = r.width, r.height
	main.SIZE = [main.WIDTH, main.HEIGHT]
	main.SCREEN = pygame.display.set_mode(main.SIZE)
	main.SURF = pygame.Surface(main.SIZE)
	
	main.CAPTION = "Cixel"
	main.TICKS = 0
	
	main.IMG2 = pygame.Surface(main.SIZE)
	main.I = 0
	
	# functions
	pygame.display.set_caption(main.CAPTION)

""" RUN """
# run function (uses tick() and render())
def run():
	ticksPerSecond = 60
	lastTime = time.time() * 1000000000
	nsPerTick =  1000000000.0 / float(ticksPerSecond)
	
	ticks = 0
	frames = 0
	
	lastTimer = time.time() * 1000
	delta = 0.0
	
	while True:
		now = time.time() * 1000000000
		delta += float(now - lastTime) / float(nsPerTick)
		lastTime = now
		shouldRender = False
				
		while delta >= 1:
			ticks += 1
			main.TICKS += 1
			tick()
			delta -= 1
			shouldRender = True
		
		if shouldRender:
			frames += 1
			render()
		
		if time.time() * 1000 - lastTimer >= 1000:
			lastTimer += 1000
			
			# debug
			# print("Frames: " + str(frames) + ", ticks: " + str(ticks))
			
			frames = 0
			ticks = 0

# main variable
main = dummy()
init()

# start program
run()

Drawing filter

# importing needed modules
import pygame, sys, time, math, os, random

""" CLASSES """
# dummy class for global variables
class dummy():
	pass

""" FUNCTIONS """
# quits the program
def quit():
	sys.exit()

""" TICK; RENDER """
# tick function
def tick():
	# handle events
	for event in pygame.event.get():
		# quit
		if event.type == pygame.QUIT:
			quit()
		
		if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
			if not os.path.exists(os.getcwd() + "/out"):
				try:
					os.mkdir(os.getcwd() + "/out")
				except:
					print "error: path"
			
			n = 0
			while os.path.exists(os.getcwd() + "/out/out" + str(n) + ".png"):
				n += 1
			
			try:
				pygame.image.save(main.IMG2, os.getcwd() + "/out/out" + str(n) + ".png")
			except:
				print "error: file"

# render function
def render():
	# fill
	main.SURF.fill([0, 0, 0])
	
	# Drawing filter
	# apply filter
	for _ in range(0, main.WIDTH):
		if random.randint(0, 10) == 0:
			x, y = main.I % main.WIDTH, main.I / main.WIDTH
			pygame.draw.circle(main.IMG2, main.IMG.get_at([x, y]), [x, y], 10)
		
		if main.I < main.WIDTH * main.HEIGHT - 1:
			main.I += 1
		else:
			pygame.display.set_caption(main.CAPTION + " (Done)")
	
	# blit and flip
	main.SURF.blit(main.IMG2, [0, 0])
	main.SCREEN.blit(main.SURF, [0, 0])
	pygame.display.flip()

""" INIT """
# initialize program
def init():
	# load image
	main.IMG = pygame.image.load(os.getcwd() + "/pic.jpg")
	r = main.IMG.get_rect()
	main.IMG = pygame.transform.scale(main.IMG, [r.width, r.height])
	r = main.IMG.get_rect()
	
	# variables
	main.WIDTH, main.HEIGHT = r.width, r.height
	main.SIZE = [main.WIDTH, main.HEIGHT]
	main.SCREEN = pygame.display.set_mode(main.SIZE)
	main.SURF = pygame.Surface(main.SIZE)
	
	main.CAPTION = "Drawing"
	main.TICKS = 0
	
	main.IMG2 = pygame.Surface(main.SIZE)
	main.I = 0
	
	# functions
	pygame.display.set_caption(main.CAPTION)

""" RUN """
# run function (uses tick() and render())
def run():
	ticksPerSecond = 60
	lastTime = time.time() * 1000000000
	nsPerTick =  1000000000.0 / float(ticksPerSecond)
	
	ticks = 0
	frames = 0
	
	lastTimer = time.time() * 1000
	delta = 0.0
	
	while True:
		now = time.time() * 1000000000
		delta += float(now - lastTime) / float(nsPerTick)
		lastTime = now
		shouldRender = False
				
		while delta >= 1:
			ticks += 1
			main.TICKS += 1
			tick()
			delta -= 1
			shouldRender = True
		
		if shouldRender:
			frames += 1
			render()
		
		if time.time() * 1000 - lastTimer >= 1000:
			lastTimer += 1000
			
			# debug
			# print("Frames: " + str(frames) + ", ticks: " + str(ticks))
			
			frames = 0
			ticks = 0

# main variable
main = dummy()
init()

# start program
run()

Stretch filter

# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 21st of August, 2015

# importing needed modules
import pygame, sys, time, math, os, random

""" CLASSES """
# dummy class for global variables
class dummy():
	pass

""" FUNCTIONS """
# quits the program
def quit():
	sys.exit()

""" TICK; RENDER """
# tick function
def tick():
	# handle events
	for event in pygame.event.get():
		# quit
		if event.type == pygame.QUIT:
			quit()
		
		if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
			if not os.path.exists(os.getcwd() + "/out"):
				try:
					os.mkdir(os.getcwd() + "/out")
				except:
					print "error: path"
			
			n = 0
			while os.path.exists(os.getcwd() + "/out/out" + str(n) + ".png"):
				n += 1
			
			try:
				pygame.image.save(main.IMG2, os.getcwd() + "/out/out" + str(n) + ".png")
			except:
				print "error: file"

# render function
def render():
	# fill
	main.SURF.fill([0, 0, 0])
	
	# Stretch filter
	# apply filter
	for _ in range(0, main.WIDTH):
		if random.randint(0, 2) == 0:
			x, y = main.I % main.WIDTH, main.I / main.WIDTH
			c0 = main.IMG.get_at([x, y])
			c = c0
			"""c = [0, 0, 0]
			n = 255/8
			for _ in range(0, 3):
				c[_] = c0[_] / n * n"""
			#main.IMG2.set_at([x, y], c)
			#pygame.draw.circle(main.IMG2, c, [x, y], 10)
			pygame.draw.line(main.IMG2, c, main.SIZE, [x, y])
		
		if main.I < main.WIDTH * main.HEIGHT - 1:
			main.I += 1
		else:
			pygame.display.set_caption(main.CAPTION + " (Done)")
	
	# blit and flip
	main.SURF.blit(main.IMG2, [0, 0])
	main.SCREEN.blit(main.SURF, [0, 0])
	pygame.display.flip()

""" INIT """
# initialize program
def init():
	# load image
	main.IMG = pygame.image.load(os.getcwd() + "/pic.jpg")
	r = main.IMG.get_rect()
	main.IMG = pygame.transform.scale(main.IMG, [r.width, r.height])
	r = main.IMG.get_rect()
	
	# variables
	main.WIDTH, main.HEIGHT = r.width, r.height
	main.SIZE = [main.WIDTH, main.HEIGHT]
	main.SCREEN = pygame.display.set_mode(main.SIZE)
	main.SURF = pygame.Surface(main.SIZE)
	
	main.CAPTION = "Stretch"
	main.TICKS = 0
	
	main.IMG2 = pygame.Surface(main.SIZE)
	main.I = 0
	
	# functions
	pygame.display.set_caption(main.CAPTION)

""" RUN """
# run function (uses tick() and render())
def run():
	ticksPerSecond = 60
	lastTime = time.time() * 1000000000
	nsPerTick =  1000000000.0 / float(ticksPerSecond)
	
	ticks = 0
	frames = 0
	
	lastTimer = time.time() * 1000
	delta = 0.0
	
	while True:
		now = time.time() * 1000000000
		delta += float(now - lastTime) / float(nsPerTick)
		lastTime = now
		shouldRender = False
				
		while delta >= 1:
			ticks += 1
			main.TICKS += 1
			tick()
			delta -= 1
			shouldRender = True
		
		if shouldRender:
			frames += 1
			render()
		
		if time.time() * 1000 - lastTimer >= 1000:
			lastTimer += 1000
			
			# debug
			# print("Frames: " + str(frames) + ", ticks: " + str(ticks))
			
			frames = 0
			ticks = 0

# main variable
main = dummy()
init()

# start program
run()
Advertisements

2 thoughts on “J-Filters

  1. Pingback: Primes II | J-Blog

  2. Pingback: J-Filters II | J-Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s