【问题标题】:I faced some problems to enhance on multiple images using Python, It shows some error我在使用 Python 增强多个图像时遇到了一些问题,它显示了一些错误
【发布时间】:2020-04-28 10:08:15
【问题描述】:

在这里,我想更改图像数据集的默认锐度。它适用于单个图像,但是当我应用于多个图像时,它会显示一个错误,例如 AttributeError: 'numpy.ndarray' object has no attribute 'filter'。我应该怎么做才能解决这个问题?为此,我的代码如下-

from PIL import Image
from PIL import ImageEnhance
import cv2
import glob

dataset = glob.glob('input/*.png')
other_dir = 'output/'
for img_id, img_path in enumerate(dataset):
    img = cv2.imread(img_path,0)

    enhancer = ImageEnhance.Sharpness(img)
    enhanced_im = enhancer.enhance(8.0)

    cl2 = cv2.resize(enhanced_im, (1024,1024), interpolation = cv2.INTER_CUBIC)
    cv2.imwrite(f'{other_dir}/enhanced_{img_id}.png',cl2)

【问题讨论】:

    标签: python spyder image-preprocessing


    【解决方案1】:

    您正在尝试使用 PIL 来增强 numpy 数组。 cv2 将图像从图像路径转换为 ​​numpy 数组。这不适用于PIL 图像操作。

    您可以使用PIL 加载图像,执行PIL 增强功能,然后将其转换为numpy 数组以传递给您的cv2.resize() 方法。

    试试:

    from PIL import Image
    from PIL import ImageEnhance
    import cv2
    import glob
    import numpy as np
    
    dataset = glob.glob('input/*.png')
    other_dir = 'output/'
    for img_id, img_path in enumerate(dataset):
        img = Image.open(img_path)  # this is a PIL image
    
        enhancer = ImageEnhance.Sharpness(img)  # PIL wants its own image format here
        enhanced_im = enhancer.enhance(8.0)  # and here
        enhanced_cv_im = np.array(enhanced_im) # cv2 wants a numpy array
    
        cl2 = cv2.resize(enhanced_cv_im, (1024,1024), interpolation = cv2.INTER_CUBIC)
        cv2.imwrite(f'{other_dir}/enhanced_{img_id}.png',cl2)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-19
      • 1970-01-01
      • 2021-11-18
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多