summaryrefslogtreecommitdiff
path: root/cogs/Image.py
blob: aea7e2268e8a1ca29f7a8aa3e6408d3095f97b85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
import math
import re

from discord.ext import commands
import discord
import os
from io import BytesIO
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
from PIL import ImageFilter
from PIL import ExifTags
import time
import mimetypes
import requests
import json
import importlib
import utils
import random
import traceback
importlib.reload(utils)


class ImageCmd(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command(
        aliases=[],
        application_command_meta=commands.ApplicationCommandMeta(
            options=[
                discord.ApplicationCommandOption(
                    name="template_name",
                    description="The name of the template.",
                    type=discord.ApplicationCommandOptionType.string,
                    required=True,
                )
            ],
        )
    )
    @commands.defer(ephemeral=False)
    @commands.bot_has_permissions(send_messages=True)
    async def image(self, ctx, template_name):
        """A meme creating command. Use /imagelist for available templates."""
        # Check to see if the guild has it's own image folder
        if not os.path.exists(f"./overlays/{ctx.guild.id}"):
            os.mkdir(f"./overlays/{ctx.guild.id}")
            os.mkdir(f"./overlays/{ctx.guild.id}/images")

        templates = self.get_image_template_names_listed()
        print(f"templates {templates}")
        if template_name.casefold() not in templates:
            await ctx.send(f"Image templates: {', '.join(templates)}")
            return

        image = await self.get_image(ctx, "image")
        print(f"image {image}")
        if not image:
            emb = await utils.embed(ctx, "No image found", "To use this command, an image must be within the 20 most recent messages.")
            await ctx.send(embed=emb)
            return

        overlay_return = await self.img_edit_overlay(image, template_name, ctx.guild.id, ctx)
        file = await self.pil_to_discordfile(overlay_return)
        await ctx.send(file=file)

    @classmethod
    async def pil_to_discordfile(cls, file, filename='image.png'):
        temp_image = BytesIO()
        file.save(temp_image, 'PNG')
        temp_image.seek(0)
        return discord.File(fp=temp_image, filename=filename)

    @commands.defer(ephemeral=True)
    @commands.command(aliases=[], application_command_meta=commands.ApplicationCommandMeta(options=[]))
    async def imagelist(self, ctx):
        """Show all available templates to use with /image"""
        image_list = ImageList()
        image_list.generate_image_list()
        file = await image_list.get_image_for_discord()
        await ctx.send(file=file)

    @commands.defer(ephemeral=False)

    @commands.command(
        aliases=[],
        application_command_meta=commands.ApplicationCommandMeta(
            options=[
                discord.ApplicationCommandOption(
                    name="top_text",
                    description="Top text for this image.",
                    type=discord.ApplicationCommandOptionType.string,
                    required=False,
                ),
                discord.ApplicationCommandOption(
                    name="bottom_text",
                    description="Bottom text for this image.",
                    type=discord.ApplicationCommandOptionType.string,
                    required=False,
                )
            ],
        )
    )
    async def text(self, ctx, top_text="", bottom_text=""):
        """Add a caption to the last image posted."""
        print(f"ctx {ctx}")
        top_text = "" if not 'top_text' in ctx.given_values else ctx.given_values['top_text']
        bottom_text = "" if not 'bottom_text' in ctx.given_values else ctx.given_values['bottom_text']
        print(f"top_text {top_text}")
        print(f"bottom_text {bottom_text}")
        img = await self.get_image(ctx, "image")
        try:
            if img:
                str_filename = await self.img_edit_add_text(img, top_text, bottom_text, ctx.guild.id)
                await ctx.send(file=discord.File(open(str_filename, 'rb'), str_filename))

            else:
                emb = await utils.embed(ctx, f"Unable to retrieve image", "I wasn't able to see a image in the recent chat history.")
                await ctx.send(embed=emb)
        except Exception as e:
            traceback.print_exc()
            print(f"Error: {e}")
            emb = await utils.embed(ctx, f"Error", e)
            await ctx.send(embed=emb)

    @commands.defer(ephemeral=False)
    @commands.command(aliases=[], application_command_meta=commands.ApplicationCommandMeta(options=[]))
    async def deep(self, ctx):
        """Send the last image posted through a Deep Dream"""
        print(f"ctx {ctx}")
        img = await self.get_image(ctx, "url")
        emb = await self.deep_master(ctx, img)
        await ctx.send(embed=emb)

    @commands.defer(ephemeral=False)
    @commands.context_command(name="DeepAI - Deep Dream")
    async def deep_context(self, ctx: discord.ext.commands.context.Context, message: discord.message.Message):
        """Send the last image posted through a Deep Dream - Via Context Menu"""
        if len(message.embeds) > 0 and message.embeds[0].image:
            image = message.embeds[0].image.url
        elif len(message.attachments) > 0:
            image = message.attachments[0].url
        else:
            emb = await utils.embed(ctx, f"Unable to retrieve image", "I wasn't able to see an image in the message you selected.")
            await ctx.send(embed=emb)
            return

        emb = await self.deep_master(ctx, image)
        await ctx.send(embed=emb)

    async def deep_master(self, ctx, img):
        """Send the last image posted through a Deep Dream"""
        start_time = time.time()

        if img:
            response = requests.post("https://api.deepai.org/api/deepdream",
                                     headers={'api-key': 'd1a66541-5c1f-4862-b011-46972d1287ee'},
                                     data={'image': img})
            return_data = json.loads(response.text)

            emb = await utils.embed(ctx,
                                    title="Deep Dream",
                                    message="",
                                    footer=f"\nDone in {round(time.time() - start_time, 2)} seconds.",
                                    image=return_data['output_url'])
        else:
            emb = await utils.embed(ctx, f"Unable to retrieve image", "I wasn't able to see a image in the recent chat history.")
        return emb

    @commands.defer(ephemeral=False)
    @commands.command(aliases=[], application_command_meta=commands.ApplicationCommandMeta(options=[]))
    async def intensify(self, ctx, intensity: int = 1):
        """Shake images with intensity and violence."""
        img = await self.get_image(ctx.channel, "image")

        img.paste(img, (0, 0))

        if img:
            img_x, img_y = img.size

            max_scale = 200

            img_x = float(img_x / 100)
            img_y = float(img_y / 100)
            while img_x < max_scale or img_y < max_scale:
                img_x = img_x + float(img_x / 100)
                img_y = img_y + float(img_y / 100)
            img_x = int(img_x)
            img_y = int(img_y)
            img = img.resize((img_x, img_y), Image.ANTIALIAS)

            frames = []
            for i in range(4):
                img_x_nudge = random.randint(round(-(img_x / 100 * intensity)), (round(img_x / 100 * intensity)))
                img_y_nudge = random.randint(round(-(img_y / 100 * intensity)), (round(img_y / 100 * intensity)))

                im = Image.new('RGBA', (img_x, img_y), (255, 255, 255, 0))
                # im.convert('RGBA')
                im.paste(img, (img_x_nudge, img_y_nudge))
                # im.convert('RGBA')

                frames.append(im)
                # im.save(f'test{i}.png')

            arr = BytesIO()
            frames[0].save('test_gif1.gif', format='GIF', save_all=True, duration=30, loop=0, transparency=0, disposal=0)
            frames[0].save('test_gif2.gif', format='GIF', save_all=True, duration=30, loop=0, transparency=255, disposal=0)
            frames[0].save('test_gif3.gif', format='GIF', save_all=True, duration=30, loop=0, transparency=0, disposal=1)
            frames[0].save('test_gif4.gif', format='GIF', save_all=True, duration=30, loop=0, transparency=255, disposal=1)
            frames[0].save('test_gif5.gif', format='GIF', save_all=True, duration=30, loop=0, transparency=0, disposal=2)
            frames[0].save('test_gif6.gif', format='GIF', save_all=True, duration=30, loop=0, transparency=255, disposal=2)
            frames[0].save('intensify.gif', format='GIF', append_images=frames[1:], save_all=True, duration=30, loop=0, disposal=0)
            # img.save(arr, format='GIF', append_images=frames[1:], save_all=True, duration=30, loop=0, transparency=0, disposal=2)

            img_debug = Image.new('RGBA', (img_x, img_y), (0, 0, 0, 0))
            draw = ImageDraw.Draw(img_debug)
            draw.ellipse((25, 25, 75, 75), fill=(255, 0, 0))
            img_debug.save(arr, format='GIF', transparency=0)

            # first_frame = frames.pop(0)
            # first_frame.save("out.gif", save_all=True, append_images=frames, duration=30, loop=0, transparency=0, disposal=2)

            arr.seek(0)
            await ctx.send(file=discord.File(arr, 'intensify.gif'))
            return
        else:
            await ctx.send(content="Cannot find image.")

    async def get_image(self, channel: discord.TextChannel, return_as: str):
        image = None
        async for message in channel.history(limit=20, before=None, after=None):
            re_matches = re.findall(r"(https?://\S+)", message.content)

            if len(message.embeds) > 0 and message.embeds[0].image:
                image = message.embeds[0].image.url
                break

            elif len(message.attachments) > 0:
                image = message.attachments[0].url
                break

            elif len(re_matches) > 0:
                for match in re_matches:
                    if '?' in match:
                        match = match.split("?")[0]

                    if match.endswith(('jpg', 'jpeg', 'png', 'gif',)):
                        image = match
                        break
                if image:
                    break

        if not image:
            return None

        if return_as == 'url':
            return image

        elif return_as == 'image':
            image = Image.open(BytesIO(requests.get(image).content))

            if image.format in ['GIF']:
                return image
            else:
                rotation = await self.get_exif_data(image)
                if rotation:
                    image = image.rotate(rotation, expand=True)
                return image

    @staticmethod
    async def send_image(ctx, file, title, message):

        emb = await utils.embed(ctx, title=title, message=message)
        await ctx.send(embed=emb, file=discord.File(file, file.name))

        if ctx.channel.permissions_for(ctx.me).manage_messages:
            await ctx.message.delete()

    @staticmethod
    async def get_exif_data(image):
        exif = image._getexif()
        for orientation in ExifTags.TAGS.keys():
            if ExifTags.TAGS[orientation] == 'Orientation':
                break

        print(f"orientation {orientation}")

        if exif and orientation and exif[orientation] == 3:
            return 180
        elif exif and orientation and exif[orientation] == 6:
            return 270
        elif exif and orientation and exif[orientation] == 8:
            return 90
        else:
            return None

    @commands.defer(ephemeral=False)
    @commands.command(hidden=True)
    async def emboss(self, ctx, intensity=1):
        """Image Effect: Emboss"""
        img = await self.get_image(ctx.channel, "image")
        file = await self.img_edit_effect(img, ImageFilter.EMBOSS, ctx, intensity)
        await ctx.send(file=file)

    @commands.defer(ephemeral=False)
    @commands.command(hidden=True)
    async def blur(self, ctx, intensity=1):
        """Image Effect: Blur"""
        img = await self.get_image(ctx.channel, "image")
        file = await self.img_edit_effect(img, ImageFilter.BLUR, ctx, intensity)
        await ctx.send(file=file)

    @commands.defer(ephemeral=False)
    @commands.command(hidden=True)
    async def sharpen(self, ctx, intensity=1):
        """Image Effect: Sharpen"""
        img = await self.get_image(ctx.channel, "image")
        file = await self.img_edit_effect(img, ImageFilter.SHARPEN, ctx, intensity)
        await ctx.send(file=file)

    @staticmethod
    def is_url_image(url):
        mimetype, encoding = mimetypes.guess_type(url)
        return mimetype and mimetype.startswith('image')

    @staticmethod
    def check_url(url):
        try:
            request = requests.get(url)
            if request.status_code == 200:
                return True
        except Exception as e:
            return False

    def is_image_and_ready(self, url):
        return self.is_url_image(url) and self.check_url(url)

    @staticmethod
    def get_image_template_names(guild_id):
        template_files = []

        for file in os.listdir(f"./overlays/{guild_id}/"):
            if str(file).startswith("overlay"):
                template_files.append(f"{file.replace('overlay_', '').split('.')[0]}")
            elif str(file).startswith("frame"):
                template_files.append(f"{file.replace('frame_', '').split('.')[0]}")

        return template_files

    @staticmethod
    def get_image_template_names_listed():
        images = []

        for file in os.listdir(f"./config/images/"):
            if 'templated_image.png' in file:
                continue
            else:
                images.append(f"{file.split('.')[0]}")

        # Return formatted lists
        if len(images) > 0:
            images.sort()
            return images
        else:
            return []

    async def img_edit_add_text(self, img, top_text: str, bottom_text: str, guild_id):
        # Define images
        str_filename = f"./overlays/{guild_id}/tmp_text.png"

        img = await self.img_edit_add_text_write(img, top_text, "top")
        print(f"Success top")
        img = await self.img_edit_add_text_write(img, bottom_text, "bottom")
        print(f"Success bottom")

        # Save Image and return
        img.save(str_filename)
        return str_filename

    @staticmethod
    async def img_edit_add_text_write(img, input_text, text_pos):
        # Check font is less than image width
        img_w, img_h = img.size
        img_draw = ImageDraw.Draw(img)
        int_font = 90
        font = ImageFont.truetype("./fonts/calibri/calibri.ttf", int_font)
        text_w, text_h = img_draw.textsize(input_text, font=font)

        while text_w > img_w:
            int_font = int_font - 1
            font = ImageFont.truetype("./fonts/calibri/calibri.ttf", int_font)
            text_w, text_h = img_draw.textsize(input_text, font=font)

        # Set text
        int_x = (img_w - text_w) / 2
        int_border = 3
        if text_pos == "top":
            int_y = -5
        elif text_pos == "bottom":
            int_y = img_h - text_h - 10

        # Add Text diagonals
        img_draw.text((int_x - int_border, int_y - int_border), input_text, font=font, fill="black")
        img_draw.text((int_x + int_border, int_y - int_border), input_text, font=font, fill="black")
        img_draw.text((int_x - int_border, int_y + int_border), input_text, font=font, fill="black")
        img_draw.text((int_x + int_border, int_y + int_border), input_text, font=font, fill="black")

        # Add Text straights
        img_draw.text((int_x + int_border, int_y), input_text, font=font, fill="black")
        img_draw.text((int_x - int_border, int_y), input_text, font=font, fill="black")
        img_draw.text((int_x, int_y + int_border), input_text, font=font, fill="black")
        img_draw.text((int_x, int_y - int_border), input_text, font=font, fill="black")
        img_draw.text((int_x, int_y), input_text, (255, 255, 255), font=font)  # Inner Text
        return img

    async def img_edit_overlay(self, template, overlay_name, guild_id, ctx):
        image_overlay_type = 'overlay'
        match_found = False

        # Define order for overlays
        if image_overlay_type == 'overlay':
            bg = Image.open(f"./config/images/{overlay_name}.png")
            fg = template

            img = Image.new("RGBA", bg.size)
            str_filename = f"./config/images/templated_image.png"

            im_boundry, im_topleft = self.get_image_transparency_boundry(f"./config/images/{overlay_name}.png")

            fg_scaled_x, fg_scaled_y = fg.size
            fg_scaled_x = float(fg_scaled_x / 100)
            fg_scaled_y = float(fg_scaled_y / 100)

            while fg_scaled_x < im_boundry[0] or fg_scaled_y < im_boundry[1]:
                fg_scaled_x = fg_scaled_x + float(fg_scaled_x / 100)
                fg_scaled_y = fg_scaled_y + float(fg_scaled_y / 100)

            fg_scaled_x = int(fg_scaled_x)
            fg_scaled_y = int(fg_scaled_y)
            fg_scaled_x_offset = round((fg_scaled_x - im_boundry[0]) / 2)
            fg_scaled_y_offset = round((fg_scaled_y - im_boundry[1]) / 2)

            # resize
            fg = fg.resize((fg_scaled_x, fg_scaled_y), Image.ANTIALIAS)

            # Paste image
            img.paste(fg, (im_topleft[0] - fg_scaled_x_offset, im_topleft[1] - fg_scaled_y_offset),
                      fg.convert('RGBA'))
            img.paste(bg, (0, 0), bg.convert('RGBA'))

        # elif image_overlay_type == 'frame':
        else:
            fg = Image.open(f"./config/images/{overlay_name}.png")
            bg = Image.open(img)

            img = Image.new("RGBA", bg.size)
            str_filename = f"./config/images/{overlay_name}.png"

            bg_width, bg_height = bg.size

            fg_scaled_x, fg_scaled_y = fg.size
            fg_scaled_x = float(fg_scaled_x / 100)
            fg_scaled_y = float(fg_scaled_y / 100)

            max_image_width = 15

            while fg_scaled_x < ((bg_width / 100) * max_image_width):
                fg_scaled_x = fg_scaled_x + float(fg_scaled_x / 100)
                fg_scaled_y = fg_scaled_y + float(fg_scaled_y / 100)

            fg_scaled_x = int(fg_scaled_x)
            fg_scaled_y = int(fg_scaled_y)

            # resize
            fg = fg.resize((fg_scaled_x, fg_scaled_y), Image.ANTIALIAS)

            # Paste image
            img.paste(bg, (0, 0), bg.convert('RGBA'))
            img.paste(fg, (0, 0), fg.convert('RGBA'))

        # img.save(str_filename)
        return img

    @classmethod
    async def img_edit_effect(cls, img, effect_type, ctx, cycles: int):
        while cycles >= 0:
            img = img.filter(effect_type)
            cycles = cycles-1

        return await cls.pil_to_discordfile(img)

    @staticmethod
    def get_image_transparency_boundry(image_overlay):
        im = Image.open(image_overlay)
        image_x, image_y = im.size
        iter_x = iter_y = 1
        top_co = None
        lef_co = im.size
        bot_co = [1, 1]
        rig_co = [1, 1]

        while iter_y < image_y:
            while iter_x < image_x:
                ret_pixel = im.getpixel((iter_x, iter_y))

                # if str(ret_pixel[3]) == "0":
                if int(ret_pixel[3]) < 255:
                    # top pixel will be the first y coordinate match
                    if top_co is None:
                        top_co = [iter_x, iter_y]

                    # Check the x iteration is less than lef_co
                    if iter_x < lef_co[0]:
                        lef_co = [iter_x, iter_y]

                    # Check the x iteration is less than lef_co
                    if iter_x > rig_co[0]:
                        rig_co = [iter_x, iter_y]

                    # Set bot_co to iter_y
                    bot_co = [iter_x, iter_y]

                iter_x = iter_x + 1
            iter_y = iter_y + 1
            iter_x = 1

        top_left = [lef_co[0], top_co[1]]
        bottom_right = [rig_co[0], bot_co[1]]

        boundry_size = [bottom_right[0] - top_left[0], bottom_right[1] - top_left[1]]
        # print(f"lef_co: {lef_co}, top_co: {top_co}, rig_co: {rig_co}, bot_co: {bot_co}")
        # print(f"Co-ordinate 1: ({top_left}), Co-ordinate 2: ({bottom_right}). Boundry size: {boundry_size}")
        return boundry_size, top_left


def setup(bot):
    print("INFO: Loading [Image]... ", end="")
    bot.add_cog(ImageCmd(bot))
    print("Done!")


def teardown(bot):
    print("INFO: Unloading [Image]")


class ImageList:
    def __init__(self):
        self.image_thumbnail_scale = (100, 100)
        self.images_in_dir = []
        self.get_images()
        self.image_scale = None
        self.get_base_image_scale()

        self.x_offset = 0
        self.y_offset = 0

        self.max_x_size = 96
        self.max_y_size = 80

        self.image = Image.new('RGBA', self.image_scale, color='#202020')
        return

    def get_base_image_scale(self):
        row_count = math.ceil(len(self.images_in_dir) / math.ceil(math.sqrt(len(self.images_in_dir))))
        column_count = math.ceil(len(self.images_in_dir) / row_count)
        self.image_scale = (row_count*100, column_count*100)

    def get_images(self):
        for file in os.listdir(f"./config/images/"):
            self.images_in_dir.append(f"{file.split('.')[0]}")

        # Return formatted lists
        if len(self.images_in_dir) > 0:
            self.images_in_dir.sort()

    def generate_image_list(self):
        for image_name in self.images_in_dir:
            print(f"Processing {image_name}...")
            image_template = Image.open(f'./config/images/{image_name}.png')
            image_scale_x, image_scale_y = image_template.size
            image_scale_x_scaled = round(image_scale_x / self.image_thumbnail_scale[0])
            image_scale_y_scaled = round(image_scale_y / self.image_thumbnail_scale[1])
            while True:
                image_scale_x_scaled += round((image_scale_x / self.image_thumbnail_scale[0]))
                image_scale_y_scaled += round((image_scale_y / self.image_thumbnail_scale[1]))

                if image_scale_x_scaled >= self.max_x_size or image_scale_y_scaled >= self.max_y_size:
                    if image_scale_x_scaled > self.max_x_size:
                        image_scale_x_scaled = self.max_x_size
                    if image_scale_y_scaled > self.max_y_size:
                        image_scale_y_scaled = self.max_y_size
                    break

            # Determine the centered X of the thumbnails position
            centering_image_x = (round(self.image_thumbnail_scale[0] / 2)) - (round(image_scale_x_scaled / 2))

            # Place the green in the background
            image_green = Image.new('RGBA', (image_scale_x_scaled, image_scale_y_scaled), color='#00FF00')
            self.image.paste(image_green, (self.x_offset + centering_image_x, self.y_offset))

            # Place the template in the foreground with it's own mask to see the green
            image_template = image_template.resize((image_scale_x_scaled, image_scale_y_scaled))

            self.image.paste(image_template, (self.x_offset + centering_image_x, self.y_offset), mask=image_template)

            d = ImageDraw.Draw(self.image)
            font = ImageFont.truetype("./fonts/roboto/Roboto-Regular.ttf", 10)
            font_colour = (255, 255, 255, 255)
            name_w, name_h = d.textsize(image_name, font=font)

            d.text((self.x_offset + (100-name_w)/2, self.y_offset+80), image_name, font=font, fill=font_colour)

            self.x_offset += self.image_thumbnail_scale[0]
            if self.x_offset >= self.image_scale[0]:
                self.x_offset = 0
                self.y_offset += self.image_thumbnail_scale[0]

    async def get_image_for_discord(self, filename='image.png'):
        temp_image = BytesIO()
        self.image.save(temp_image, 'PNG')
        temp_image.seek(0)
        return discord.File(fp=temp_image, filename=filename)