Repository: python-discord/sir-lancebot
Issue: ValueError: empty range for randrange() (10, 9, -1)

Sentry Issue: [SIR-LANCEBOT-4Y](https://sentry.io/organizations/python-discord/issues/2373621629/?referrer=github_integration)

```
ValueError: empty range for randrange() (10, 9, -1)
(4 additional frame(s) were not displayed)
...
  File "bot/exts/evergreen/avatar_modification/_effects.py", line 23, in apply_effect
    im = effect(im, *args)
  File "bot/utils/halloween/spookifications.py", line 58, in get_random_effect
    return effect(im)
  File "bot/utils/halloween/spookifications.py", line 46, in bat
    y = randint(10, bat_size)
  File "random.py", line 248, in randint
    return self.randrange(a, b+1)
  File "random.py", line 226, in randrange
    raise ValueError("empty range for randrange() (%d, %d, %d)" % (istart, istop, width))

Unhandled command error: empty range for randrange() (10, 9, -1)
```

Seems to be caused by profile pictures with a width smaller than 100 pixels due to the this line `y = randint(10, bat_size)` where `bat_size` is defined as `bat_size = randint(wt//10, wt//7)`

Changed files:
bot/exts/evergreen/avatar_modification/avatar_modify.py, bot/exts/evergreen/avatar_modification/_effects.py

diff --git a/bot/exts/evergreen/avatar_modification/_effects.py b/bot/exts/evergreen/avatar_modification/_effects.py
index 46d1a2ab..92244207 100644
--- a/bot/exts/evergreen/avatar_modification/_effects.py
+++ b/bot/exts/evergreen/avatar_modification/_effects.py
@@ -22,6 +22,7 @@ class PfpEffects:
         """Applies the given effect to the image passed to it."""
         im = Image.open(BytesIO(image_bytes))
         im = im.convert("RGBA")
+        im = im.resize((1024, 1024))
         im = effect(im, *args)
 
         bufferedio = BytesIO()
@@ -74,7 +75,6 @@ class PfpEffects:
     @staticmethod
     def pridify_effect(image: Image.Image, pixels: int, flag: str) -> Image.Image:
         """Applies the given pride effect to the given image."""
-        image = image.resize((1024, 1024))
         image = PfpEffects.crop_avatar_circle(image)
 
         ring = Image.open(Path(f"bot/resources/pride/flags/{flag}.png")).resize((1024, 1024))
@@ -283,16 +283,14 @@ class PfpEffects:
         return new_image
 
     @staticmethod
-    def mosaic_effect(img_bytes: bytes, squares: int, file_name: str) -> discord.File:
-        """Separate function run from an executor which turns an image into a mosaic."""
-        avatar = Image.open(BytesIO(img_bytes))
-        avatar = avatar.convert("RGBA").resize((1024, 1024))
+    def mosaic_effect(image: Image.Image, squares: int) -> Image.Image:
+        """
+        Applies a mosaic effect to the given image.
 
-        img_squares = PfpEffects.split_image(avatar, squares)
+        The "squares" argument specifies the number of squares to split
+        the image into. This should be a square number.
+        """
+        img_squares = PfpEffects.split_image(image, squares)
         new_img = PfpEffects.join_images(img_squares)
 
-        bufferedio = BytesIO()
-        new_img.save(bufferedio, format="PNG")
-        bufferedio.seek(0)
-
-        return discord.File(bufferedio, filename=file_name)
+        return new_img
diff --git a/bot/exts/evergreen/avatar_modification/avatar_modify.py b/bot/exts/evergreen/avatar_modification/avatar_modify.py
index 765c316e..7b4ae9c7 100644
--- a/bot/exts/evergreen/avatar_modification/avatar_modify.py
+++ b/bot/exts/evergreen/avatar_modification/avatar_modify.py
@@ -338,10 +338,11 @@ class AvatarModify(commands.Cog):
             img_bytes = await user.avatar_url_as(size=1024).read()
 
             file = await in_executor(
-                PfpEffects.mosaic_effect,
+                PfpEffects.apply_effect,
                 img_bytes,
+                PfpEffects.mosaic_effect,
+                file_name,
                 squares,
-                file_name
             )
 
             if squares == 1: