def get(self): i_key = ndb.Key(urlsafe=self.request.get('img_id')) x = self.request.get('size') deg = self.request.get('deg') flip = str(self.request.get('flip')) lucky = str(self.request.get('lucky')) #flipV=self.request.get('flipV') #nothing=self.request.get('nothing') #self.response.out.write(str(x)+"<br>") greeting = i_key.get() if greeting.bomma: if x == '': x = '500' x = int(x) deg = int(deg) self.response.headers['Content-Type'] = 'image/png' greeting.bomma = images.resize(greeting.bomma, x, x) greeting.bomma = images.rotate(greeting.bomma, deg) if flip == "flipH": greeting.bomma = images.horizontal_flip(greeting.bomma) if flip == "flipV": greeting.bomma = images.vertical_flip(greeting.bomma) if lucky == "lucky": greeting.bomma = images.im_feeling_lucky(greeting.bomma) self.response.out.write(greeting.bomma) else: self.response.out.write('No image')
def post(self): user = users.get_current_user() uploaded = Uploaded.get_by_key_name(str(user)) response = "baz" if uploaded is None: response = "No image uploaded yet." else: image = uploaded.image if self.request.get('hflip'): newimage = images.horizontal_flip(image) elif self.request.get('vflip'): newimage = images.vertical_flip(image) elif self.request.get('rotater'): newimage = images.rotate(image, 90) elif self.request.get('rotatel'): newimage = images.rotate(image, 270) uploaded.image = newimage uploaded.put() self.redirect("/")
health['datastore'] = RUNNING entry = StatusText(key_name = "bazbookey") entry.content = "bazbooval" if entry.put(): health['datastore_write'] = RUNNING else: health['datastore_write'] = FAILED logging.error("Datastore write FAILED no exception given") except Exception, e: health['datastore'] = FAILED logging.error("Datastore FAILED %s"%(str(e))) if capability == "all" or capability == "images": try: image = urlfetch.fetch("http://localhost:8079/images/cloud.png").content images.horizontal_flip(image) images.vertical_flip(image) images.rotate(image, 90) images.rotate(image, 270) health['images'] = RUNNING except Exception, e: health['images'] = FAILED logging.error("images API FAILED %s"%(str(e))) if capability == "all" or capability == "memcache": try: if memcache.set("boo", "baz", 10): health['memcache'] = RUNNING else: health['memcache'] = FAILED logging.error("memcached API FAILED no exception")
try: entry = StatusText(key_name = "bazbookey") entry.content = "bazbooval" if entry.put(): health['datastore_write'] = RUNNING else: health['datastore_write'] = FAILED logging.error("Datastore write FAILED no exception given") except Exception, e: health['datastore_write'] = FAILED logging.error("Datastore write FAILED %s"%(str(e))) if capability == "all" or capability == "images": try: image = urlfetch.fetch("http://localhost/images/status_running.gif").content images.horizontal_flip(image) images.vertical_flip(image) images.rotate(image, 90) images.rotate(image, 270) health['images'] = RUNNING except Exception, e: health['images'] = FAILED logging.error("images API FAILED %s"%(str(e))) if capability == "all" or capability == "memcache": try: if memcache.set("boo", "baz", 10): health['memcache'] = RUNNING else: health['memcache'] = FAILED logging.error("memcached API FAILED no exception")
class ImageCache(db.Model): url = db.StringProperty() image = db.BlobProperty() # fetches images from the internet, manipulates then, and then caches in the datastore. # TODO - once you can programatically write to the blobstore, we should do that, because # GAE will let you serve binaries directly from the blobstore without having to activate # a handler. @classmethod def generate_image(cls, url): image_data = urlfetch.Fetch(url).content image = cls.weave(image_data) if not image: return None blob = db.Blob(image) cache = cls(url=url, image=blob) cache.save() return cache @classmethod def weave(cls, image_data): # where in the image to crop the box. numbers are relative to image # make sure max width + crop width can never be > 1 !! Likewise height. left = 0.0 + random.uniform(0, 0.4) top = 0.4 + random.uniform(0, 0.2) crop_width = 0.1 + random.uniform( 0, 0.3) # width of the box, as a proportion of the image width # target tile width and height. Image will be twice as wide/high target_width = 500 / 2 target_height = 320 / 2 # the box wants to be in the aspect ratio of the output file. try: image = images.Image(image_data) ratio = float(image.width) / image.height except images.NotImageError, e: logging.error("Can't parse image: %s" % e) return None # not a lot we can do here. logging.info("width is %s height is %s Ratio is %s" % (image.width, image.height, ratio)) crop_height = (crop_width * ratio) * (float(target_height) / target_width) # don't fall outside of the bounding box. Really shouldn't happen, and it # messes with the nice distribution of the boxes, but it's better than dying. if crop_height + top > 1: top = 1 - crop_height if crop_width + left > 1: left = 1 - crop_width image.crop(left, top, left + crop_width, top + crop_height) tile = image.execute_transforms(images.JPEG) tile_image = images.Image( tile) # need to do this to get width/height. Sigh. logging.info("image is %s by %s" % (tile_image.width, tile_image.height)) # issue here - the crop function might not produce an image of the # exact required aspect ratio, so when we resize to the desired tile size # here, we dont get an image of the exact right size. To avoid hairline # cracks, make sure we composite before we resize. w = tile_image.width - 1 h = tile_image.height - 1 one = images.composite([ (tile, 0, 0, 1.0, images.TOP_LEFT), (images.horizontal_flip(tile), w, 0, 1.0, images.TOP_LEFT), (images.vertical_flip(tile), 0, h, 1.0, images.TOP_LEFT), (images.horizontal_flip( images.vertical_flip(tile)), w, h, 1.0, images.TOP_LEFT), ], w * 2, h * 2, output_encoding=images.JPEG) return images.resize(one, target_width * 2, target_height * 2, output_encoding=images.JPEG)