0

I applied Image deconvolution using an unsupervised wiener algorithm and increased the sharpness and contrast on a specific dataset. But I faced an error while compiling the code. It shows AttributeError: 'numpy.ndarray' object has no attribute 'convert'. How to fix it? My code is given below -

import cv2
import glob
from matplotlib import pyplot as plt
from skimage import io, color, restoration, img_as_float
import scipy.stats as st
import numpy as np
from PIL import Image
from PIL import ImageEnhance

all_img = glob.glob('input/*.png')
other_dir = 'output/'

for img_id, img_path in enumerate(all_img):
    img = img_as_float(io.imread(img_path,0))

    def gkern(kernlen=21, nsig=2):   
        lim = kernlen//2 + (kernlen % 2)/2
        x = np.linspace(-lim, lim, kernlen+1)
        kern1d = np.diff(st.norm.cdf(x))
        kern2d = np.outer(kern1d, kern1d)
        return kern2d/kern2d.sum()

    psf = gkern(5,3)   

    deconvolved, _ = restoration.unsupervised_wiener(img, pdf)

    # Applied Sharpness and contrast
    enhancer_object = ImageEnhance.Contrast(deconvolved)
    out = enhancer_object.enhance(1.4)
    enhancer = ImageEnhance.Sharpness(out)
    enhanced_im = enhancer.enhance(8.0)  
    enhanced_cv_im = np.array(enhanced_im)

    cl2 = cv2.resize(enhanced_cv_im, (512,512), interpolation = cv2.INTER_CUBIC)
    plt.imsave(f"output/unsupervised_wiener_{img_id}.png", cl2, cmap='gray')

It shows the error-

runfile('C:/Users/Junaed/.spyder-py3/unsupervised_wiener.py', wdir='C:/Users/Junaed/.spyder-py3')
Traceback (most recent call last):

  File "C:\Users\Junaed\.spyder-py3\unsupervised_wiener.py", line 37, in <module>
    enhancer_object = ImageEnhance.Contrast(deconvolved)

AttributeError: 'numpy.ndarray' object has no attribute 'convert'

11
  • 1
    can you post the whole error? Commented May 6, 2020 at 18:31
  • 1
    When posting a question about code that produces an Exception, always include the complete Traceback - copy and paste it then format it as code (select it and type ctrl-k) Commented May 6, 2020 at 18:37
  • 2
    from the error message I suspect that you used a numpy array where an image object was expected. But without the whole traceback this is just speculation. Please add the whole output. Commented May 6, 2020 at 18:37
  • How to fix it? Instead of passing a numpy.ndarray for the argument, pass the object type that the method/function expects. Commented May 6, 2020 at 18:38
  • Is the indentation in your post correct? Is gkern iteratively defined in the for loop? Commented May 6, 2020 at 18:41

1 Answer 1

3

ImageEnhance.Contrast() is expecting a PIL image which it can run image.convert on but you passed it a numpy array. To convert to PIL you can do this

from PIL import Image
import numpy 

im = Image.fromarray(numpy.uint8(deconvolved))
Sign up to request clarification or add additional context in comments.

6 Comments

i add this, it will shows a message that name 'PIL' is not defined. But i already added this.
deconvolved, _ = restoration.unsupervised_wiener(img, psf) im = PIL.Image.fromarray(numpy.uint8(deconvolved)) enhancer_object = ImageEnhance.Contrast(im) out = enhancer_object.enhance(1.4) enhancer = ImageEnhance.Sharpness(out) enhanced_im = enhancer.enhance(8.0) enhanced_cv_im = np.array(enhanced_im) After edit my code part
You need to import PIL for use by pip installing Pillow then you can import PIL
I already used that pillow library before post the question. I use like this - from PIL import Image from PIL import ImageEnhance
Okay then just import Image as well from PIL and use Image.fromarray() instead
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.