I have this script which is successfully applying a watermark to an image, resizing the watermark and setting the image “quality”.
I find that it’s actually reducing the quality and resizing it to be smaller than I want.
This is a telegram bot that I’m using, I send the image to the bot, the bot is adding the watermark and then resizing. I don’t want the resizing part at all. I want the original size + the watermark.
QUESTION: How do i change this so it doesn’t resize my original image that the watermark is added to?
CURRENT FLOW
- I sent image to my bot which activates the script
- The script adds a watermark and resizes the image
- The script sends the resized image back to me with the watermark included
DESIRED FLOW
- I send image to my bot which activates script
- The script adds a watermark but does not resize the original image
- The script sends the image back to me with the watermark included
CODE 1
# -*- coding: utf-8 -*-
import telebot
import config
import random
import os
import time
from tools.tools import getting_ready
bot = telebot.TeleBot(config.BOT_TOKEN)
@bot.message_handler(commands=('start', 'help'))
def send_welcome(message):
bot.send_message(message.chat.id, 'Hi, Send or Forward me a PHOTO!')
@bot.message_handler(func=lambda message: True)
def echo_all(message):
bot.reply_to(message, 'Send /start or /help')
@bot.message_handler(content_types=("photo"))
def send_watermark(message):
chat_id = message.chat.id
sent_message = bot.reply_to(message, 'Downloading...')
message_id = sent_message.message_id
file = bot.get_file(message.photo(0).file_id)
downloaded_file = bot.download_file(file.file_path)
bot.edit_message_text(f'Downloaded!nnNow, Genarating Watermarks...nncurrent watermark: {config.WATERMARK}', chat_id=chat_id, message_id=message_id)
__path = 'images/' + str(random.randint(100000, 999999)) + '.tmp'
with open(__path, 'wb') as f:
f.write(downloaded_file)
fname = getting_ready(__path)
os.remove(__path)
time.sleep(2)
bot.edit_message_text('Now, Uploading...', chat_id=chat_id, message_id=message_id)
for i in ('black','white',):
__file = 'images/out/{}/{}'.format(i, fname)
__photo = open(__file, 'rb')
bot.send_photo(message.chat.id, __photo, reply_to_message_id=message.message_id)
os.remove(__file)
bot.delete_message(chat_id=chat_id, message_id=message_id)
print('watermark bot started successfully!')
bot.polling()
CODE 2
from PIL import Image, ImageFont, ImageDraw, ImageEnhance
import hashlib
import piexif
import config
def getting_ready(path):
fname = md5(path) + '.jpg'
image = Image.open(path).convert('RGB')
try:
exif_dict = piexif.load(image.info('exif'))
orientation = exif_dict('0th')(274)
except KeyError:
orientation = None
rotate_values = {3: 180, 6: 270, 8: 90}
if orientation in rotate_values:
image = image.rotate(rotate_values(orientation), expand=True)
for color in ('black', 'white'):
watermark(image, fname, color)
return fname
def watermark(img, new_fname, color):
text = config.WATERMARK
wm = Image.new('RGBA', img.size, (0, 0, 0, 0))
fontsize = img.size(1) // 100 * config.FONT_SIZE
font = ImageFont.truetype(f'tools/fonts/{config.FONT_NAME}', fontsize)
indent = fontsize // 8
w, h = font.getsize(text)
text_position = (img.size(0) - w - indent, img.size(1) - h - indent)
draw = ImageDraw.Draw(wm, 'RGBA')
draw.text(text_position, text, font=font, fill=color)
alpha = wm.split()(3)
alpha = ImageEnhance.Brightness(alpha).enhance(config.TRANSPARENCY)
wm.putalpha(alpha)
out_path = 'images/out/{}/{}'.format(color, new_fname)
Image.composite(wm, img, wm).save(out_path, 'JPEG', optimize=False, quality=config.QUALITY)
def md5(path):
with open(path, 'rb') as f:
md5hash = hashlib.md5()
for chunk in iter(lambda: f.read(4096), b''):
md5hash.update(chunk)
return md5hash.hexdigest()