Changeset 7

Show
Ignore:
Timestamp:
09/23/07 11:59:05 (1 year ago)
Author:
steve
Message:

Faster, hovering text, nicer (refractored code), pyrex enabled now too

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • config.py

    r6 r7  
    1111import os 
    1212 
    13 taps = 16 
     13taps = 64 
    1414weights = (1,0.5, 0.75) 
    1515img_size = (256, 256) 
    1616crop_size= 100 
    17  
     17tmp_file=".89885e5234e2f0ae1e2015642425d1eb.jpg" 
     18font_size=12 
     19font_name="Monaco" 
    1820def main(): 
    1921        pass 
  • mactorii.py

    r6 r7  
    1010import sys 
    1111import os 
     12import math 
     13import Image 
    1214 
    1315import wavelet 
     
    1820from pyglet import image as pyglet_image 
    1921from pyglet import clock  
     22from pyglet.window import key 
     23from pyglet.window import mouse 
     24from pyglet import font 
     25 
     26files = None 
     27win = None 
    2028 
    2129yoffset = 0 
    2230xoffset = 0 
    23 rows = 0 
     31rows = 1 
     32xmotion = 0 
     33         
     34clickx = 0 
     35clicky = 0 
     36 
     37hoverx = 0 
     38hovery = 0 
     39 
     40selected = None 
     41 
     42def on_mouse_motion(x,y,dx,dy): 
     43        global hoverx 
     44        global hovery 
     45         
     46        hoverx = x 
     47        hovery = y 
     48         
     49def on_mouse_press(x, y, button, modifiers): 
     50        global clickx 
     51        global clicky 
     52         
     53        if button == mouse.LEFT: 
     54                clickx = x 
     55                clicky = y 
     56                 
     57def on_mouse_release(x,y,button, modifiers): 
     58        global clickx 
     59        global clicky 
     60         
     61        return 
     62        if button == mouse.LEFT: 
     63                clickx = -1 
     64                clicky = -1 
     65                         
     66def on_key_release(symbol, modifier): 
     67        global xoffset 
     68        global xmotion 
     69 
     70        if symbol == key.LEFT: 
     71                xmotion = 0 
     72         
     73        if symbol == key.RIGHT: 
     74                xmotion = 0 
     75                 
     76def on_key_press(symbol, modifier): 
     77        global xoffset 
     78        global xmotion 
     79 
     80        if symbol == key.LEFT: 
     81                        xmotion = 10 
     82         
     83        if symbol == key.RIGHT: 
     84                        xmotion -= 10 
     85 
     86def strip_width(): 
     87        """returns the width of the strip in pixels""" 
     88        global rows 
     89        return math.ceil(1.0 * len(files)/rows) * config.crop_size 
    2490         
    2591def on_resize(width, height): 
     
    2995        global rows 
    3096         
    31         rows = int(height/config.crop_size)      
     97        p = xoffset/strip_width() 
     98         
     99        rows = int(height/config.crop_size)  
    32100         
    33101        yoffset = (height - rows * config.crop_size)/2 
    34102        yoffset = height-yoffset-config.crop_size 
    35103         
    36 def main(): 
    37         global xoffset 
    38         global yoffset 
    39         global rows 
    40          
    41         files = sys.argv[1:] 
     104        # compute the new xoffset 
     105        xoffset = p * strip_width() 
     106         
     107def sort_func(item): 
     108        assert selected != None 
     109         
     110        selected_sig = selected[2] 
     111        item_sig = item[1][2] 
     112         
     113        score = [] 
     114        for b in xrange(3): 
     115                score.append(config.weights[b]*len(selected_sig[b].intersection(item_sig[b]))) 
     116        return -sum(score) 
     117 
     118def load_files(files): 
     119        """loads the files given in the command line""" 
    42120        images = dict() 
     121         
    43122        for file in files: 
    44123                print "processing file: %s"%(file) 
    45124                wi = wavelet.open(file) 
    46125                sig = wi.signature() 
    47                 wi.cleanup() 
    48                 img = pyglet_image.load(file) 
    49                 region = img.get_region(x=img.width/2-config.crop_size/2, \ 
    50                                                                 y=img.height/2-config.crop_size/2, \ 
    51                                                                 width=config.crop_size, height=config.crop_size) 
    52                 images[file] = (region, img, 0) 
    53                  
    54                  
     126                wi.im.thumbnail((config.crop_size, config.crop_size)) 
     127                wi.im.save(config.tmp_file) 
     128         
     129                thumbnail = pyglet_image.load(config.tmp_file) 
     130 
     131                images[file] = (thumbnail, None, sig, wi.size) 
     132                 
     133        return images 
     134         
     135def window_setup(): 
     136        """sets up our window""" 
    55137        win = window.Window(resizable=True, visible=False) 
     138         
    56139        win.push_handlers(on_resize) 
     140        win.push_handlers(on_key_press) 
     141        win.push_handlers(on_key_release) 
     142        win.push_handlers(on_mouse_press) 
     143        win.push_handlers(on_mouse_release) 
     144        win.push_handlers(on_mouse_motion) 
     145         
     146        win.set_visible() 
     147         
     148        return win 
     149         
     150def font_setup(): 
     151        """sets up fonts""" 
     152        return font.load(config.font_name, config.font_size) 
     153 
     154def is_over_image(x, y, mousex, mousey): 
     155        if mousex < 0 or mousey < 0: 
     156                return False 
     157                 
     158        if mousex < x or mousex > x + config.crop_size: 
     159                return False 
     160                 
     161        if mousey < y or mousey > y + config.crop_size: 
     162                        return False 
     163                                 
     164        return True 
     165         
     166def main(): 
     167        global xoffset 
     168        global xmotion 
     169        global yoffset 
     170        global rows 
     171        global files 
     172        global win 
     173        global clickx 
     174        global clicky 
     175        global hoverx 
     176        global hovery 
     177        global selected 
     178         
     179        files = sys.argv[1:] 
     180        images = load_files(files) 
     181         
     182        win = window_setup() 
     183        ft = font_setup() 
     184         
     185        assert win != None 
     186        assert ft != None 
    57187         
    58188        clock.set_fps_limit(30) 
    59         win.set_visible() 
     189         
     190        renderables = images.items() 
     191         
    60192        while not win.has_exit: 
    61193                clock.tick() 
     
    63195                glClear(GL_COLOR_BUFFER_BIT) 
    64196                 
    65                 x = xoffset 
     197                if xmotion < 0: 
     198                        if strip_width() + xoffset > win.width: 
     199                                xoffset+=xmotion 
     200                                 
     201                if xmotion > 0: 
     202                        if xoffset < 0: 
     203                                xoffset+=xmotion 
     204                                 
     205                x = xoffset  
    66206                y = yoffset 
     207                pix_name = None 
     208                pix_size = None 
     209                 
    67210                drawn = 0 
    68                 for image in images.values()
     211                for filename, image in renderables
    69212                        img = image[0] 
    70213                        img.blit(x,y) 
    71214                         
     215                        if is_over_image(x,y,clickx, clicky): 
     216                                print "%s selected"%(filename) 
     217                                selected = image 
     218                                renderables.sort(key=sort_func) 
     219                                 
     220                                clickx = -1 
     221                                clicky = -1 
     222                                 
     223                                xoffset = 0 
     224                                break 
     225                                 
     226                        if is_over_image(x,y,hoverx, hovery): 
     227                                pix_name =      font.Text(ft, filename, hoverx, hovery+config.font_size+5) 
     228                                pix_size = font.Text(ft, "%dx%d"%(image[3][0], image[3][0]), hoverx, hovery+5) 
    72229                        drawn+=1 
    73230                        y-=config.crop_size 
     
    76233                                x+=config.crop_size 
    77234                                y = yoffset 
     235                if pix_name: 
     236                        pix_name.draw()          
     237                if pix_size: 
     238                        pix_size.draw() 
    78239                                 
    79240                win.flip() 
  • wavelet.py

    r6 r7  
    1111import os 
    1212import Image 
     13 
    1314import config 
     15import pyx 
    1416 
    1517def open(path): 
     
    3234                         
    3335                im = Image.open(path) 
    34                 im = im.resize(config.img_size) 
     36                self.size = im.size 
     37                 
     38                im.thumbnail(config.img_size) 
    3539                im = im.convert("RGB") 
    36                 im = im.convert("RGB", rgb2yiq) 
    3740                 
    38                 self.data = im.getdata() 
    39                 self.im = Image.open(path) 
     41                self.data = im.convert("RGB", rgb2yiq).getdata() 
     42                self.im = im 
    4043                self.wavelets = None 
    4144                self.sig = None 
     
    133136                input = list(self.data) 
    134137                 
    135                 self.wavelets = self.transform_array(input) 
     138                #self.wavelets = self.transform_array(input) 
     139                self.wavelets = pyx.pyx_transform_array(input) 
    136140                return 
    137141