def generate_card(string: str, img_path: str, filename: str, font_size: int, x: int, y: int, color, char_limit, font="opensans"): try: if len( string ) >= char_limit: # 23 or bigger string would cut the text out, for now just avoid it. raise TooManyCharsException("Diminue esse textão aí, pfv.") else: position = (x, y) img = Image.open(img_path) drawer = ImageDraw.Draw(img) drawer.text(position, string, font=ImageFont.truetype(font='templates/fonts/' + font + '.ttf', size=font_size), fill=color) img_url = save_image_to_imgur(image_to_byte_array(img)) image_is_valid, file_bytes = validate_image(img_url) discord_file = create_discord_file_object(file_bytes, ".png") return discord_file except Exception as e: puts.info(e) raise FutebotException(e)
def get_image(ctx, search_query, spoiler, **kwargs): file_type = ".gif" if kwargs.get("gif", None) else ".jpg" url = generate_image_search_url(search_query, file_type) res = requests.get(url) search_result = res.json() if "items" not in search_result: raise Exception("We couldn't find any images for your search") image_is_valid = False for item in search_result["items"]: image_link = item["link"] image_is_valid, file_bytes = validate_image(image_link) if image_is_valid: app = ClarifaiApp() model = app.models.get("moderation") response = model.predict_by_url(url=image_link) if response['outputs'][0]['data']['concepts'][0]['name'] == 'explicit' \ or response['outputs'][0]['data']['concepts'][0]['name'] == 'suggestive'\ or response['outputs'][0]['data']['concepts'][0]['name'] == 'gore'\ or response['outputs'][0]['data']['concepts'][0]['name'] == 'drug'\ or (response['outputs'][0]['data']['concepts'][0]['name'] == 'safe' and response['outputs'][0]['data']['concepts'][0]['value'] < 0.95): spoiler = "--spoiler" f = create_discord_file_object(file_bytes, file_type, spoiler) return f break
def generate_card_img_title_description(string: str, img_path: str, filename: str, font_size: int, x: int, y: int, color, char_limit, img_url, img_x, img_y, img_width, img_height, description: str, desc_x: int, desc_y: int, desc_font_size: int, font="opensans"): try: if len( string ) >= char_limit: # 23 or bigger string would cut the text out, for now just avoid it. raise TooManyCharsException("Diminue esse textão aí, pfv.") else: position = (x, y) img = Image.open(img_path) drawer = ImageDraw.Draw(img) drawer.text(position, string, font=ImageFont.truetype(font='templates/fonts/' + font + '.ttf', size=font_size), fill=color) drawer.text((desc_x, desc_y), description, font=ImageFont.truetype(font='templates/fonts/' + font + '.ttf', size=desc_font_size), fill=color) response = requests.get(img_url, stream=True) img2 = Image.open(BytesIO(response.content)) img2.thumbnail((img_width, img_height), Image.ANTIALIAS) img.paste(img2, (img_x, img_y)) img_url = save_image_to_imgur(image_to_byte_array(img)) image_is_valid, file_bytes = validate_image(img_url) discord_file = create_discord_file_object(file_bytes, ".png") return discord_file except Exception as e: puts.info(e) return None
def generate_card_twit(name: str, display_name: str, img_path: str, filename: str, img_url, description: str): try: if len(description) >= 50: raise TooManyCharsException("Diminue esse textão aí, pfv.") else: img = Image.open(img_path) drawer = ImageDraw.Draw(img) drawer.text( (95, 30), name, font=ImageFont.truetype(font='templates/fonts/helvetica.ttf', size=20), fill=(0, 0, 0)) drawer.text( (90, 55), display_name, font=ImageFont.truetype(font='templates/fonts/helvetica.ttf', size=18), fill=(150, 150, 150)) drawer.text( (20, 100), description, font=ImageFont.truetype(font='templates/fonts/helvetica.ttf', size=35), fill=(0, 0, 0)) response = requests.get(img_url, stream=True) img2 = Image.open(BytesIO(response.content)) img2 = mask_circle_transparent(img2, 0) img2.thumbnail((60, 60), Image.ANTIALIAS) img.paste(img2, (20, 20), img2) img_url = save_image_to_imgur(image_to_byte_array(img)) image_is_valid, file_bytes = validate_image(img_url) discord_file = create_discord_file_object(file_bytes, ".png") return discord_file except Exception as e: puts.info(e) return None
async def gifme(ctx, search_query, spoiler=None): try: url = generate_image_search_url(search_query, gif=True) res = requests.get(url) search_result = res.json() if "items" not in search_result: raise Exception("We couldn't find any images for your search") image_is_valid = False for item in search_result["items"]: image_link = item["link"] image_is_valid, file_bytes = validate_image(image_link) if image_is_valid: f = create_discord_file_object(file_bytes, ".gif", spoiler) await ctx.send(file=f) break except Exception as e: puts.info(e) await ctx.send(e)
def generate_card_multiple_texts(img_path: str, filename: str, *texts: tuple): try: img = Image.open(img_path) drawer = ImageDraw.Draw(img) for text in texts: string = text[0] font_size = text[1] x = text[2] y = text[3] color = text[4] char_limit = text[5] font = text[6] if len( string ) >= char_limit: # 23 or bigger string would cut the text out, for now just avoid it. raise TooManyCharsException("Diminue esse textão aí, pfv.") else: position = (x, y) drawer.text(position, string, font=ImageFont.truetype(font='templates/fonts/' + font + '.ttf', size=font_size), fill=color) img_url = save_image_to_imgur(image_to_byte_array(img)) image_is_valid, file_bytes = validate_image(img_url) discord_file = create_discord_file_object(file_bytes, ".png") return discord_file except Exception as e: puts.info(e) raise FutebotException(e)
def parse_to_discord_file(img): img_url = save_image_to_imgur(image_to_byte_array(img)) image_is_valid, file_bytes = validate_image(img_url) discord_file = create_discord_file_object(file_bytes, ".png") return discord_file