我在使用 Python 对多个图像进行增强时遇到了一些问题,它显示了一些错误
I faced some problems to enhance on multiple images using Python, It shows some error
在这里,我想更改图像数据集的默认清晰度。它适用于单个图像,但当我应用于多个图像时,它会显示一个错误,如 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)
您正在尝试使用 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)
在这里,我想更改图像数据集的默认清晰度。它适用于单个图像,但当我应用于多个图像时,它会显示一个错误,如 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)
您正在尝试使用 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)