J-Filters III

This filter swaps the red and blue color values. It results in a rather soft image.

The original image (can also be found on my Stacking Stones post).

The original image

RGB Swap (just swaps the red and blue color value, that is why the lake appears red).

RGB Swap


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 1st of September, 2015
# Jonathan Frech 3rd of October  , 2015

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

""" FUNCTIONS """
# 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

# 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(main.COLOR)
	
	n = 2
	
	for _ in range(0, main.WIDTH):
		x, y = main.I % main.WIDTH, main.I / main.WIDTH
		
		# [[
		
		c = main.IMG.get_at([x, y])
		main.IMG2.set_at([x, y], [
			c[2],
			c[1],
			c[0]
		])
		
		# ]]
		
		if main.I < main.WIDTH * main.HEIGHT - 1:
			main.I += 1
		
	main.SURF.blit(main.IMG2, [0, 0])
	
	# blit and flip
	main.SCREEN.blit(main.SURF, [0, 0])
	pygame.display.flip()

""" INIT """
# initialize program
def init():
	main.IMG = pygame.image.load(os.getcwd() + "/Original.jpg")
	r = main.IMG.get_rect()
	main.IMG = pygame.transform.scale(main.IMG, [r.width, r.height])
	
	
	
	r = main.IMG.get_rect()
	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 = "Filter"
	main.COLOR = [150, 0, 0]
	main.TICKS = 0
	main.KEYSDOWN = []
	
	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