Example #1
0
def resize_sprite(sprite, factor: float):
    """ Resize a sprite (string of bytes / rb). """
    image = Image.open(BytesIO(sprite))

    # Resize with the scaled proportions
    width, height = image.size
    width, height = int(width * factor), int(height * factor)
    image = image.resize((width, height), Image.NEAREST)

    # Return the byte-like object
    return utils.convert_image_object(image)
Example #2
0
async def comic(message: discord.Message):
    try:
        messages = mcache[(message.channel, message.server)]
    except KeyError:
        return "Not enough messages."
    stackpointer = 0
    nicks = set()

    #makes sure the first sentence is added
    nicks.add(messages[0][1])

    for msg in range(len(messages) - 1, 0, -1):
        stackpointer += 1
        previous = messages[msg][0] - messages[msg - 1][0]
        nicks.add(messages[msg][1])
        if stackpointer > 10 or previous.total_seconds() > 1800 or len(
                nicks) > 8:
            break

    #fixes first line disappearing when it shouldn't
    if stackpointer is not 0:
        stackpointer += 1
    messages = messages[-1 * stackpointer:]

    #make sure that isn't a message that isn't in nicks (bugfix)
    for msg in messages:
        if msg[1] not in nicks:
            messages.remove(msg)

    panels = []
    panel = []

    for (time, nick, messagel) in messages:
        if len(panel) == 2 or len(panel) == 1 and panel[0][0] == nick:
            panels.append(panel)
            panel = []
        if messagel.count('\x01') >= 2:
            ctcp = messagel.split('\x01', 2)[1].split(' ', 1)
            if len(ctcp) == 1:
                ctcp += ['']
            if ctcp[0] == 'ACTION':
                message = '*' + ctcp[1] + '*'

        panel.append((nick, messagel))

    panels.append(panel)
    #debug info
    print(repr(nicks))
    print(repr(panels))

    image_comic = make_comic(nicks, panels)

    image_comic = utils.convert_image_object(image_comic)
    await client.send_file(message.channel, image_comic, filename="comic.png")
Example #3
0
async def greater(message: discord.Message, text: Annotate.CleanContent):
    """ Gives a **huge** version of emojies. """
    # Parse all unicode and load the emojies
    parsed_emoji, has_custom = await format_emoji(text, message.server)
    assert parsed_emoji, "I couldn't find any emoji in that text of yours."

    # Combine multiple images if necessary, otherwise send just the one
    if len(parsed_emoji) == 1:
        image_fp = utils.convert_image_object(parsed_emoji[0])
        await client.send_file(message.channel, image_fp, filename="emoji.png")
        return

    # See if there's a need to rescale all images.
    height = default_size
    for e in parsed_emoji:
        if e.height < height:
            height = e.height

    # Resize all emoji (so that the height == size) when one doesn't match any of the predetermined sizes
    total_width = 0
    if has_custom:
        for i, e in enumerate(parsed_emoji):
            if e.height > height:
                width = round(e.width * (height / e.height))
                total_width += width
                parsed_emoji[i] = e.resize((width, height), Image.ANTIALIAS)
            else:
                total_width += e.width
    else:
        total_width = len(parsed_emoji) * height

    # Stitch all the images together
    image = Image.new("RGBA", (total_width, height))
    x = 0
    for image_object in parsed_emoji:
        image.paste(image_object, box=(x, 0))
        x += image_object.width

    # Upload the stitched image
    image_fp = utils.convert_image_object(image)
    await client.send_file(message.channel, image_fp, filename="emojies.png")
Example #4
0
async def greater(message: discord.Message, text: Annotate.CleanContent):
    """ Gives a **huge** version of emojies. """
    # Parse all unicode and load the emojies
    parsed_emoji, has_custom = await format_emoji(text, message.server)
    assert parsed_emoji, "I couldn't find any emoji in that text of yours."

    # Combine multiple images if necessary, otherwise send just the one
    if len(parsed_emoji) == 1:
        image_fp = utils.convert_image_object(parsed_emoji[0])
        await client.send_file(message.channel, image_fp, filename="emoji.png")
        return

    # See if there's a need to rescale all images.
    height = default_size
    for e in parsed_emoji:
        if e.height < height:
            height = e.height

    # Resize all emoji (so that the height == size) when one doesn't match any of the predetermined sizes
    total_width = 0
    if has_custom:
        for i, e in enumerate(parsed_emoji):
            if e.height > height:
                width = round(e.width * (height / e.height))
                total_width += width
                parsed_emoji[i] = e.resize((width, height), Image.ANTIALIAS)
            else:
                total_width += e.width
    else:
        total_width = len(parsed_emoji) * height

    # Stitch all the images together
    image = Image.new("RGBA", (total_width, height))
    x = 0
    for image_object in parsed_emoji:
        image.paste(image_object, box=(x, 0))
        x += image_object.width

    # Upload the stitched image
    image_fp = utils.convert_image_object(image)
    await client.send_file(message.channel, image_fp, filename="emojies.png")
Example #5
0
async def send_image(message: discord.Message, image_arg: ImageArg, **params):
    """ Send an image. """
    try:
        if image_arg.gif and gif_support:
            image_fp = BytesIO(image_arg.gif_bytes)
        else:
            image_fp = utils.convert_image_object(image_arg.object, image_arg.format, **params)
    except KeyError as e:
        await client.send_message(message.channel, "Image format `{}` is unsupported.".format(e))
    else:
        await client.send_file(message.channel, image_fp,
                               filename="{}.{}".format(message.author.display_name, image_arg.extension))
Example #6
0
async def greater(message: discord.Message, text: Annotate.Content):
    """ Gives a **huge** version of emojies. """
    # Parse all unicode and load the emojies
    images, total_width, height = await convert_to_images(text)

    # Stitch all the images together
    image = Image.new("RGBA", (total_width, height))
    x = 0
    for image_object in images:
        image.paste(image_object, box=(x, 0))
        x += image_object.width

    # Upload the stitched image
    image_fp = utils.convert_image_object(image)
    await client.send_file(message.channel, image_fp, filename="emojies.png")
Example #7
0
    def modify(self, function, *args, **kwargs):
        """ Modify the image object using the given Image function.
        This function supplies sequence support. """
        if not gif_support or not self.gif:
            self.object = function(self.object, *args, **kwargs)
        else:
            frames = []
            duration = self.object.info.get("duration") / 1000
            for frame in ImageSequence.Iterator(self.object):
                frame_bytes = utils.convert_image_object(function(frame, *args, **kwargs))
                frames.append(imageio.imread(frame_bytes, format="PNG"))

            # Save the image as bytes and recreate the image object
            image_bytes = imageio.mimwrite(imageio.RETURN_BYTES, frames, format=self.format, duration=duration)
            self.object = Image.open(BytesIO(image_bytes))
            self.gif_bytes = image_bytes
Example #8
0
async def gif(message: discord.Message, text: Annotate.CleanContent):
    """ Gives a **huge** version of emojies AS A GIF. """
    images, total_width, height = await convert_to_images(text)

    # Get optional duration
    duration = 0.15

    duration_arg = text.split(" ")[-1]
    if re.match(r"[0-9.]+", duration_arg):
        duration = float(duration_arg) / 10

    frames = []
    for image in images:
        frame_bytes = utils.convert_image_object(image, format="PNG")
        frames.append(imageio.imread(frame_bytes))

    # Make a gif
    image_bytes = imageio.mimwrite(imageio.RETURN_BYTES,
                                   frames,
                                   format="GIF",
                                   duration=duration)
    await client.send_file(message.channel,
                           BytesIO(image_bytes),
                           filename="emojies.gif")