J-Filters VI

Using a vector and an angle, rotating each pixel’s position by an angle determined by its distance to the image’s center and a constant, this filter gives the image a unique spin.
For demonstration I used – once again – an image from my post Stacking Stones.

A light spin A medium spin Heavy spin!


# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 19th of February, 2016

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

# this constant changes the spin
CONSTANT = .9

""" FUNCTIONS """
# gets the position on a circle with a position, radius and an 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) )

""" GAME """
# game class
class GAME():
	# initialize program
	def __init__(self):
		self.image = pygame.image.load(os.getcwd() + "/original.jpg")
		self.rect = self.image.get_rect()
		self.image = pygame.transform.scale(self.image, [self.rect.right / 2, self.rect.bottom / 2])
		self.rect = self.image.get_rect()

		self.shiftx, self.shifty = 300, 300

		self.width, self.height = self.rect.right + self.shiftx*2, self.rect.bottom + self.shifty*2
		self.size = [self.width, self.height]
		self.surface = pygame.Surface(self.size)
		self.screen = pygame.display.set_mode(self.size)

		self.ticks = 0
		self.running = True

		self.i = 0

		# functions
		pygame.display.set_caption("J-Filters VI")
	
	# tick function
	def tick(self):
		# handle events
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				self.quit()
			if event.type == pygame.KEYDOWN:
				if event.key == pygame.K_SPACE:
					self.screenshot()

		# loop through
		for _ in range(0, self.width):
			# get positions
			x, y = self.i % self.rect.right, self.i / self.rect.right
			p = [self.rect.right / 2., self.rect.bottom / 2.]

			# calculate pixel's angle
			angle = math.atan2(y - p[1], x - p[0])
			angle = math.degrees(angle)
			
			# calculate pixel's distance to the center
			vec = vecConvert(p, [x, y])
			vecl = vecLen(vec)

			# calculate angle change
			angleadd = vecl * CONSTANT

			# calculate new position
			P = intpos(getCirclePos(p, vecl, angle+angleadd))
			P = [P[0]+self.shiftx, P[1]+self.shifty]
			
			# draw
			pygame.draw.circle(self.surface, self.image.get_at([x, y]), P, 2)

			# advance running variable i
			if self.i < (self.rect.right * self.rect.bottom)-1:
				self.i += 1

	# render function
	def render(self):
	

		# blit and flip
		self.screen.blit(self.surface, [0, 0])
		pygame.display.flip()
	
	# quits
	def quit(self):
		self.running = False

	# takes a screenshot
	def screenshot(self):
		path = os.getcwd() + "/out/"
		
		try:
			if not os.path.isdir(path):
				os.mkdir(path)
			
			name = "img" + str(len(os.listdir(path))) + ".png"
			pygame.image.save(self.surface, path + name)
		except:
			pass

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

# start game
G = GAME()
G.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