Skip to content

Commit e7fc236

Browse files
committed
Dithering (D4, D8)
Output 4 gray values and 8 gray values
1 parent 771ac7d commit e7fc236

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed

.DS_Store

0 Bytes
Binary file not shown.

01.Dithering/D4.png

105 KB
Loading

01.Dithering/D8.png

165 KB
Loading

01.Dithering/dithering(4 gray).py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Implement Dithering extend to n=4 gray values
2+
from PIL import Image
3+
import numpy as np
4+
5+
# Input a grayscale image I
6+
I = Image.open("my_cat.png").convert('L')
7+
width, height = I.size
8+
9+
# Step1 : n output and m range Declaration
10+
n = 4
11+
m = int(255/(n-1))
12+
13+
# Step2 : Define and Calculate Q
14+
column = height
15+
row = width
16+
# Define Q (2D Array)
17+
Q = [[0 for _ in range(row)] for _ in range(column)]
18+
# Calculate Q(i,j) value
19+
for j in range(I.size[1]): # j = column
20+
for i in range(I.size[0]): # i = row
21+
Q[j][i] = int(I.getpixel((i, j))/m) # Q(i,j) = [I(i,j)/85]
22+
23+
# Step3 :Define dithering matrix(D1) and generate dithering array(D)
24+
# Define Dithering Matrix (2X2)
25+
D1 = np.array([[0, 56],
26+
[84, 28]])
27+
28+
# Generate Dithering Array D of image size by repeating D1
29+
D = np.tile(D1, (int(width/2), int(height/2)))
30+
31+
# Step4 : Threshold image I
32+
for j in range(I.size[1]): # j = column
33+
for i in range(I.size[0]): # i = row
34+
35+
ori_pixel = I.getpixel((i, j))
36+
37+
if ori_pixel - m * Q[j][i] > D[j][i]:
38+
I.putpixel((i, j), m * (Q[j][i] + 1))
39+
else:
40+
I.putpixel((i, j), m * (Q[j][i] + 0))
41+
42+
# Save new image I'
43+
I.save("D4.png")
44+
45+
# Step5 : Display images I and I'(dithering_img)
46+
I = Image.open("my_cat.png").convert('L')
47+
I.show()
48+
dithering_img = Image.open("D4.png")
49+
dithering_img.show()
50+

01.Dithering/dithering(8 gray).py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Implement Dithering extend to n=8 gray values
2+
from PIL import Image
3+
import numpy as np
4+
5+
# Input a grayscale image I
6+
I = Image.open("my_cat.png").convert('L')
7+
width, height = I.size
8+
9+
# Step1 : n output and m range Declaration
10+
n = 8
11+
m = int(255/(n-1))
12+
13+
# Step2 : Define and Calculate Q
14+
column = height
15+
row = width
16+
# Define Q (2D Array)
17+
Q = [[0 for _ in range(row)] for _ in range(column)]
18+
# Calculate Q(i,j) value
19+
for j in range(I.size[1]): # j = column
20+
for i in range(I.size[0]): # i = row
21+
Q[j][i] = int(I.getpixel((i, j))/m) # Q(i,j) = [I(i,j)/85]
22+
23+
# Step3 :Define dithering matrix(D1) and generate dithering array(D)
24+
# Define Dithering Matrix (2X2)
25+
D1 = np.array([[0, 24],
26+
[36, 12]])
27+
28+
# Generate Dithering Array D of image size by repeating D1
29+
D = np.tile(D1, (int(width/2), int(height/2)))
30+
31+
# Step4 : Threshold image I
32+
for j in range(I.size[1]): # j = column
33+
for i in range(I.size[0]): # i = row
34+
35+
ori_pixel = I.getpixel((i, j))
36+
37+
if ori_pixel - m * Q[j][i] > D[j][i]:
38+
I.putpixel((i, j), m * (Q[j][i] + 1))
39+
else:
40+
I.putpixel((i, j), m * (Q[j][i] + 0))
41+
42+
# Save new image I'
43+
I.save("D8.png")
44+
45+
# Step5 : Display images I and I'(dithering_img)
46+
I = Image.open("my_cat.png").convert('L')
47+
I.show()
48+
dithering_img = Image.open("D8.png")
49+
dithering_img.show()
50+

0 commit comments

Comments
 (0)