J-Filters II

Playing around with pygame and image manipulation, I created another set of filters. My previous filters can be found here.

For demonstration purposes I used an image from my post Stacking Stones.
The original imageThis is a simple blur filter. It takes the average of pixel attached to one another.

Blur filter

This filter changes the color depth. A usual image has color values from 0 to 255 on red, green and blue. The filter changes those values to 0 to 8.

Color Depth filter

Mix-up repositions the pixels from their original position. So it creates a mixed-up image.

Mix-up


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 1st of September, 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 22nd of August, 2015


Blur filter

# importing needed modules
import pygame, sys, time, math, os, random
pygame.font.init()
""" 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()
		
		# save image
		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])
	
	# Blur filter
	n = 2
	for _ in range(0, main.WIDTH):
		x, y = main.I % main.WIDTH, main.I / main.WIDTH
		
		# collects r, g and b
		r, g, b = 0, 0, 0
		t = 0
		for X in range(-n, n):
			for Y in range(-n, n):
				if x + X >= 0 and y + Y >= 0 and x + X < main.WIDTH and y + Y < main.HEIGHT:
					c = main.IMG.get_at([x + X, y + Y])
					r += c[0]
					g += c[1]
					b += c[2]
					t += 1
		
		# gets the average
		if t > 0:
			main.IMG2.set_at([x, y], [r/t, g/t, b/t])
		
		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():
	# loads the 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 = "Blur"
	main.TICKS = 0
	
	main.IMG2 = pygame.Surface(main.SIZE)
	main.I = 0
	main.FONT = pygame.font.SysFont(None, 50)
	
	# 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()

Color Depth 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()
		
		# save image
		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])
	
	# Color Depth filter
	for _ in range(0, main.WIDTH):
		x, y = main.I % main.WIDTH, main.I / main.WIDTH
		
		# gets color
		c0 = main.IMG.get_at([x, y])
		
		# decreases the depth value (0-255 -> 0-8)
		c = [0, 0, 0]
		n = 255/8
		for _ in range(0, 3):
			c[_] = c0[_] / n * n
		main.IMG2.set_at([x, y], c)
		
		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.CENTER = [main.WIDTH / 2., main.HEIGHT / 2.]
	main.SCREEN = pygame.display.set_mode(main.SIZE)
	main.SURF = pygame.Surface(main.SIZE)
	
	main.CAPTION = "Color Depth"
	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()

Mix-up filter

# importing needed modules
import pygame, sys, time, math, os, random
pygame.font.init()
""" CLASSES """
# dummy class for global variables
class dummy():
	pass

""" FUNCTIONS """
# gets the mouse position
def getMousePos():
	p = pygame.mouse.get_pos()
	return [p[0], p[1]]

# validates color integer
# extra feature: _min and _max implementation
def colorValid(_color, _min = 0, _max = 255):
	newColor = math.fabs(_color)
	n = _max - _min
	if newColor > n:
		if int(newColor / n) % 2 == 0:
			newColor = newColor % n
		else:
			newColor = n - (newColor % n)
	
	return int(newColor) + _min

# gets the position on a circle
# circle center                           : '_pos'
# circle radius                           : '_radius'
# angle from center to point on the circle: '_angle'
def getCirclePos(_pos, _radius, _angle):
	return [
				_pos[0] + _radius * math.cos(math.radians(_angle)),
				_pos[1] + _radius * math.sin(math.radians(_angle))
			]

# returns an integer version of given positon
def intpos(_pos):
	return [int(_pos[0]), int(_pos[1])]

# basic vector functions
def vecConvert(p1, p2):
	return [p2[0] - p1[0], p2[1] - p1[1]]
def vecLen(vec):
	return math.sqrt( (vec[0]**2) + (vec[1]**2) )
def vecMultiply(vec, n):
	return [vec[0] * n, vec[1] * n]
def vecGetPoint(vec, point):
	return [point[0] + vec[0], point[1] + vec[1]]
def vecAdd(vec1, vec2):
	return [vec1[0] + vec2[0], vec1[1] + vec2[1]]

# calculates distance between given positions
def posDistance(p1, p2):
	return math.sqrt( (p2[0] - p1[0])**2 + (p2[1] - p1[1])**2 )

# 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()
		
		# save image
		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"
				main.KEYSDOWN.append(event.key)

# render function
def render():
	# fill
	main.SURF.fill([0, 0, 0])
	
	# Mix-up filter
	n = 2
	for _ in range(0, main.WIDTH):
		x, y = main.I % main.WIDTH, main.I / main.WIDTH
		
		# get color
		c0 = main.IMG.get_at([x, y])
		c = c0
		
		# sets pixel at randomly changed positon
		main.IMG2.set_at([x + random.randint(-10, 10), y + random.randint(-10, 10)], c)
		
		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 = "Mix-up"
	main.TICKS = 0
	
	main.IMG2 = pygame.Surface(main.SIZE)
	main.I = 0
	main.FONT = pygame.font.SysFont(None, 50)
	
	# 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

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