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

94 lines
3.1 KiB
Python
Raw Normal View History

2020-03-16 21:02:05 -04:00
# SPDX-License-Identifier: GPL-2.0-or-later
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
2020-05-16 22:50:34 -04:00
from functools import partial
2023-10-16 09:46:31 -04:00
from random import randint, uniform
from PIL import Image, ImageEnhance
2023-10-16 09:46:31 -04:00
2020-06-19 14:40:41 -04:00
from ubot.micro_bot import ldr
2023-10-16 09:46:31 -04:00
2020-07-16 13:41:42 -04:00
@ldr.add("deepfry", pattern_extra="(f|)", userlocking=True)
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
if event.is_reply:
reply_message = await event.get_reply_message()
data = await ldr.get_image(reply_message)
2023-10-16 09:46:31 -04:00
2020-05-09 12:25:08 -04:00
if not data:
2020-06-06 11:04:14 -04:00
await event.reply("I can't deep fry that!")
2023-10-16 09:46:31 -04:00
return
else:
data = await ldr.get_image(event)
2023-10-16 09:46:31 -04:00
2020-05-09 12:25:08 -04:00
if not data:
2020-06-06 11:04:14 -04:00
await event.reply("Reply to an image or sticker or caption an image to deep fry it!")
2023-10-16 09:46:31 -04:00
return
# Download photo (highres) as byte array
image = io.BytesIO()
await event.client.download_media(data, image)
image = Image.open(image)
for _ in range(frycount):
2020-05-16 22:50:34 -04:00
image = await event.client.loop.run_in_executor(ldr.thread_pool, partial(deepfry, 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)
2020-05-09 12:25:08 -04:00
if event.is_reply:
await reply_message.reply(file=fried_io, force_document=as_file)
2023-10-16 09:46:31 -04:00
else:
2020-05-09 12:25:08 -04:00
await event.reply(file=fried_io, force_document=as_file)
2023-10-16 09:46:31 -04:00
2020-05-16 22:50:34 -04:00
def deepfry(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