Jedan program koji je vrlo koristan za obradu slika za igre. Npr. imate 2d sprite koji ste dobili renderom u blenderu ili nekom drugom 3d programu. Program ce smanjiti velicinu sprite-a na najmanju mogucu velicinu koju on dozvoljava (napraviti sto manji okvir oko slike) i obrisati sve zamrljanje pixele, tacnije dati im boju transparentnog pixela. Program nije jos sasvim optimizovan ali sluzi svrsi. Potreban vam je pygame i pyrex(za kompajliranje pyx filea).
Code:
#main.py
from boost import*
import pygame, time
pygame.init()
image = pygame.image.load('ime_slike.bmp')
width = image.get_width()
height = image.get_height()
trans_color = image.get_at((0,0))
pixels = []
def pregled(w,h, image):
for x in xrange(width):
for y in xrange(height):
test_pixel = image.get_at((x,y))
if provera(30, test_pixel,trans_color):
pixels.append((x,y))
else:
if test_pixel != trans_color:
image.set_at((x,y),trans_color)
print 'Pregled slike...'
t = time.time()
pregled(width,height, image)
print 'Pregled zavrsen. Vreme: ',time.time()-t
xmax = 0
xmin = width
ymax = 0
ymin = height
print 'Najvece i najmanje vrednosti...'
t= time.time()
for x,y in pixels:
if x > xmax:
xmax = x
elif x < xmin:
xmin = x
if y > ymax:
ymax = y
elif y < ymin:
ymin = y
print ' Vrdnosti odredjene. Vreme: ', time.time()-t
rect = pygame.Rect(xmin,ymin, xmax-xmin,ymax-ymin)
new_surface = pygame.Surface((xmax-xmin,ymax-ymin))
new_surface.blit(image, (0,0), rect)
pygame.image.save(new_surface, 'izlaz.bmp')
print xmin, ymin
Code:
#boost.pyx
cdef izmedju(int n, int b1,int b2):
if b1<=n<=b2:
return 1
else:
return 0
def provera(int razmak, c1, c2):
if c1 == c2:
return 0
elif izmedju(c1[0], c2[0]-razmak, c2[0]+razmak)\
and izmedju(c1[1], c2[1]-razmak, c2[1]+razmak)\
and izmedju(c1[2], c2[2]-razmak, c2[2]+razmak):
return 0
else:
return 1
The earth teaches us more about ourselves than all the books. Because it resists us. Man discovers himself when he measures himself against the obstacle.