-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogTransformation.py
57 lines (31 loc) · 1.25 KB
/
logTransformation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# import Pillow modules
import math
from PIL import Image
from PIL import ImageFilter
#import matplotlib.pyplot as plt
# Compute log
def logTransform(c, f):
g = c * math.log(float(1 + f), 10)
return g
# Apply logarithmic transformation for an image
def logTransformImage(image, outputMax=255, inputMax=255):
c = outputMax / math.log(inputMax + 1, 10)
# Read pixels and apply logarithmic transformation
for i in range(0, img.size[0] - 1):
for j in range(0, img.size[1] - 1):
# Get pixel value at (x,y) position of the image
f = img.getpixel((i, j))
# Do log transformation of the pixel
redPixel = round(logTransform(c, f[0]))
greenPixel = round(logTransform(c, f[1]))
bluePixel = round(logTransform(c, f[2]))
# Modify the image with the transformed pixel values
img.putpixel((i, j), (redPixel, greenPixel, bluePixel))
return image
# Display the original image
imageFileName = "forest.jpg"
img = Image.open(imageFileName)
img.show()
# Display the image after applying the logarithmic transformation
logTransformedImage = logTransformImage(img)
logTransformedImage.show()