'Sharpness slider in Python with PIL

I am trying to use a slider with an image to change the sharpness of the image. But I am getting an error. Please help me find the mistake and how to rectify it so that it runs properly. I would also appreciate help with making the sliding scale be 0.1 instead of an integer.

import PIL
from PIL import ImageEnhance    
import cv2
a = 0.1
max_a = 10
trackbar_name2 = 'sharpness'
window_name = 'a'

img49 = PIL.ImageEnhance.Sharpness(img49)

def slider(x):
    ret,img_final = PIL.ImageEnhance.Sharpness(img49)
    img_final.enhance(x).show()

slider(0.1)
cv2.createTrackbar(trackbar_name2, window_name, a, max_a, slider, resolution = 0.1)

cv2.destroyAllWindows()

I am getting the following error:

Traceback (most recent call last):

  Input In [97] in <cell line: 7>
    img49 = PIL.ImageEnhance.Sharpness(img49)

  File ~\anaconda3\lib\site-packages\PIL\ImageEnhance.py:100 in __init__
    self.degenerate = image.filter(ImageFilter.SMOOTH)

AttributeError: 'Sharpness' object has no attribute 'filter'

Thank you.



Solution 1:[1]

import cv2 as cv2
import PIL

import os

from PIL import ImageEnhance
from PIL import Image

import matplotlib.pyplot as plt
import numpy as np

os.chdir("C:\A\PIL")

img49 = Image.open("Image.jpg")
a = 1
max_a = 10
trackbar_name2 = 'sharpness'
window_name = 'windowname'

enhance = PIL.ImageEnhance.Sharpness(img49)
img_final = np.uint8(img49)

def slider(x):
    img = Image.fromarray(img_final,'RGB')
    img = enhance.enhance(x)
    img = np.uint8(img)
    cv2.imshow(window_name,img)

slider(1)
cv2.createTrackbar(trackbar_name2, window_name, a, max_a, slider)

cv2.destroyAllWindows()

to get fractions, write

img = enhance.enhance(x/10) 

in the def slider function. This is just a workaround. I still don't know how to do it in the proper way such that it works for all numbers.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rukuto