コード例 #1
0
    async def edit_image(self, ctx: context.Context, url: str, edit_type: str, **kwargs):

        if ctx.message.attachments:
            url = ctx.message.attachments[0].url

        if url is None:
            url = str(ctx.author.avatar_url_as(format='gif' if ctx.author.is_avatar_animated() is True else 'png'))

        try:
            async with self.bot.session.get(url) as response:
                image_bytes = await response.read()
        except Exception:
            raise ArgumentError(f'Something went wrong while trying to get that image. Check the url.')
        else:
            content_type = response.headers.get('Content-Type')
            content_length = response.headers.get('Content-Length')

            if content_type not in ['image/png', 'image/gif', 'image/jpeg', 'image/webp']:
                raise ImageError('That file format is not allowed, only png, gif, jpg and webp are allowed.')

            if content_length and int(content_length) > 15728640:
                raise ImageError('That file is over 15mb.')

        parent_pipe, child_pipe = multiprocessing.Pipe()
        args = (image_operations[edit_type], image_bytes, child_pipe)

        process = multiprocessing.Process(target=do_edit_image, kwargs=kwargs, daemon=True, args=args)
        process.start()

        data = await self.bot.loop.run_in_executor(None, parent_pipe.recv)
        if isinstance(data, ImageError):
            process.terminate()
            raise ImageError('Something went wrong while trying to process that image.')

        process.join()
        process.close()

        image = data['image']
        image_format = data['format']
        image_text = data['text']

        url = 'https://idevision.net/api/media/post'
        headers = {"Authorization": self.bot.config.idevision_key}
        upload_data = aiohttp.FormData()
        upload_data.add_field('file', image, filename=f'image.{image_format.lower()}')

        async with self.bot.session.post(url, data=upload_data, headers=headers) as response:
            if response.status == 413:
                raise ImageError('The image produced was over 20mb.')
            post = await response.json()

        embed = discord.Embed(colour=ctx.colour)
        embed.set_footer(text=image_text)
        embed.set_image(url=post.get('url'))
        return embed
コード例 #2
0
    async def edit_image(self, ctx: context.Context, edit_type: str,  url: str = None, **kwargs) -> discord.Embed:

        if ctx.message.attachments:
            url = ctx.message.attachments[0].url

        if url is None:
            url = str(ctx.author.avatar_url_as(format='gif' if ctx.author.is_avatar_animated() is True else 'png'))

        try:
            async with self.bot.session.get(url) as response:
                image_bytes = await response.read()
        except Exception:
            raise ArgumentError(f'Something went wrong while trying to download that image. Check the URL.')
        else:
            if response.headers.get('Content-Type') not in ['image/png', 'image/gif', 'image/jpeg', 'image/webp']:
                raise ImageError('That file format is not allowed, only png, gif, jpg and webp are allowed.')

            if response.headers.get('Content-Length') and int(response.headers.get('Content-Length')) > 15728640:
                raise ImageError('That file is over 15mb.')

        parent_pipe, child_pipe = multiprocessing.Pipe()
        args = (image_operations[edit_type], image_bytes, child_pipe)

        process = multiprocessing.Process(target=do_edit_image, kwargs=kwargs, daemon=True, args=args)
        process.start()

        data = await self.bot.loop.run_in_executor(None, parent_pipe.recv)
        if isinstance(data, ImageError):
            process.terminate()
            raise ImageError('Something went wrong while trying to process that image.')

        process.join()
        process.close()

        form_data = aiohttp.FormData()
        form_data.add_field('file', data['image'], filename=f'image.{data["format"].lower()}')

        async with self.bot.session.post('https://media.mrrandom.xyz/api/media', headers={"Authorization": self.bot.config.axelweb_token}, data=form_data) as response:

            if response.status == 413:
                raise ImageError('The image produced was over 100mb.')

            post = await response.json()

        embed = discord.Embed(colour=ctx.colour)
        embed.set_footer(text=data['text'])
        embed.set_image(url=f'https://media.mrrandom.xyz/{post.get("filename")}')
        return embed
コード例 #3
0
def do_edit_image(edit_function: typing.Any, image_bytes: bytes, child_pipe: multiprocessing.Pipe, **kwargs):

    try:

        image_original = io.BytesIO(image_bytes)
        image_edited = io.BytesIO()

        with Image(file=image_original) as old_image:
            with Image() as new_image:

                image_format = old_image.format
                if image_format == 'GIF':
                    old_image.coalesce()
                    for old_frame in old_image.sequence:
                        new_frame, image_text = edit_function(old_frame, **kwargs)
                        new_image.sequence.append(new_frame)
                else:
                    new_image, image_text = edit_function(old_image, **kwargs)

                image_format = new_image.format
                new_image.save(file=image_edited)

        image_edited.seek(0)

        child_pipe.send({
            'image': image_edited,
            'format': image_format,
            'text': image_text
        })

        image_original.close()
        image_edited.close()

    except MissingDelegateError:
        child_pipe.send(ImageError())