Advertisement
here2share

# tk_sharpen_image.py

Jul 15th, 2023
1,500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.80 KB | None | 0 0
  1. 1. High-pass filter:
  2. ```
  3. def sharpen_image(img):
  4.     # applying high pass filter
  5.     kernel = [[-1,-1,-1], [-1,9,-1], [-1,-1,-1]]
  6.     sharp_img = img.filter(ImageFilter.Kernel((3,3), kernel))
  7.     return sharp_img
  8. ```
  9.  
  10. 2. Laplacian filter:
  11. ```
  12. def sharpen_image(img):
  13.     # applying Laplacian filter
  14.     kernel = [[0, -1, 0], [-1, 4, -1], [0, -1, 0]]
  15.     sharp_img = img.filter(ImageFilter.Kernel((3,3), kernel))
  16.     return sharp_img
  17. ```
  18.  
  19. 3. Sobel operator:
  20. ```
  21. def sharpen_image(img):
  22.     # applying sobel operator
  23.     kernel_x = [[-1,0,1], [-2,0,2], [-1,0,1]]
  24.     kernel_y = [[-1,-2,-1], [0,0,0], [1,2,1]]
  25.     img_x = img.filter(ImageFilter.Kernel((3,3), kernel_x))
  26.     img_y = img.filter(ImageFilter.Kernel((3,3), kernel_y))
  27.     sharp_img = ImageChops.add(img_x, img_y)
  28.     return sharp_img
  29. ```
  30.  
  31. 4. Unsharp mask filter:
  32. ```
  33. def sharpen_image(img):
  34.     # applying unsharp mask filter
  35.     blurred = img.filter(ImageFilter.BLUR)
  36.     sharp = ImageEnhance.Sharpness(img).enhance(2.0)
  37.     sharp_img = ImageChops.subtract(sharp, blurred)
  38.     return sharp_img
  39. ```
  40.  
  41. 5. Roberts operator:
  42. ```
  43. def sharpen_image(img):
  44.     # applying roberts operator
  45.     kernel_x = [[1,0], [0,-1]]
  46.     kernel_y = [[0,1], [-1,0]]
  47.     img_x = img.filter(ImageFilter.Kernel((2,2), kernel_x))
  48.     img_y = img.filter(ImageFilter.Kernel((2,2), kernel_y))
  49.     sharp_img = ImageChops.add(img_x, img_y)
  50.     return sharp_img
  51. ```
  52.  
  53. 6. Prewitt operator:
  54. ```
  55. def sharpen_image(img):
  56.     # applying prewitt operator
  57.     kernel_x = [[-1,-1,-1], [0,0,0], [1,1,1]]
  58.     kernel_y = [[-1,0,1], [-1,0,1], [-1,0,1]]
  59.     img_x = img.filter(ImageFilter.Kernel((3,3), kernel_x))
  60.     img_y = img.filter(ImageFilter.Kernel((3,3), kernel_y))
  61.     sharp_img = ImageChops.add(img_x, img_y)
  62.     return sharp_img
  63. ```
  64.  
  65. 7. Scharr operator:
  66. ```
  67. def sharpen_image(img):
  68.     # applying scharr operator
  69.     kernel_x = [[3,0,-3], [10,0,-10], [3,0,-3]]
  70.     kernel_y = [[3,10,3], [0,0,0], [-3,-10,-3]]
  71.     img_x = img.filter(ImageFilter.Kernel((3,3), kernel_x))
  72.     img_y = img.filter(ImageFilter.Kernel((3,3), kernel_y))
  73.     sharp_img = ImageChops.add(img_x, img_y)
  74.     return sharp_img
  75. ```
  76.  
  77. 8. Canny edge detection filter:
  78. ```
  79. def sharpen_image(img):
  80.     # applying canny edge detection filter
  81.     edges = img.filter(ImageFilter.FIND_EDGES)
  82.     sharp_img = ImageChops.subtract(img, edges)
  83.     return sharp_img
  84. ```
  85.  
  86. 9. Median filter:
  87. ```
  88. def sharpen_image(img):
  89.     # applying median filter
  90.     sharp_img = img.filter(ImageFilter.MedianFilter(size=3))
  91.     return sharp_img
  92. ```
  93.  
  94. 10. Gaussian weighted average:
  95. ```
  96. def sharpen_image(img):
  97.     # applying gaussian weighted average
  98.     kernel = [[0.04, 0.12, 0.04], [0.12, 0.36, 0.12], [0.04, 0.12, 0.04]]
  99.     sharp_img = img.filter(ImageFilter.Kernel((3,3), kernel))
  100.     return sharp_img
  101. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement