0 votes
1 view
in Data Science by (10.1k points)

I am working on an image dataset and I am trying to change the default sharpness. When I try to change a single image it works fine but I try on multiple images it is not working fine. I am getting error as shown below:

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

How do I solve this? This is the code which I am using:

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)

1 Answer

0 votes
by (20.4k points)

As per my knowledge, you're using the PIL to enhance a NumPy array.cv2 which converts images path into NumPy array. This process will not work using the PIL image operations.

Load the image using the PIL command then enhance the image using PIL enhancement then convert into NumPy array so that you can pass it to cv2 using resize() method.

Code to do it:

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)

If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...