Home > Mobile >  Spyder Python error: "TypeError("Image data cannot be converted to float")"
Spyder Python error: "TypeError("Image data cannot be converted to float")"

Time:04-05

i am newbie to python. I am trying to create a Python Program to image dehazing using dcp. I have an image that need to view at console at first and need to do some dehazing method. unfortunately, here i unable to upload or view the image and it saying Image data cannot be converted to float. I am getting the following error when I try running it.

import cv2 
import math 
import numpy as np
import matplotlib.pyplot as plt

def DarkChannel(im,sz):
    b,g,r = cv2.split(img)
    dc = cv2.min(cv2.min(r,g),b)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (sz,sz))
    dark = cv2.erode(dc,kernel)
    return dark
img = cv2.imread("C:/Users/User/Documents/sypder/img/bird.jpg", 1)
plt.imshow(img)

CodePudding user response:

try to read this post : https://www.pythonfixing.com/2021/10/fixed-typeerror-image-data-can-not.html

it's jupiter based article but you can change them accordingly

I'm a newbie too that come across your question but those might be a help

goodluck!

CodePudding user response:

It seems your file path is wrong since your sample code worked perfectly for me. If you are struggling with file paths you can pass it as a raw string.

img = cv2.imread(r"C:\Users\User\Documents\sypder\img\bird.jpg", 1) 

If you fix it like this, it should work. I copied the object name from properties while doing it.

  • Related