2
0
mirror of https://github.com/Nick80835/microbot synced 2025-08-23 18:49:31 +00:00
microbot/ubot/modules/deepfry.py

80 lines
2.8 KiB
Python
Raw Permalink Normal View History

2023-10-16 09:46:31 -04:00
# Original source for the deepfrying code (used under the following license): https://github.com/Ovyerus/deeppyer
# MIT License
#
# Copyright (c) 2017 Ovyerus
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import io
from random import randint, uniform
from PIL import Image, ImageEnhance
2023-10-16 09:46:31 -04:00
2020-09-13 13:50:36 -04:00
from ubot import ldr
2023-10-16 09:46:31 -04:00
2020-07-21 09:44:28 -04:00
@ldr.add("deepfry", pattern_extra="(f|)", userlocking=True, help="Deepfries images, takes a number of passes as an argument.")
2023-10-16 09:46:31 -04:00
async def deepfryer(event):
as_file = bool(event.other_args[0])
2020-05-09 12:25:08 -04:00
2023-10-16 09:46:31 -04:00
try:
2020-05-02 16:40:26 -04:00
frycount = int(event.args)
2023-10-16 09:46:31 -04:00
if frycount < 1:
frycount = 1
elif frycount > 10:
frycount = 10
except ValueError:
frycount = 1
data = await event.get_image(size_limit=1024 * 3072)
2023-10-16 09:46:31 -04:00
if not data:
await event.reply("Reply to an image or sticker or caption an image to deep fry it!")
return
2023-10-16 09:46:31 -04:00
# Download photo (highres) as byte array
image = io.BytesIO()
await event.client.download_media(data, image)
image = Image.open(image)
for _ in range(frycount):
image = await ldr.run_async(deepfrysync, image)
2023-10-16 09:46:31 -04:00
fried_io = io.BytesIO()
2020-05-09 12:25:08 -04:00
fried_io.name = "image.png"
image.save(fried_io, "PNG")
2023-10-16 09:46:31 -04:00
fried_io.seek(0)
2021-02-13 12:46:22 -05:00
await event.respond(file=fried_io, force_document=as_file, reply_to=event.reply_to_msg_id or event.id)
2023-10-16 09:46:31 -04:00
def deepfrysync(img):
2020-05-09 12:25:08 -04:00
# Generate color overlay
2023-10-16 09:46:31 -04:00
overlay = img.copy()
overlay = ImageEnhance.Contrast(overlay).enhance(uniform(0.7, 1.8))
overlay = ImageEnhance.Brightness(overlay).enhance(uniform(0.8, 1.3))
overlay = ImageEnhance.Color(overlay).enhance(uniform(0.7, 1.4))
# Blend random colors onto and sharpen the image
img = Image.blend(img, overlay, uniform(0.1, 0.4))
img = ImageEnhance.Sharpness(img).enhance(randint(5, 200))
return img