修复错误 'numpy.ndarray' object has no attribute 'convert'
To fix an error 'numpy.ndarray' object has no attribute 'convert'
我使用无监督维纳算法应用图像反卷积,并增加了特定数据集的清晰度和对比度。但是我在编译代码时遇到了错误。它显示 AttributeError: 'numpy.ndarray' object has no attribute 'convert'. 如何解决?我的代码如下 -
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')
显示错误-
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'
ImageEnhance.Contrast() 期待一个 PIL 图像,它可以 运行 image.convert 但你传递给它一个 numpy 数组。要转换为 PIL,您可以这样做
from PIL import Image
import numpy
im = Image.fromarray(numpy.uint8(deconvolved))
我使用无监督维纳算法应用图像反卷积,并增加了特定数据集的清晰度和对比度。但是我在编译代码时遇到了错误。它显示 AttributeError: 'numpy.ndarray' object has no attribute 'convert'. 如何解决?我的代码如下 -
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')
显示错误-
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'
ImageEnhance.Contrast() 期待一个 PIL 图像,它可以 运行 image.convert 但你传递给它一个 numpy 数组。要转换为 PIL,您可以这样做
from PIL import Image
import numpy
im = Image.fromarray(numpy.uint8(deconvolved))