-
-
Notifications
You must be signed in to change notification settings - Fork 40
add basic image manipulation #469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,97 @@ | ||||||||||||||
| import io | ||||||||||||||
|
|
||||||||||||||
| import discord | ||||||||||||||
| import httpx | ||||||||||||||
| from discord import app_commands | ||||||||||||||
| from discord.ext import commands | ||||||||||||||
| from loguru import logger | ||||||||||||||
| from PIL import Image, ImageEnhance, ImageOps | ||||||||||||||
|
|
||||||||||||||
| from tux.utils.embeds import EmbedCreator | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| class ImgEffect(commands.Cog): | ||||||||||||||
| def __init__(self, bot: commands.Bot) -> None: | ||||||||||||||
| self.bot = bot | ||||||||||||||
| self.allowed_mimetypes = [ | ||||||||||||||
| "image/jpeg", | ||||||||||||||
| "image/png", | ||||||||||||||
| ] | ||||||||||||||
|
|
||||||||||||||
| imgeffect = app_commands.Group(name="imgeffect", description="Image effects") | ||||||||||||||
|
|
||||||||||||||
| @imgeffect.command( | ||||||||||||||
| name="deepfry", | ||||||||||||||
| description="Deepfry an image", | ||||||||||||||
| ) | ||||||||||||||
| async def deepfry(self, interaction: discord.Interaction, image: discord.Attachment) -> None: | ||||||||||||||
| """ | ||||||||||||||
| Deepfry an image. | ||||||||||||||
|
|
||||||||||||||
| Parameters | ||||||||||||||
| ---------- | ||||||||||||||
| interaction : discord.Interaction | ||||||||||||||
| The interaction object for the command. | ||||||||||||||
| image : discord.File | ||||||||||||||
| The image to deepfry. | ||||||||||||||
| """ | ||||||||||||||
|
|
||||||||||||||
| # check if the image is a image | ||||||||||||||
| logger.info(f"Content type: {image.content_type}, Filename: {image.filename}, URL: {image.url}") | ||||||||||||||
| if image.content_type not in self.allowed_mimetypes: | ||||||||||||||
| logger.error("The file is not a permitted image.") | ||||||||||||||
| embed = EmbedCreator.create_error_embed( | ||||||||||||||
| title="Invalid File", | ||||||||||||||
| description="The file must be an image. Allowed types are PNG, JPEG, and JPG.", | ||||||||||||||
| interaction=interaction, | ||||||||||||||
| ) | ||||||||||||||
| await interaction.response.send_message(embed=embed, ephemeral=True) | ||||||||||||||
| return | ||||||||||||||
|
|
||||||||||||||
| # say that the image is being processed | ||||||||||||||
| logger.info("Processing image...") | ||||||||||||||
| await interaction.response.send_message("Processing image...") | ||||||||||||||
|
|
||||||||||||||
| # open url with PIL | ||||||||||||||
| logger.info("Opening image with PIL and HTTPX...") | ||||||||||||||
| async with httpx.AsyncClient() as client: | ||||||||||||||
electron271 marked this conversation as resolved.
Show resolved
Hide resolved
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (performance): Consider using a single httpx.AsyncClient instance for the cog Creating a new AsyncClient for each request may be inefficient. Consider creating a single client instance in the init method and reusing it across the cog. |
||||||||||||||
| response = await client.get(image.url) | ||||||||||||||
| pil_image = Image.open(io.BytesIO(response.content)) | ||||||||||||||
electron271 marked this conversation as resolved.
Show resolved
Hide resolved
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Add error handling for image processing operations Consider wrapping the image processing operations in a try-except block to catch and handle potential errors, such as corrupted images or unexpected formats.
Suggested change
|
||||||||||||||
| pil_image = pil_image.convert("RGB") | ||||||||||||||
| logger.info("Image opened with PIL.") | ||||||||||||||
|
|
||||||||||||||
| # resize image to 50% then back to original size | ||||||||||||||
| logger.info("Resizing image...") | ||||||||||||||
| pil_image = pil_image.resize((int(pil_image.width * 0.25), int(pil_image.height * 0.25))) | ||||||||||||||
| logger.info("Image resized.") | ||||||||||||||
|
|
||||||||||||||
| # increase sharpness | ||||||||||||||
| logger.info("Increasing sharpness...") | ||||||||||||||
| pil_image = ImageEnhance.Sharpness(pil_image).enhance(100.0) | ||||||||||||||
| logger.info("Sharpness increased.") | ||||||||||||||
|
|
||||||||||||||
| logger.info("Adjusting color...") | ||||||||||||||
| r = pil_image.split()[0] | ||||||||||||||
| r = ImageEnhance.Contrast(r).enhance(2.0) | ||||||||||||||
electron271 marked this conversation as resolved.
Show resolved
Hide resolved
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Parameterize enhancement factors for flexibility Consider making the enhancement factors (like 2.0 for contrast, 1.5 for brightness) configurable parameters. This would allow for easy adjustment of the 'deepfry' effect without changing the code. |
||||||||||||||
| r = ImageEnhance.Brightness(r).enhance(1.5) | ||||||||||||||
|
|
||||||||||||||
| colours = ((254, 0, 2), (255, 255, 15)) | ||||||||||||||
| r = ImageOps.colorize(r, colours[0], colours[1]) | ||||||||||||||
|
|
||||||||||||||
| pil_image = Image.blend(pil_image, r, 0.75) | ||||||||||||||
|
|
||||||||||||||
| logger.info("Color adjustment complete.") | ||||||||||||||
|
|
||||||||||||||
| # send image | ||||||||||||||
| logger.info("Sending image...") | ||||||||||||||
| pil_image = pil_image.resize((int(pil_image.width * 2), int(pil_image.height * 2))) | ||||||||||||||
| arr = io.BytesIO() | ||||||||||||||
| pil_image.save(arr, format="JPEG", quality=1) | ||||||||||||||
| arr.seek(0) | ||||||||||||||
| file = discord.File(arr, filename="deepfried.jpg") | ||||||||||||||
| # edit message with image | ||||||||||||||
| await interaction.followup.send(content="Here is your deepfried image:", file=file) | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| async def setup(bot: commands.Bot) -> None: | ||||||||||||||
| await bot.add_cog(ImgEffect(bot)) | ||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.