def meme_rand(): """ Generate a random meme """ img = random.choice(imgs) quote = random.choice(quotes) path = generate_meme(img, quote.body, quote.author) return render_template('meme.html', path=path)
def meme_post(): """ Create a user defined meme """ img_url = request.form['image_url'] r = requests.get(img_url) with open("./temp.jpg", 'wb') as f: f.write(r.content) img = "./temp.jpg" body = request.form['body'] author = request.form['author'] path = generate_meme(img, body, author) os.remove("./temp.jpg") return render_template('meme.html', path=path)
def meme_post(): """ Create a user defined meme """ # @TODO: # 1. Use requests to save the image from the image_url # form param to a temp local file. # 2. Use the meme object to generate a meme using this temp # file and the body and author form paramaters. # 3. Remove the temporary saved image. image_extension = None image_url = request.form.get("image_url", None) body = request.form.get("body", None) author = request.form.get("author", None) path = None download_path_name = None try: req = requests.get(url=image_url) if req.status_code == 200: content_type = req.headers.get("Content-Type") if content_type in ["image/png", "image/jpeg", "image/jpg"]: image_extension = content_type.split('/')[-1] file_name = str(uuid.uuid4()) download_path_name = os.path.join("static/", f"{file_name}.{image_extension}") img_name = f"{file_name}.{image_extension}" with open(download_path_name, "wb") as f: f.write(req.content) f.close() except Exception as e: print("Error uploading", e) if download_path_name is not None: path = Path(generate_meme(download_path_name, body=body, author=author)) os.remove(download_path_name) return render_template('meme.html', path=path)
"""Runs generate_meme function from meme.py with supplied command-line arguments. Arguments: --path, --body, --author. If none given will default to './data/photos/dog/' directory for images and './data/DogQuotes/' directory for quote body and author (picked at random from imported documents) """ import argparse from meme import generate_meme if __name__ == "__main__": args = None parser = argparse.ArgumentParser("create random quote captions on images") parser.add_argument( '--path', type=str, default=None, help='path to folder containing .jpg images, default=None') parser.add_argument('--body', type=str, default=None, help="quote text, default=None") parser.add_argument('--author', type=str, default=None, help="author, default=None") args = parser.parse_args() print(generate_meme(args.path, args.body, args.author))