J-Filters IV

As in the previous post, I used pygame’s rotozoom() function. This time to turn an image around the screen while shrinking it in size to get an unusual image filter. The original image can be found in Stacking Stones post.

This filter is called Rotozoom.

``````# Python 2.7.7 Code
# Pygame 1.9.1 (for Python 2.7.7)
# Jonathan Frech 5th of January, 2015``````

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

""" FUNCTIONS """
# validates a color integer
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 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) )
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]]

""" GAME """
# game class
class GAME():
# initialize program
def __init__(self):
path = os.getcwd() + "/original.jpg"
self.IMG = pygame.image.load(path)
rect = self.IMG.get_rect()

#self.IMG = pygame.transform.scale(self.IMG, [rect.right/5, rect.bottom/5])
#rect = self.IMG.get_rect()

self.N = 0

self.WIDTH, self.HEIGHT = rect.right, rect.bottom
self.SIZE = [self.WIDTH, self.HEIGHT]
self.SURF = pygame.Surface(self.SIZE)
self.SCREEN = pygame.display.set_mode(self.SIZE)

self.TICKS = 0
self.RUNNING = True

self.N = self.WIDTH/2

# functions
pygame.display.set_caption("Rotofilter")

# 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()

# render function
def render(self):
#self.SURF.blit(pygame.transform.rotozoom(self.IMG, self.TICKS, 1./self.TICKS), [1./self.TICKS * self.WIDTH/2, 1./self.TICKS * self.HEIGHT/2])

# number of turns
n = 3

# draw
if self.N % (360*(n+1)) != 0:
self.N += 1
img = pygame.transform.rotozoom(self.IMG, self.N, (self.WIDTH/3.5)/self.N)
rect = img.get_rect()
self.SURF.blit(img, [(self.WIDTH - rect.right)/2, (self.HEIGHT - rect.bottom)/2])

# blit and flip
self.SCREEN.blit(self.SURF, [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.SURF, 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
GAME().run()``````
Advertisements

One thought on “J-Filters IV”

1. Pingback: The hundredth Post | J-Blog