def _random_image(field): log.debug("Generating identicon image") from identicon import identicon name = "dilla_%s.png" % random_slug(field) icon = identicon.render_identicon( \ random.randint(5 ** 5, 10 ** 10), \ random.randint(64, 250)) icon.save(os.path.join(destination, name), 'PNG') return os.path.join(field.upload_to, name)
def _random_image(field): log.debug("Generating identicon image") from identicon import identicon name = "dilla_%s.png" % random_slug(record, field) icon = identicon.render_identicon( \ random.randint(5 ** 5, 10 ** 10), \ random.randint(64, 250)) # using storage tmp_file = StringIO() icon.save(tmp_file, 'PNG') name = field.generate_filename(record.obj, name) saved_name = field.storage.save(name, ContentFile(tmp_file.getvalue())) return saved_name
def on_get(self, req, resp, email_hash): resp.content_type = "image/png" #cut possible extensions, we do not support them email_hash = email_hash[:32] size = 80 if req.get_param("s"): size = int(req.get_param("s")) if req.get_param("size"): size = int(req.get_param("size")) # look for photo in cache found, photo = self.get_photo_from_cache(email_hash, size) if found: #logging.debug("hash %s with size %d found in cache", email_hash, size) pass else: # photo not found in cache, look in db found, photo = self.get_photo_from_db(email_hash) if found: logging.debug("hash %s found in db", email_hash) photo = self.resize_and_square(photo, size) self.put_photo_in_cache(email_hash, size, photo) else: # photo not found in db, render identicon logging.debug("hash %s NOT found, rendering identicon", email_hash) code = abs(hash(email_hash)) % (10 ** 8) icon = identicon.render_identicon(code, size) icon.thumbnail((size,size)) b = io.BytesIO() icon.save(b, 'PNG') photo = b.getvalue() self.put_photo_in_cache(email_hash, size, photo) resp.status = falcon.HTTP_200 resp.body = photo