def update_photo(it, request_handler): try: raw_file = request_handler.request.get('new-photo') rot = request_handler.request.get("rotation") if len(raw_file) > 0: # a new image saved img = DBImage() # - no: create it if rot and (rot <> u'0'): # is a rotation requested? angle = int(rot) * 90 raw_file = images.rotate(raw_file, angle) # exif = raw_file.get_original_metadata() img.picture = db.Blob(raw_file) img.make_thumb() img.owner = request_handler.user_id img.put() logging.debug('update_photo Ins:',img.key.id()) if it.photo: # the item has an image already? logging.debug( 'update_photo Del:',it.photo.id()) it.photo.delete() else: # no new image - rotate an existing image? img = None # no image supplied if rot and (rot != u'0'): # is a rotation requested? old_img = it.photo if old_img and old_img.picture: # if so, does the item have a pic already? angle = int(rot) * 90 # rotate & save in place rotated_pic = images.rotate(old_img.picture, angle) old_img.picture = db.Blob(rotated_pic) old_img.thumb = None old_img.put() except Exception: logging.exception("newOrUpdateItem Image Resize: ", exc_info=True) img = None return img
def mark_img_init(self): if self.black_img == (): logging.info('Loading Black Image...') f = open("img/black.png", "rb") img = f.read() f.close() self.black_img = (img, images.rotate(img, 180), images.resize(img, 16, 16) ) if self.white_img == (): logging.info('Loading White Image...') f = open("img/white.png", "rb") img = f.read() f.close() self.white_img = (img, images.rotate(img, 180), images.resize(img, 16, 16) )
def FAQCategoryUpdate(self, faqcat): """ update faqcat""" if self.is_current_user_admin(): print(faqcat.id) if faqcat.id != None: item = ndb.Key(urlsafe=faqcat.id).get() if item is None: raise endpoints.BadRequestException("FAQCategory not found") else: if faqcat.image != None: image = faqcat.image if len(image) > 6*1024*1024: raise endpoints.BadRequestException("Max. image size is 6*1024*1024 bytes") write_retry_params = gcs.RetryParams(backoff_factor=1.1) filename = "/" + BUCKET_NAME + "/" +str(uuid.uuid4()) png = images.rotate(image, 0, output_encoding=images.PNG) gcs_file = gcs.open(filename,'w',retry_params=write_retry_params,content_type='image/png',) gcs_file.write(png) gcs_file.close() blobkey = blobstore.create_gs_key("/gs" + filename) url = images.get_serving_url(blobkey) item.image_url = url item.blobkey = filename del faqcat.image item.put() return item else: raise endpoints.BadRequestException("ID missing") else: raise endpoints.UnauthorizedException("Only Volunteers users can update FAQCategory. \ Contact [email protected] for more information.")
def OfferInsert(self, offer): """ Created create offer""" user = self.get_current_user() offer.owner_key = user.key urls = [] blobkeys = [] for image in offer.images: if len(image) > 6*1024*1024: for blobkey in blobkeys: gcs.delete(blobkey) raise endpoints.BadRequestException("Max. image size is 6*1024*1024 bytes") write_retry_params = gcs.RetryParams(backoff_factor=1.1) filename = "/" + BUCKET_NAME + "/" +str(uuid.uuid4()) png = images.rotate(image, 0, output_encoding=images.PNG) gcs_file = gcs.open(filename,'w',retry_params=write_retry_params,content_type='image/png',) gcs_file.write(image) gcs_file.close() blobkey = blobstore.create_gs_key("/gs" + filename) blobkeys.append(filename) #url = images.get_serving_url("gs" + filename) url = images.get_serving_url(blobkey) urls.append(url) offer.image_urls = urls offer.blobkeys = blobkeys del offer.images offer.put() return offer
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 get(self): project_id = self.request.get('project_id') metadata = self.request.get('metadata') resize = self.request.get('resize') rotate = self.request.get('rotate') transform = self.request.get('transform') query = db.GqlQuery("SELECT * FROM ProjectLogo WHERE " "project_id = '%s'" % project_id) logo = query[0] if metadata is not None and metadata == 'true': if transform is not None and transform == 'true': logo_image = images.Image(logo.picture) if resize is not None and len(resize) > 0: logo_image.resize(width=int(resize)) if rotate is not None and rotate == 'true': logo_image.rotate(90) logo_image.execute_transforms() else: self.response.headers['Content-Type'] = 'application/json' if resize is not None and len(resize) > 0: logo.picture = images.resize(logo.picture, width=int(resize)) if rotate is not None and rotate == 'true': logo.picture = images.rotate(logo.picture, 90) logo_image = images.Image(logo.picture) image_data = { 'height': logo_image.height, 'width': logo_image.width, 'format': logo_image.format } self.response.out.write(json.dumps(image_data)) else: self.response.headers['Content-Type'] = 'image/png' self.response.out.write(str(logo.picture))
def get(self): project_id = self.request.get('project_id') metadata = self.request.get('metadata') resize = self.request.get('resize') rotate = self.request.get('rotate') transform = self.request.get('transform') query = db.GqlQuery("SELECT * FROM ProjectLogo WHERE " "project_id = '%s'" % project_id) logo = query[0] if metadata is not None and metadata == 'true': if transform is not None and transform == 'true': logo_image = images.Image(logo.picture) if resize is not None and len(resize) > 0: logo_image.resize(width=int(resize)) if rotate is not None and rotate == 'true': logo_image.rotate(90) logo_image.execute_transforms() else: self.response.headers['Content-Type'] = 'application/json' if resize is not None and len(resize) > 0: logo.picture = images.resize(logo.picture, width=int(resize)) if rotate is not None and rotate == 'true': logo.picture = images.rotate(logo.picture, 90) logo_image = images.Image(logo.picture) image_data = { 'height' : logo_image.height, 'width' : logo_image.width, 'format' : logo_image.format } self.response.out.write(json.dumps(image_data)) else: self.response.headers['Content-Type'] = 'image/png' self.response.out.write(logo.picture)
def number_img_init(self, num): if self.number_img.get(str(num)) == None: logging.info('Loading ' + str(num) + '.png ...') f = open("img/" + str(num) + ".png", "rb") num_img = f.read() self.number_img[str(num)] = (num_img, images.rotate(num_img, 180)) f.close()
def UserInsert(self, user): """ Creates create user""" current_user = endpoints.get_current_user() user_id = self.get_user_id(current_user) users = User.query(User.user_id == user_id) if users.count() == 0: user.user_id = user_id if user.image != None: image = user.image if len(image) > 6*1024*1024: raise endpoints.BadRequestException("Max. image size is 6*1024*1024 bytes") write_retry_params = gcs.RetryParams(backoff_factor=1.1) filename = "/" + BUCKET_NAME + "/" +str(uuid.uuid4()) png = images.rotate(image, 0, output_encoding=images.PNG) gcs_file = gcs.open(filename,'w',retry_params=write_retry_params,content_type='image/png',) gcs_file.write(png) gcs_file.close() blobkey = blobstore.create_gs_key("/gs" + filename) url = images.get_serving_url(blobkey) user.image_url = url user.blobkey = filename del user.image """TODO: write better code; temp fix to get users name""" headers = {'Authorization': os.getenv('HTTP_AUTHORIZATION')} url = "https://www.googleapis.com/plus/v1/people/me" result = urlfetch.fetch(url=url, method=urlfetch.GET, headers=headers) profile = json.loads(result.content) print(profile) user.name = profile['displayName'] profUrl = profile['url'] if user.im != None: try: im_json = json.loads(user.im) except: raise endpoints.BadRequestException("im needs to be empty or valid json") else: im_json = {} im_json["gplus"] = {"url":profUrl,"display":user.name} user.im = json.dumps(im_json) current_user = endpoints.get_current_user() email = current_user.email() if self.is_current_user_admin(): user.is_volunteer = True user.is_admin = True else: user.is_volunteer = False user.is_admin = False user.put() print(user) return user else: return users.get()
def UserUpdate(self, user): """ Updates informations about the user """ current_user = endpoints.get_current_user() user_id = self.get_user_id(current_user) users = User.query(User.user_id == user_id) if users.count() != 1: raise endpoints.BadRequestException("There no user with user id %s." % user_id) else: update_user = users.get() if user.image != None: image = user.image if len(image) > 6*1024*1024: raise endpoints.BadRequestException("Max. image size is 6*1024*1024 bytes") write_retry_params = gcs.RetryParams(backoff_factor=1.1) filename = "/" + BUCKET_NAME + "/" +str(uuid.uuid4()) png = images.rotate(image, 0, output_encoding=images.PNG) gcs_file = gcs.open(filename,'w',retry_params=write_retry_params,content_type='image/png',) gcs_file.write(png) gcs_file.close() blobkey = blobstore.create_gs_key("/gs" + filename) url = images.get_serving_url(blobkey) update_user.image_url = url update_user.blobkey = filename del user.image update_user.address = user.address if user.im != None: try: im_json = json.loads(user.im) except: raise endpoints.BadRequestException("im needs to be empty or valid json") else: im_json = {} if "gplus" in json.loads(update_user.im): im_json["gplus"] = json.loads(update_user.im)["gplus"] else: """TODO: write better code; temp fix to get users name""" headers = {'Authorization': os.getenv('HTTP_AUTHORIZATION')} url = "https://www.googleapis.com/plus/v1/people/me" result = urlfetch.fetch(url=url, method=urlfetch.GET, headers=headers) profile = json.loads(result.content) user.name = profile['displayName'] profUrl = profile['url'] im_json["gplus"] = {"url":profUrl,"display":user.name} update_user.im = json.dumps(im_json) if user.interest != None: update_user.interest = user.interest update_user.put() return update_user
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("/")
def post(self): #check login if not users.get_current_user(): self.redirect(users.create_login_url('/')) user = users.get_current_user() query = db.GqlQuery("SELECT * FROM Image WHERE user = :1", user) saved_image = query.get() action = self.request.get("action") if saved_image and action: if action == "resize": width = self.request.get("width") height = self.request.get("height") if width and height: img = images.resize(saved_image.data, int(width), int(height), saved_image.output_encoding) elif action == "rotate": degrees = self.request.get("degrees") if degrees: img = images.rotate(saved_image.data, int(degrees), saved_image.output_encoding) elif action == "im_feeling_lucky": img = images.im_feeling_lucky(saved_image.data, saved_image.output_encoding) elif action == "crop": left_x = self.request.get("left_x") top_y = self.request.get("top_y") right_x = self.request.get("right_x") bottom_y = self.request.get("bottom_y") if left_x and top_y and right_x and bottom_y: img = images.crop(saved_image.data, float(left_x), float(top_y), float(right_x), float(bottom_y), saved_image.output_encoding) if img: # save new settings output_encoding = saved_image.output_encoding content_type, width, height = getImageInfo(img) # delete previous entity for image in query: db.delete(image) image = Image() image.user = user image.data = db.Blob(img) image.output_encoding = output_encoding image.content_type = content_type image.width = width image.height = height image.put()
def MentoringRequestInsert(self, mentoringrequest): """ Create mentoringrequest""" user = self.get_current_user() mentoringrequest.requester_key = user.key if mentoringrequest.image != None: image = mentoringrequest.image if len(image) > 6*1024*1024: raise endpoints.BadRequestException("Max. image size is 6*1024*1024 bytes") write_retry_params = gcs.RetryParams(backoff_factor=1.1) filename = "/" + BUCKET_NAME + "/" +str(uuid.uuid4()) png = images.rotate(image, 0, output_encoding=images.PNG) gcs_file = gcs.open(filename,'w',retry_params=write_retry_params,content_type='image/png',) gcs_file.write(png) gcs_file.close() blobkey = blobstore.create_gs_key("/gs" + filename) url = images.get_serving_url(blobkey) mentoringrequest.image_url = url print(url) mentoringrequest.blobkey = filename print(blobkey) del mentoringrequest.image mentoringrequest.put() return mentoringrequest
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") except Exception, e: health['memcache'] = FAILED
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") except Exception, e: health['memcache'] = FAILED
def piece_international_img_init(self): if self.piece_international_img != {}: return logging.info('Loading International Piece Image...') f = open("img/fu_international.png", "rb") fu_img = f.read() f.close() f = open("img/ky_international.png", "rb") ky_img = f.read() f.close() f = open("img/ke_international.png", "rb") ke_img = f.read() f.close() f = open("img/gi_international.png", "rb") gi_img = f.read() f.close() f = open("img/ki_international.png", "rb") ki_img = f.read() f.close() f = open("img/hi_international.png", "rb") hi_img = f.read() f.close() f = open("img/ka_international.png", "rb") ka_img = f.read() f.close() f = open("img/ou_international.png", "rb") ou_img = f.read() f.close() f = open("img/to_international.png", "rb") to_img = f.read() f.close() f = open("img/ny_international.png", "rb") ny_img = f.read() f.close() f = open("img/nk_international.png", "rb") nk_img = f.read() f.close() f = open("img/ng_international.png", "rb") ng_img = f.read() f.close() f = open("img/ry_international.png", "rb") ry_img = f.read() f.close() f = open("img/um_international.png", "rb") um_img = f.read() f.close() self.piece_international_img['p'] = (fu_img, images.rotate(fu_img, 180)) self.piece_international_img['l'] = (ky_img, images.rotate(ky_img, 180)) self.piece_international_img['n'] = (ke_img, images.rotate(ke_img, 180)) self.piece_international_img['s'] = (gi_img, images.rotate(gi_img, 180)) self.piece_international_img['g'] = (ki_img, images.rotate(ki_img, 180)) self.piece_international_img['r'] = (hi_img, images.rotate(hi_img, 180)) self.piece_international_img['b'] = (ka_img, images.rotate(ka_img, 180)) self.piece_international_img['k'] = (ou_img, images.rotate(ou_img, 180)) self.piece_international_img['+p'] = (to_img, images.rotate(to_img, 180)) self.piece_international_img['+l'] = (ny_img, images.rotate(ny_img, 180)) self.piece_international_img['+n'] = (nk_img, images.rotate(nk_img, 180)) self.piece_international_img['+s'] = (ng_img, images.rotate(ng_img, 180)) self.piece_international_img['+r'] = (ry_img, images.rotate(ry_img, 180)) self.piece_international_img['+b'] = (um_img, images.rotate(um_img, 180)) self.piece_international_img['+g'] = (ki_img, images.rotate(ki_img, 180))
def piece_img_init(self): ### 画像の初期化が終わっていたらいちいち再読み込みを行わない ### Google App Engineでは前の呼び出し時のインスタンスを ### 覚えていることがまれによくある if self.piece_img != {}: return logging.info('Loading Kanji Piece Image...') f = open("img/fu.png", "rb") fu_img = f.read() f.close() f = open("img/ky.png", "rb") ky_img = f.read() f.close() f = open("img/ke.png", "rb") ke_img = f.read() f.close() f = open("img/gi.png", "rb") gi_img = f.read() f.close() f = open("img/ki.png", "rb") ki_img = f.read() f.close() f = open("img/hi.png", "rb") hi_img = f.read() f.close() f = open("img/ka.png", "rb") ka_img = f.read() f.close() f = open("img/ou.png", "rb") ou_img = f.read() f.close() f = open("img/to.png", "rb") to_img = f.read() f.close() f = open("img/ny.png", "rb") ny_img = f.read() f.close() f = open("img/nk.png", "rb") nk_img = f.read() f.close() f = open("img/ng.png", "rb") ng_img = f.read() f.close() f = open("img/ry.png", "rb") ry_img = f.read() f.close() f = open("img/um.png", "rb") um_img = f.read() f.close() self.piece_img['p'] = (fu_img, images.rotate(fu_img, 180)) self.piece_img['l'] = (ky_img, images.rotate(ky_img, 180)) self.piece_img['n'] = (ke_img, images.rotate(ke_img, 180)) self.piece_img['s'] = (gi_img, images.rotate(gi_img, 180)) self.piece_img['g'] = (ki_img, images.rotate(ki_img, 180)) self.piece_img['r'] = (hi_img, images.rotate(hi_img, 180)) self.piece_img['b'] = (ka_img, images.rotate(ka_img, 180)) self.piece_img['k'] = (ou_img, images.rotate(ou_img, 180)) self.piece_img['+p'] = (to_img, images.rotate(to_img, 180)) self.piece_img['+l'] = (ny_img, images.rotate(ny_img, 180)) self.piece_img['+n'] = (nk_img, images.rotate(nk_img, 180)) self.piece_img['+s'] = (ng_img, images.rotate(ng_img, 180)) self.piece_img['+r'] = (ry_img, images.rotate(ry_img, 180)) self.piece_img['+b'] = (um_img, images.rotate(um_img, 180)) self.piece_img['+g'] = (ki_img, images.rotate(ki_img, 180))