“Cannot perform reduce with flexible type” in Image Processing

回眸只為那壹抹淺笑 提交于 2020-07-23 08:27:26

问题


I have a problem. The main task is to create a bot in a telegram, which, when a photo is loaded into it, will change the contrast of the image. The main condition is the addition of the so-called black and white dots. Their principle is to search for the lightest point, the darkest and maximum values at these points.Just the code (working, checking) looks something like this (used numpy and PIL).

from PIL import Image
import numpy as np

image = Image.open('example1.jpg')
arr = np.asarray(image)

rgbmax = np.max(arr)
rgbmin = np.min(arr)

rgbmax = 240
rgbmin = 15

R, G, B = image.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)

resultimg = Image.merge("RGB", (rout, gout, bout))
resultimg.save("result_img.jpg")

This part is required. We must use this method. So, if i use that in my code, i get this error.

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(image_name)

    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()

Error and traceback

2020-05-23 13:03:37,571 (util.py:68 WorkerThread1) ERROR - TeleBot: "TypeError occurred, args=('cannot perform reduce with flexible type',)
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 74, in handle_image
    rgbmax = np.max(arr)
  File "<__array_function__ internals>", line 6, in amax
  File "E:\Anaconda\lib\site-packages\numpy\core\fromnumeric.py", line 2668, in amax
    keepdims=keepdims, initial=initial, where=where)
  File "E:\Anaconda\lib\site-packages\numpy\core\fromnumeric.py", line 90, in _wrapreduction
    return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
TypeError: cannot perform reduce with flexible type
"
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 74, in handle_image
    rgbmax = np.max(arr)
  File "<__array_function__ internals>", line 6, in amax
  File "E:\Anaconda\lib\site-packages\numpy\core\fromnumeric.py", line 2668, in amax
    keepdims=keepdims, initial=initial, where=where)
  File "E:\Anaconda\lib\site-packages\numpy\core\fromnumeric.py", line 90, in _wrapreduction
    return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
TypeError: cannot perform reduce with flexible type

Maybe I'm doing something wrong? The fact is that this is the first task in python, and along the way I'm trying to figure it out. I would be very grateful if you were prompted how to solve this problem, or rather get rid of the error and make it work.

来源:https://stackoverflow.com/questions/61970403/cannot-perform-reduce-with-flexible-type-in-image-processing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!