Skip to content

Instantly share code, notes, and snippets.

@Rachel030219
Created December 29, 2023 03:02
Show Gist options
  • Save Rachel030219/855bfad100f637993d72ae7a46f3364a to your computer and use it in GitHub Desktop.
Save Rachel030219/855bfad100f637993d72ae7a46f3364a to your computer and use it in GitHub Desktop.
A Telegram bot used to draw CIE 1931 Chromaticity diagram out of image files.
import os
import telebot
import config
import picture_to_cie_diagram
from telebot import apihelper
from urllib3 import ProxyManager
bot = telebot.TeleBot('<TOKEN>')
def draw_diagram(message, file_id):
if message.chat.type != 'private':
bot.reply_to(message, 'This bot should not be accessed from outside of private chat. Nothing will be done.')
return
bot.send_chat_action(message.chat.id, 'upload_document')
file = bot.get_file(file_id)
response = urllib3.request('GET', f'https://api.telegram.org/file/bot<TOKEN>/{file.file_path}')
path = os.path.split(file.file_path)[1]
with open(f'{path}', 'w+b') as temp_file:
temp_file.write(response.data)
xy_array = picture_to_cie_diagram.image_to_cie_xy(path)
output_path = os.path.splitext(path)[0] + '_diagram.jpg'
picture_to_cie_diagram.plot_xy_coordinates_with_color(xy_array, output_path)
bot.send_document(message.chat.id, telebot.types.InputFile(output_path))
os.remove(path)
os.remove(output_path)
@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
bot.reply_to(message, """This bot is used to generate CIE 1931 Chromaticity diagram from image files.\n
To use this bot, send an image file to this bot and it will reply with the generated diagram.\n
Only the first image is processed, so make sure to send only one each time.""")
@bot.message_handler(content_types=['photo'])
def send_image_from_image(message):
bot.reply_to(message, 'Sending images as photos will RUIN THE RESULTS, and should only be used for testing.')
image_info = message.photo[len(message.photo) - 1]
draw_diagram(message, image_info.file_id)
@bot.message_handler(content_types=['document'])
def send_image_from_document(message):
document = message.document
draw_diagram(message, document.file_id)
bot.infinity_polling()
@Rachel030219
Copy link
Author

You should use this along with picture_to_cie_diagram.py.

Put them in the same folder, fill in bot token, set up proxy (optional) and you are ready to go.

IMAGE 2023-12-29 11:03:43

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment