async def meme_generator(ctx, *args): """Command to generate memes with top and bottom text Args: - ctx: context that the command occured use this to access the message and other attributes - *args: arguments passed in with the command """ channel = ctx.channel url = overlay.get_image_url_args(ctx.message, args, 3, 2) if url == 0: # invalid image await channel.send(embed=discord.Embed(description="Invalid image", color=discord.Color.red())) return else: output = overlay.paste_text_top_bottom(args[0], args[1], overlay.url_to_image(url)) output.save('meme.png') try: message = await channel.send(file=discord.File('meme.png')) except: message = await channel.send(embed=discord.Embed( description="Image too large", color=discord.Color.red())) return custom_meme.track_command(ctx.author.id, message) os.remove('meme.png')
def test_overlay03(): """Test draw image command""" fname = base + '03.png' tname = os.path.join(tdir, fname) rname = os.path.join(refd, fname) image_link = 'https://s.hdnux.com/photos/51/23/24/10827008/3/920x920.jpg' path = Path('memes/lan/landrew.png') origin = overlay.landrew_origin output = overlay.overlay_image(overlay.url_to_image(image_link), path, origin) output.save(tname) tsize = os.path.getsize(tname) print(glob.glob(tname), '[', tsize, 'bytes', ']') rsize = os.path.getsize(rname) print(glob.glob(rname), '[', rsize, 'bytes', ']') result = compare_images(rname, tname, tol=TOLERANCE) if result is not None: print('result=', result) assert result is None
async def saturate(ctx, *args): """Command to saturate a given image by a given factor Args: - ctx: context that the command occured use this to access the message and other attributes - *args: arguments passed into the command (in this case the pixelation factor) """ channel = ctx.channel url = overlay.get_image_url_args(ctx, args, 2, 1) try: factor = float(args[0]) except: await channel.send(embed=discord.Embed( description="Invalid Parameters", color=discord.Color.red())) return if url == 0: await channel.send(embedd=discord.Embed(description="Invalid Image", color=discord.Color.red())) return output = filters.saturate_image(overlay.url_to_image(url), factor) output.save('saturate.png') try: message = await channel.send(file=discord.File('saturate.png')) except: message = await channel.send(embed=discord.Embed( description="Image too large", color=discord.Color.red())) custom_meme.track_command(ctx.author.id, message) os.remove('saturate.png')
def test_overlay05(): """Test meme command with multi-line input""" fname = base + '05.png' tname = os.path.join(tdir, fname) rname = os.path.join(refd, fname) image = overlay.url_to_image( 'https://cdn.discordapp.com/attachments/501594682820788224/701314537420488714/barrify.png' ) ttext = 'You know...' btext = 'We could create a student who finishes the homework faster' output = overlay.paste_text_top_bottom(ttext, btext, image) output.save(tname) tsize = os.path.getsize(tname) print(glob.glob(tname), '[', tsize, 'bytes', ']') rsize = os.path.getsize(rname) print(glob.glob(rname), '[', rsize, 'bytes', ']') result = compare_images(rname, tname, tol=TOLERANCE) if result is not None: print('result=', result) assert result is None
def test_overlay04(): """Test meme command with simple input""" fname = base + '04.png' tname = os.path.join(tdir, fname) rname = os.path.join(refd, fname) image = overlay.url_to_image( 'https://pngimg.com/uploads/farmer/farmer_PNG59.png') ttext = 'HA' btext = 'Farmer' output = overlay.paste_text_top_bottom(ttext, btext, image) output.save(tname) tsize = os.path.getsize(tname) print(glob.glob(tname), '[', tsize, 'bytes', ']') rsize = os.path.getsize(rname) print(glob.glob(rname), '[', rsize, 'bytes', ']') result = compare_images(rname, tname, tol=TOLERANCE) if result is not None: print('result=', result) assert result is None
def paste_on_face(face_path, image_url, face_scale): # open image in opencv format and find the coordinates and dimensions of faces image_for_coordinates = open_image_cv(image_url) faces = face_coordinates(image_for_coordinates) #open the image and face to paste with the Image library image = url_to_image(image_url) face = Image.open(face_path) #check if there were no faces found in the inputed image if len(faces) == 0: return 0 for (x, y, w, h) in faces: # make a copy of the face to resize selected_face = face.copy() #set face width and height face_width = int(w*face_scale[0]) face_height = int(h*face_scale[0]) # resizes to the size of the face in the image selected_face = selected_face.resize([face_width, face_height], Image.ANTIALIAS) # set x and y position with adjustments for centering x_pos = x-int(face_scale[1]*h) y_pos = y-int(face_scale[2]*h) # paste face onto the inputed image at the specified coordinates image.paste(selected_face, (x_pos, y_pos), selected_face) return image
def test_customHighlight(): fname = base + '_customHighlight.png' tname = os.path.join(tdir, fname) rname = os.path.join(refd, fname) image = overlay.url_to_image( 'https://cdn.discordapp.com/attachments/501594682820788224/701320190738038837/zoomarun_final.png' ) red = 30 green = 0 blue = 75 output = filters.custom_edge_highlight_image(image, red, green, blue) output.save(tname) tsize = os.path.getsize(tname) print(glob.glob(tname), '[', tsize, 'bytes', ']') rsize = os.path.getsize(rname) print(glob.glob(rname), '[', rsize, 'bytes', ']') result = compare_images(rname, tname, tol=TOLERANCE) if result is not None: print('result=', result) assert result is None
async def intensify(ctx, *args): """Command to intensify inputed image by the inputed factor Args: - ctx: context that the command occured use this to access the message and other attributes - *args: arguments passed in with the command """ channel = ctx.channel try: factor = float(args[0]) url = overlay.get_image_url_args(ctx, args, 2, 1) except: factor = 2 # default if no factor specified url = overlay.get_image_url_args(ctx, args, 1, 0) if url == 0: # invalid image await channel.send(embed=discord.Embed(description="Invalid image", color=discord.Color.red())) return output = filters.intensify_image(overlay.url_to_image(url), factor) if output == 0: # if factor < 0 await channel.send(embed=discord.Embed(description="Invalid factor", color=discord.Color.red())) return # save and send image output.save('intensify.png') try: message = await channel.send(file=discord.File('intensify.png')) except: message = await channel.send(embed=discord.Embed( description="Image too large", color=discord.Color.red())) custom_meme.track_command(ctx.author.id, message) os.remove('intensify.png')
async def draw_universal(ctx, path, command_end_index, origin, name): """Universal function which is called by draw command with the following arguments Args: - ctx: context that the command occured use this to access the message and other attributes - path: path to the drawing image (ie memes/lan/landrew.png) - command_end_index: index the end of the command (ie. for bdraw its 7 for '$' 'b' 'd' 'r' 'a' 'w' ' ') - origin: pixel origin imported from overlay.py - name: output file name """ channel = ctx.channel async with channel.typing(): url = over.get_image_url(ctx.message, command_end_index) if url == 0: # no url, hand should write the inputed text if len(ctx.message.content[command_end_index:].strip()) == 0: # no input given, try to use previous bot output if bot_last_command[ctx.author.id] is not None: message = bot_last_command[ctx.author.id] url = over.get_image_url(message, 0) output = over.overlay_image(over.url_to_image(url), Path(path), origin) else: await channel.send(embed=discord.Embed( description= "No previous bot command to pull from. Please specify text or an image.", color=discord.Color.red())) return else: output = over.draw_text( ctx.message.content[command_end_index:], Path(path), origin) else: # url inputed, hand should draw on the image output = over.overlay_image(over.url_to_image(url), Path(path), origin) output.save(name + '.png') try: message = await channel.send(file=discord.File(name + '.png')) except: message = await channel.send(embed=discord.Embed( description="Image too large", color=discord.Color.red())) track_command(ctx.author.id, message) # tracks the most recent command of a user os.remove(name + '.png')
async def mirror(ctx, *args): """Command to mirror given image on the inputted axis Args: - ctx: context that the command occured use this to access the message and other attributes - *args: arguments passed in to the command (in this case either the X axis or Y axis) """ channel = ctx.channel try: url = overlay.get_image_url_args(ctx.message, args, 2, 1) axis = args[0] except: await channel.send(embed=discord.Embed(description="Invalid input", color=discord.Color.red())) return if axis != "x" and axis != "y" and axis != "X" and axis != "Y": await channel.send( embed=discord.Embed(description="Invalid axis, please use x or y", color=discord.Color.red())) return if axis == "x" or axis == "X": output = filters.mirror_x(overlay.url_to_image(url)) output.save("mirror_x.png") try: message = await channel.send(file=discord.File("mirror_x.png")) except: message = await channel.send(embed=discord.Embed( description="Image too large", color=discord.Color.red())) return custom_meme.track_command(ctx.author.id, message) os.remove("mirror_x.png") return if axis == "y" or axis == "Y": output = filters.mirror_y(overlay.url_to_image(url)) output.save("mirror_y.png") try: message = await channel.send(file=discord.File("mirror_y.png")) except: message = await channel.send(embed=discord.Embed( description="Image too large", color=discord.Color.red())) return custom_meme.track_command(ctx.author.id, message) os.remove("mirror_y.png")
async def draw_universal(ctx, path, command_end_index, origin, name): """Universal function which is called by draw command with the following arguments Args: - ctx: context that the command occured use this to access the message and other attributes - path: path to the drawing image (ie memes/lan/landrew.png) - command_end_index: index the end of the command (ie. for bdraw its 7 for '$' 'b' 'd' 'r' 'a' 'w' ' ') - origin: pixel origin imported from overlay.py - name: output file name """ channel = ctx.channel #in case of gif url = over.get_gif_url(ctx, command_end_index) if url != 0: #get list of frames imgList = over.gif_url_to_image_list(url, 1) if imgList == 0: #if invalid list return await channel.send(embed=discord.Embed(description="invalid image", color=discord.Color.red())) return #get list of image clips gifClip = make_draw_gif(imgList, 0) gifClip.write_gif(name + '.gif', 24, program='imageio') try: #check if message is <8 mb message = await channel.send(file=discord.File(name + '.gif')) except: #random color cause why not randRGB = lambda: random.randint(0, 255) randColor = int('%02X%02X%02X' % (randRGB(), randRGB(), randRGB()), 16) os.remove(name + '.gif') await channel.send(embed=discord.Embed( description="GIF + image becomes too large to send, sorry :(", color=randColor)) return track_command(ctx.author.id, message) os.remove(name + '.gif') return url = over.get_image_url(ctx, command_end_index) if url == 0: # no url, hand should write the inputed text output = over.draw_text(ctx.message.content[command_end_index:], Path(path), origin) else: # url inputed, hand should draw on the image output = over.overlay_image(over.url_to_image(url), Path(path), origin) output.save(name + '.png') try: message = await channel.send(file=discord.File(name + '.png')) except: message = await channel.send(embed=discord.Embed( description="Image too large", color=discord.Color.red())) track_command(ctx.author.id, message) # tracks the most recent command of a user os.remove(name + '.png')
async def bdraw(ctx): url = get_image_url(ctx) if url == 0: output = draw_text(ctx.message.content[7:], Path('memes/barrington/bdraw.png')) else: output = overlay_image(url_to_image(url), Path('memes/barrington/bdraw.png')) output.save('barrington-drawing.png') await client.send_file(ctx.message.channel, 'barrington-drawing.png') os.remove('barrington-drawing.png')
async def mdraw(ctx): url = get_image_url(ctx) if url == 0: output = draw_text(ctx.message.content[7:], Path('memes/marius/draw.png')) else: output = overlay_image(url_to_image(url), Path('memes/marius/draw.png')) name = 'marius-drawing.png' output.save(name) await client.send_file(ctx.message.channel, name) os.remove(name)
async def mdraw(ctx): url = get_image_url(ctx) if url == 0: output = draw_text(ctx.message.content[7:], Path('memes/marius/draw.png'), marius_origin) else: output = overlay_image(url_to_image(url), Path('memes/marius/draw.png'), marius_origin) name = 'marius-drawing.png' output.save(name) message = await client.send_file(ctx.message.channel, name) track_command(ctx.message.author.id, message) os.remove(name)
async def bdraw(ctx): url = get_image_url(ctx) if url == 0: output = draw_text(ctx.message.content[7:], Path('memes/barrington/bdraw.png'), barr_origin) else: output = overlay_image(url_to_image(url), Path('memes/barrington/bdraw.png'), barr_origin) output.save('barrington-drawing.png') message = await client.send_file(ctx.message.channel, 'barrington-drawing.png') track_command(ctx.message.author.id, message) os.remove('barrington-drawing.png')
async def noise_filter(ctx): """Command to apply a noise filter on the inputted image Args: - ctx: context that the command occured use this to access the message and other attributes """ channel = ctx.channel url = overlay.get_image_url(ctx, 7) if url == 0: await channel.send(embed=discord.Embed(description="Invalid Image", color=discord.Color.red())) return output = filters.scramble_pixels(overlay.url_to_image(url)) output.save('noise.png') try: message = await channel.send(file=discord.File('noise.png')) except: message = await channel.send(embed=discord.Embed( description="Image too large", color=discord.Color.red())) custom_meme.track_command(ctx.author.id, message) os.remove('noise.png')
async def highlight_edge(ctx, *args): """Command to apply an edge highlighting algorithm to a given image Args: - ctx: context that the command occured use this to access the message and other attributes """ channel = ctx.channel url = overlay.get_image_url_args(ctx, args, 1, 0) if url == 0: await channel.send(embed=discord.Embed(description="Invalid Image"), color=discord.Color.red()) return output = filters.highlight_image(overlay.url_to_image(url)) output.save('highlighted.png') try: message = await channel.send(file=discord.File('highlighted.png')) except: message = await channel.send(embed=discord.Embed( description="Image too large", color=discord.Color.red())) custom_meme.track_command(ctx.author.id, message) os.remove('highlighted.png')
async def make_okay(ctx): """Command to turn a given image into a video where marius says 'okay' in the background Args: - ctx: context that the command occured use this to access the message and other attributes """ channel = ctx.channel url = overlay.get_image_url(ctx, 6) if url == 0: await channel.send(embed=discord.Embed(description="Invalid Image", color=discord.Color.red())) return clip = filters.make_okay_clip(overlay.url_to_image(url)) clip.write_videofile("okay.mp4", audio="sfx/okayturnedupto8.mp3", fps=24) try: message = await channel.send(file=discord.File("okay.mp4")) except: message = await channel.send(embed=discord.Embed( description="Image too large", color=discord.Color.red())) custom_meme.track_command(ctx.author.id, message) os.remove("okay.mp4")
def test_overlay06(): """Test url_to_image function""" fname = base + '06.png' tname = os.path.join(tdir, fname) rname = os.path.join(refd, fname) image_link = 'https://cdn.discordapp.com/attachments/501594682820788224/701087623925465108/barrify.png' output = overlay.url_to_image(image_link) output.save(tname) tsize = os.path.getsize(tname) print(glob.glob(tname), '[', tsize, 'bytes', ']') rsize = os.path.getsize(rname) print(glob.glob(rname), '[', rsize, 'bytes', ']') result = compare_images(rname, tname, tol=TOLERANCE) if result is not None: print('result=', result) assert result is None
async def custom_edge_highlight(ctx, *args): """Command to highlight an image's edges and turn them into a given color Args: - ctx: context that the command occured use this to access the message and other attributes - *args: arguments passed in with the command (in this case the RGB values for the edge color) """ channel = ctx.channel try: red = int(args[0]) green = int(args[1]) blue = int(args[2]) except: await channel.send(embed=discord.Embed( description="Invalid Parameters", color=discord.Color.red())) return url = overlay.get_image_url_args(ctx.message, args, 4, 3) if url == 0: await channel.send(embed=discord.Embed(description="Invalid Image", color=discord.Color.red())) return output = filters.custom_edge_highlight_image(overlay.url_to_image(url), red, green, blue) if output == 0: #if the RGB values are invalid await channel.send(embed=discord.Embed( description= "Invalid RGB Values, please input numbers between 0-255", color=discord.Color.red())) return output.save('custom_highlight.png') try: message = await channel.send(file=discord.File('custom_highlight.png')) except: message = await channel.send(embed=discord.Embed( description="Image too large", color=discord.Color.red())) return custom_meme.track_command(ctx.author.id, message) os.remove('custom_highlight.png')
def test_mirrory(): fname = base + '_mirrorY.png' tname = os.path.join(tdir, fname) rname = os.path.join(refd, fname) image = overlay.url_to_image( 'https://cdn.discordapp.com/attachments/501594682820788224/701320190738038837/zoomarun_final.png' ) output = filters.mirror_y(image) output.save(tname) tsize = os.path.getsize(tname) print(glob.glob(tname), '[', tsize, 'bytes', ']') rsize = os.path.getsize(rname) print(glob.glob(rname), '[', rsize, 'bytes', ']') result = compare_images(rname, tname, tol=TOLERANCE) if result is not None: print('result=', result) assert result is None