Commits

Brendan Howell committed 7fc7dce

cleaned up some examples

Comments (0)

Files changed (14)

pycessing/data/examples/arc.cess

-# just draws a circle on the screen
-def setup():
-    setStrokeWidth(5)
-    setFillState(False)
-
-def draw():
-    arc(50, 50, 50, 0, 90)

pycessing/data/examples/flickr-search.cess

 #pull a random image from flickr based on a search term
+#you will need to set your flickr api_key for this to work
 
 import json
 import urllib2
 def setup():
     global photo
     
-    pycessing.size(600,800)
+    pycessing.size(600,800)
+    pycessing.noLoop = True
     
     request = urllib2.urlopen(flickrapi + searchterm)
-    
     results = json.load(request)
+
     photo_list = results["photos"]["photo"]
-    
     selected = random.choice(photo_list)
     
     imgurl = "http://farm" + str(selected["farm"]) + ".staticflickr.com/"
     
     photo = Image(request)
     
-    pycessing.noLoop = True
+
 
     
 def draw():

pycessing/data/examples/pixel-static.cess

 import random
 
 def draw():
-	for n in range(100):
-		x = random.randint(0, pycessing.getWidth())
-		y = random.randint(0, pycessing.getHeight())
+    for n in range(100):
+        x = random.randint(0, pycessing.getWidth())
+        y = random.randint(0, pycessing.getHeight())
 		
-		r = random.randint(0, 255)
-		g = random.randint(0, 255)
-		b = random.randint(0, 255)
+        r = random.randint(0, 255)
+        g = random.randint(0, 255)
+        b = random.randint(0, 255)
 		
-		setPixel(x, y,  r, g, b)
+        setPixel(x, y, r, g, b)

pycessing/data/examples/screen-text.cess

+# it's pretty simple to draw text on the screen
 myText = ScreenText()
 
 def setup():
-	pycessing.noLoop = True
+    pycessing.noLoop = True
 	
 def draw():
-	myText.setText("hello piksels")
-	myText.draw(20,20)
+    print myText.color
+    myText.color = 255,0,0
+    myText.setText("hello piksels")
+    myText.draw(20,20)
 

pycessing/data/examples/screengrab.cess

-# just draws a circle on the screen
+# screenGrab("myfile.jpg") takes a snapshot of your image and saves
+# it to a file.
+# this boring example just draws a polygon on the screen and saves 
+# the result on the desktop
+# NOTE: change the file name and folder so this won't try to save to the
+#   application folder where pycessing lives
 points = [(10, 10), (10, 50), (60, 70), (90, 20) ]
 
 def setup():
 
 def draw():
     polygon(points)
-    screenGrab("test.jpg")
+    screenGrab("/home/mute/Desktop/test.jpg")

pycessing/data/examples/text-markov.cess

-import markov
-filestrings = open("/Users/idl/Desktop/freud.txt").read()
+# this is a rather wierd example that uses a markov chain
+# to create fake text.  you will need to supply your own 
+# training file if you want this to work.  Freud is fun
+# but you can use any standard text file.
+
+import markov
+
+trainingfile = "/Users/pycessing/Desktop/freud.txt"
+filestrings = open(trianingfile).read()
 generator = markov.Markov(filestrings)
 mytext = ScreenText()
 ypos = 0

pycessing/data/examples/text-wrapped.cess

 text = "Now is the time for all good Men to come to the aid of the party."
 
 def setup():
-	pycessing.noLoop = True
+    pycessing.noLoop = True
 	
 def draw():
-	myText.setText(text)
-	myText.draw_wrapped(10,20, 300)
+    myText.setText(text)
+    myText.draw_wrapped(10,20, 300)
 

pycessing/data/examples/textmixer.cess

-import markov
-mark = markov.Markov("freud.txt")
-mytext = ScreenText()
-ypos = 0
-
-def setup():
-	pass
-	
-def draw():
-	mytext.setText(mark.generate_markov_text(5))
-	mytext.draw(0, ypos)

pycessing/data/examples/twitter-search.cess

 
 def setup():
     global results
-    
+    
+    pycessing.noLoop = True    
     pycessing.size(800,600)
     
-    request = urllib2.urlopen(twitterapi + searchterm)
-    
+    request = urllib2.urlopen(twitterapi + searchterm)    
     results = json.load(request)
-    
-    pycessing.noLoop = True
-
+   
     
 def draw():
     ypos = 10
     for result in results["results"]:
-        mytext.setText(result["text"].strip())
+        mytext.setText(result["text"].strip())
+        print result["text"]
         mytext.draw(10,ypos)
         ypos += 30

pycessing/data/examples/twitter.cess

-#pull status updates from the twitter API
+#pull status updates for a particular user from the twitter API
 
 import json
 import urllib2
 def setup():
     global updates
     
-    pycessing.size(800,600)
+    pycessing.size(800,600)
+    pycessing.noLoop = True
     
     request = urllib2.urlopen(twitterapi + username)
-    
     updates = json.load(request)
     
-    pycessing.noLoop = True
 
 def draw():
     ypos = 10
     for update in updates:
-        mytext.setText(update["text"])
+        mytext.setText(update["text"])
+        print update["text"]
         mytext.draw(10,ypos)
         ypos += 30

pycessing/data/examples/wrapping-paper-circles.cess

+# this draws a generative pattern of circles
+# by using a "random walk" that varies values
+# randomly without jumping around.
+# I used it to make wrapping paper for gifts
 import random
 
-width = 1754
-height = 2481
+width = 1024
+height = 768
 steps = 10
 
 step = width / steps
         y += step
         print "stepped - x: " + str(x) + " y: " + str(y)
     print "done"
-    screenGrab("wrappingpaper-cir.jpg")
+    #screenGrab("wrappingpaper-cir.jpg")

pycessing/data/examples/wrapping-paper-squares.cess

-width = 1754
-height = 2481
+#this generates a pretty pantone-looking grid of colored squares
+#slowly changing color and value
+
+width = 1024
+height = 768
 steps = 10
 
 step = width / steps
         y += step
         print "stepped - x: " + str(x) + " y: " + str(y)
     print "done"
-    screenGrab("wrappingpaper-sq.jpg")
+    #screenGrab("wrappingpaper-sq.jpg")

pycessing/drawing_cairo.py

             self.machack = True  # check if we are on a mac
         else:
             self.machack = False
-        
+
     #set up cairo and pango contexts
     def setSurface(self, sdl_surface):
         self.sdl_surface = sdl_surface
         #subsurf.set_shifts((a, r, g, b))
         #subsurf.set_masks((am, rm, gm, bm))
 
-        
+
         self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
         self.ctx = cairo.Context(self.surface)
         self.pangoctx = pangocairo.CairoContext(self.ctx)
         self.font_map = pangocairo.cairo_font_map_get_default()
         self.families = self.font_map.list_families()
-        
+
     def setBackground(self, red, green=None, blue=None):
         if not(green):
             self.ctx.set_source_rgba(red/255.0, red/255.0, red/255.0, 1)
         else:
             self.ctx.set_source_rgba(red/255.0, blue/255.0, green/255.0, 1)
         self.ctx.paint()
-        
+
     def setStroke(self, red, green=None, blue=None, alpha=255, width=1):
         self.setStrokeWidth(width)
         self.setStrokeColor(red, green, blue, alpha)
-        
+
     def setStrokeColor(self, red, green=None, blue=None, alpha=255):
         #assume greyscale if only 1 parameter is specified
         if not(green):
             self.stroke_color = (red/255.0, red/255.0, red/255.0, alpha/255.0)
         else:
             self.stroke_color = (red/255.0, green/255.0, blue/255.0, alpha/255.0)
-    
+
     def setStrokeWidth(self, width):
         self.stroke_width = width
-        
+
     def setFillColor(self, red, green=None, blue=None, alpha=255):
         #assume greyscale if only 1 parameter is specified, assume you want fill
         if not(green):
         else:
             self.fill_color = (red/255.0, green/255.0, blue/255.0, alpha/255.0)
         self.fill = True
-        
+
     def setFillState(self, fill):
         self.fill = fill
-        
-    def rect(self, x, y, width, height): 
+
+    def rect(self, x, y, width, height):
         self.ctx.rectangle(x, y, width, height)
         self._fillAndStroke()
-    
+
     def circle(self, cx, cy, radius):
         self.ctx.arc(cx, cy, radius, 0, 2.0 * math.pi)
         self._fillAndStroke()
-    
+
     def ellipse(self, cx, cy, width, height):
         self.ctx.save()
         self.ctx.translate(cx, cy)
         self.ctx.arc(0.0, 0.0, 1.0, 0.0, 2.0 * math.pi)
         self.ctx.restore()
         self._fillAndStroke()
-    
+
     def arc(self, cx, cy, radius, startAngle, endAngle):
         self.ctx.arc(cx, cy, radius, math.radians(startAngle), math.radians(endAngle))
         self._fillAndStroke()
-        
+
     def line(self, x1, y1, x2, y2):
         self.ctx.move_to(x1, y1)
         self.ctx.line_to(x2, y2)
         self._fillAndStroke()
-    
+
     def polygon(self, pointlist):
         x, y = pointlist[0]
         self.ctx.move_to(x, y)
             self.ctx.line_to(x, y)
         self.ctx.close_path()
         self._fillAndStroke()
-                
+
     def curve(self, x1, y1, cx1, cy1, cx2, cy2, x2, y2):
         self.ctx.move_to(x1, y1)
         self.ctx.curve_to(cx1, cy1, cx2, cy2, x2, y2)
 
     def screenGrab(self, fileName):
         pygame.image.save(self.sdl_surface, fileName)
-        
+
     def drawText(self, x, y, txt):
         self.ctx.save()
         self.ctx.translate(x,y)
         self.pangoctx.show_layout(layout)
         print('F => %s %s'%layout.get_pixel_size())
         self.ctx.restore()
-        
+
     def _fillAndStroke(self):
         if(self.fill):
             self.ctx.set_source_rgba(self.fill_color[0], self.fill_color[1], self.fill_color[2], self.fill_color[3])
     #TODO: finish and test PDF output
     def renderToPDF(self, fileName):
     	self.surface = cairo.PDFSurface(fileName, self.sdl_surface.get_width(), self.sdl_surface.get_height())
-    	
 
-    def _blitToScreen(self):  
+
+    def _blitToScreen(self):
         dest = pygame.surfarray.pixels2d(self.sdl_surface)
         if self.machack:
             dest.data[:] = self.surface.get_data()[::-1]
         else:
             dest.data[:] = self.surface.get_data()
-        #tmp = pygame.transform.flip(self.sdl_surface, True, True)
-        #self.sdl_surface.fill((0,0,0))
-        #del dest
-        #self.sdl_surface.blit(tmp, (0,0)) 
-        #dest = pygame.surfarray.pixels2d(self.sdl_surface)
-        #dest.data[:] = self.surface.get_data()
-        #destR = pygame.surfarray.pixels_red(self.sdl_surface)
-        #destR.data[:] = self.surface.get_data()[1::4]
-        #destG = pygame.surfarray.pixels_green(self.sdl_surface)
-        #destG.data[:] = self.surface.get_data()[2::4]
-        #destB = pygame.surfarray.pixels_blue(self.sdl_surface)
-        #destB.data[:] = self.surface.get_data()[3::4]
-        #destA = pygame.surfarray.pixels_alpha(self.sdl_surface)
-        #destA.data[:] = self.surface.get_data()[::4]
-        
-    	
+
+

pycessing/images.py

 PROJECT_DIR = None
 CTX = None
 
-#TODO: write a generic transform stack to manage arbitrary sequences of 
+#TODO: write a generic transform stack to manage arbitrary sequences of
 #      transformations:
 #       ctx.save()
-#       
+#
 
 class Image2:
     """All-purpose class for cairo-based pixel images in pycessing"""
         self.rotation = 0
         self.scalex = 1.0
         self.scaley = 1.0
-        
-        
+
+
     def _loadSurface_(self):
         """load image from file or file_obj"""
 
         except AttributeError:
             imagelike = os.path.join(PROJECT_DIR, self.file_name)
         surface = pygame.image.load(imagelike).convert_alpha()
-        
+
         self.width = surface.get_width()
-        self.height = surface.get_height()    
+        self.height = surface.get_height()
         ar = pygame.surfarray.array2d(surface)
         stride = surface.get_width() * 4
         self.surface = cairo.ImageSurface.create_for_data(ar,
-                           cairo.FORMAT_ARGB32, surface.get_width(), 
+                           cairo.FORMAT_ARGB32, surface.get_width(),
                            surface.get_height(), stride)
         self.loaded = True
 
             if(width and height):
                 area = (sx, sy, width, height)
             else:
-                area = (sx, sy, self.surface.get_width(), 
+                area = (sx, sy, self.surface.get_width(),
 						self.surface.get_height())
         else:
             area = None
-            
+
+        scaley = self.scaley
+        scalex = self.scalex
+
+        #do flips if needed
+        if self.flipy:
+            scaley = scaley * -1
+            y = (1 - scaley) * (self.width / 2)
+        if self.flipx:
+            scalex = scalex * -1
+            x = (1 - scalex) * (self.height / 2)
+
         CTX.save()
 
+        CTX.scale(self.scalex, self.scaley)
         CTX.translate(x, y)
         CTX.rotate(self.rotation)
-        CTX.scale(self.scalex, self.scaley)
 
         CTX.set_source_surface(self.surface, 0, 0)
         CTX.paint()
         CTX.restore()
-        
+
     def rotate(self, angle):
         self.rotation = math.pi * angle / 180.0
-        
+
     def scale(self, pixelsx, pixelsy):
         if (pixelsx < 1) or (pixelsy < 1):
             print "image must be at least 1px wide and 1px high!"
             self._loadSurface_()
         self.scalex = float(pixelsx) / self.width
         self.scaley = float(pixelsy) / self.height
-        
+
+    def flip(self):
+        self.flipx = True
+        self.flipy = True
+
+    def flipx(self):
+        self.flipx = True
+
+    def flipy(self):
+        self.flipy = True
+
     def get_width(self):
         if(not(self.loaded)):
             self._loadSurface_()
             return self.scalex
         else:
             return self.width
-    
+
     def get_height(self):
         if(not(self.loaded)):
             self._loadSurface_()
 
         self.original_surface = self.surface.copy()
         self.loaded = True
-    
+
     def draw(self, x, y, sx=None, sy=None, width=None, height=None):
         if(not(self.loaded)):
             self._loadSurface_()
             if(width and height):
                 area = (sx, sy, width, height)
             else:
-                area = (sx, sy, self.surface.get_width(), 
+                area = (sx, sy, self.surface.get_width(),
 						self.surface.get_height())
         else:
             area = None
             self.surface.set_at((x, y), (r, g, b, a))
         else:
             self.surface.set_at((x, y), (r, g, b))
-        
+