问题
The main problem was described by the post earlier. This is a sequel with full code and traceback. I have a certain error while processing photos using the telegram bot. In particular, this is a problem with a change in contrast. Full code:
import telebot
import os
import urllib.request
from PIL import Image
import numpy as np
TOKEN = 'here token is, just cut it out'
bot = telebot.TeleBot(TOKEN)
result_storage_path = 'temp'
@bot.message_handler(commands=['start'])
def start_message(message):
bot.send_message(message.chat.id, 'Hello. I`m bot for processing your images. 🎆')
bot.send_message(message.chat.id, 'Please, send me number to continue.')
@bot.message_handler(content_types=['text'])
def handle_text(message):
cid = message.chat.id
if message.text.isdigit():
number = int(message.text)
bot.send_message(message.chat.id,
'You have sent number = ' + str(number) + '.')
else:
bot.send_message(message.chat.id, 'Input isn`t correct. Please, send number.')
@bot.message_handler(content_types=['photo'])
def handle_photo(message):
cid = message.chat.id
image_name = save_image_from_message(message)
bot.send_message(cid, 'I save your photo.')
image_name_new = handle_image(image_name)
bot.send_photo(message.chat.id, open('{0}/{1}'.format(result_storage_path, image_name_new), 'rb'), 'Done!')
bot.send_message(message.chat.id, 'Want to try again?')
cleanup_remove_images(image_name, image_name_new)
def save_image_from_message(message):
image_id = get_image_id_from_message(message)
file_path = bot.get_file(image_id).file_path
image_url = "https://api.telegram.org/file/bot{0}/{1}".format(TOKEN, file_path)
if not os.path.exists(result_storage_path):
os.makedirs(result_storage_path)
image_name = "{0}.jpg".format(image_id)
urllib.request.urlretrieve(image_url, "{0}/{1}".format(result_storage_path, image_name))
return image_name
def get_image_id_from_message(message):
return message.photo[len(message.photo) - 1].file_id
def handle_image(image_name):
content_image = Image.open("{0}/{1}".format(result_storage_path, image_name))
arr = np.asarray(content_image)
rgbmax = np.max(arr)
rgbmin = np.min(arr)
rgbmax = 240
rgbmin = 15
R, G, B = image_name.split()
rout = R.point(lambda i: (i - rgbmin) / (rgbmax - rgbmin) * 255)
gout = G.point(lambda i: (i - rgbmin) / (rgbmax - rgbmin) * 255)
bout = B.point(lambda i: (i - rgbmin) / (rgbmax - rgbmin) * 255)
result_img_pillow = Image.merge("RGB", (rout, gout, bout))
image_name_new = "handled_image_" + image_name
result_img_pillow.save("{0}/{1}".format(result_storage_path, image_name_new))
return image_name_new
def cleanup_remove_images(image_name, image_name_new):
os.remove('{0}/{1}'.format(result_storage_path, image_name))
os.remove('{0}/{1}'.format(result_storage_path, image_name_new))
bot.polling()
And this error and traceback. I will be very grateful if this problem is solved, since python itself is new to me, these are study assignments. This error occurs when a photo is loaded in the bot itself.
2020-05-23 16:11:58,514 (util.py:68 WorkerThread1) ERROR - TeleBot: "ValueError occurred, args=('not enough values to unpack (expected 3, got 1)',)
Traceback (most recent call last):
File "E:\Anaconda\lib\site-packages\telebot\util.py", line 62, in run
task(*args, **kwargs)
File "E:/ХНУРЭ/ЦОЗ/imageproc/bot.py", line 42, in handle_photo
image_name_new = handle_image(image_name)
File "E:/ХНУРЭ/ЦОЗ/imageproc/bot.py", line 80, in handle_image
R, G, B = image_name.split()
ValueError: not enough values to unpack (expected 3, got 1)
"
Traceback (most recent call last):
File "E:/ХНУРЭ/ЦОЗ/imageproc/bot.py", line 119, in <module>
bot.polling()
File "E:\Anaconda\lib\site-packages\telebot\__init__.py", line 415, in polling
self.__threaded_polling(none_stop, interval, timeout)
File "E:\Anaconda\lib\site-packages\telebot\__init__.py", line 439, in __threaded_polling
self.worker_pool.raise_exceptions()
File "E:\Anaconda\lib\site-packages\telebot\util.py", line 111, in raise_exceptions
six.reraise(self.exc_info[0], self.exc_info[1], self.exc_info[2])
File "E:\Anaconda\lib\site-packages\six.py", line 703, in reraise
raise value
File "E:\Anaconda\lib\site-packages\telebot\util.py", line 62, in run
task(*args, **kwargs)
File "E:/ХНУРЭ/ЦОЗ/imageproc/bot.py", line 42, in handle_photo
image_name_new = handle_image(image_name)
File "E:/ХНУРЭ/ЦОЗ/imageproc/bot.py", line 80, in handle_image
R, G, B = image_name.split()
ValueError: not enough values to unpack (expected 3, got 1)
来源:https://stackoverflow.com/questions/61972578/image-processing-error-not-enough-values-to-unpack