Skip to content

Commit 6e0ccb2

Browse files
authored
Add files via upload
1 parent fe981e5 commit 6e0ccb2

File tree

5 files changed

+155
-23
lines changed

5 files changed

+155
-23
lines changed

script/bayer_dithering.py

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def process_dither_color(img, matrix):
5454
else:
5555
v = 0
5656

57-
img[row][col] = v
57+
img[row][col] = v
5858

5959
def dither_color_image(image, matrix):
6060
blue=image[:,:,0] #taking the blue channel
@@ -63,4 +63,70 @@ def dither_color_image(image, matrix):
6363
process_dither_color(green, matrix)
6464
red=image[:,:,2]
6565
process_dither_color(red, matrix)
66-
return cv2.merge((blue, green, red))
66+
return cv2.merge((blue, green, red))
67+
68+
def detect_board_color(img, y, x, allow_value, mode, select_color_value=None):
69+
height = img.shape[0]
70+
width = img.shape[1]
71+
self_color = img[y][x]
72+
if select_color_value:
73+
if self_color >= select_color_value[0] and self_color <= select_color_value[1]:
74+
pass
75+
else:
76+
return False
77+
if mode == 0:
78+
x_dir = [1, 1, 1, 1, -1, -1, -1, -1]
79+
y_dir = [1, -1, 1, -1, 1, -1, 1, -1]
80+
for i in range(8):
81+
new_y = y + y_dir[i]
82+
new_x = x + x_dir[i]
83+
new_y = clamp(new_y, height-1, 0)
84+
new_x = clamp(new_x, width-1, 0)
85+
new_color = img[new_y][new_x]
86+
if self_color >= new_color+allow_value or self_color <= new_color-allow_value:
87+
return True
88+
return False
89+
elif mode == 1:
90+
x_dir = [1, 1, -1, -1]
91+
y_dir = [1, -1, 1, -1]
92+
for i in range(4):
93+
new_y = y + y_dir[i]
94+
new_x = x + x_dir[i]
95+
new_y = clamp(new_y, height-1, 0)
96+
new_x = clamp(new_x, width-1, 0)
97+
new_color = img[new_y][new_x]
98+
if self_color >= new_color+allow_value or self_color <= new_color-allow_value:
99+
return True
100+
return False
101+
102+
def clamp(value, max_num, min_num):
103+
value = max(value, min_num)
104+
value = min(value, max_num)
105+
# value = value % max_num
106+
return value
107+
108+
def process_dither_color_fixed_8_dir(img, matrix, allow_value, mode, select_color_value):
109+
or_img = img.copy()
110+
height = img.shape[0]
111+
width = img.shape[1]
112+
matrix_len = len(matrix) - 1
113+
for row in range(height):
114+
for col in range(width):
115+
if detect_board_color(or_img, row, col, allow_value, mode, select_color_value):
116+
color = img[row][col]
117+
118+
if color >= (color>>2) > matrix[row&matrix_len][col&matrix_len]:
119+
v = 255
120+
else:
121+
v = 0
122+
123+
img[row][col] = v
124+
125+
def dither_color_image_fix(image, matrix, allow_value, mode, select_color_value=None):
126+
blue=image[:,:,0] #taking the blue channel
127+
process_dither_color_fixed_8_dir(blue, matrix, allow_value, mode, select_color_value)
128+
green=image[:,:,1]
129+
process_dither_color_fixed_8_dir(green, matrix, allow_value, mode, select_color_value)
130+
red=image[:,:,2]
131+
process_dither_color_fixed_8_dir(red, matrix, allow_value, mode, select_color_value)
132+
return cv2.merge((blue, green, red))

script/contrast_and_saturation.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import cv2
2+
import numpy
3+
import math
4+
5+
def contrast_and_brightness(img, brightness=0 , contrast=100):
6+
B = brightness / 255.0
7+
c = contrast / 255.0
8+
k = math.tan((45 + 44 * c) / 180 * math.pi)
9+
10+
img = (img - 127.5 * (1 - B)) * k + 127.5 * (1 + B)
11+
12+
# values between 0-255
13+
img = numpy.clip(img, 0, 255).astype(numpy.uint8)
14+
return img
15+
16+
def saturation_and_lightness(img, lightness, saturation):
17+
18+
# origin_img = img
19+
20+
# astype image and turn to float
21+
fImg = img.astype(numpy.float32)
22+
fImg = fImg / 255.0
23+
24+
# color space exchange BGR -> HLS
25+
hlsImg = cv2.cvtColor(fImg, cv2.COLOR_BGR2HLS)
26+
hlsCopy = numpy.copy(hlsImg)
27+
28+
# lightness = 0 # lightness edited to "1 +/- n %"
29+
# saturation = 300 # saturation edited to "1 +/- n %"
30+
31+
# brightness
32+
hlsCopy[:, :, 1] = (1 + lightness / 100.0) * hlsCopy[:, :, 1]
33+
hlsCopy[:, :, 1][hlsCopy[:, :, 1] > 1] = 1 # Between 0-1
34+
35+
# saturation
36+
hlsCopy[:, :, 2] = (1 + saturation / 100.0) * hlsCopy[:, :, 2]
37+
hlsCopy[:, :, 2][hlsCopy[:, :, 2] > 1] = 1 # Between 0-1
38+
39+
# color space exchange back HLS -> BGR
40+
result_img = cv2.cvtColor(hlsCopy, cv2.COLOR_HLS2BGR)
41+
result_img = ((result_img * 255).astype(numpy.uint8))
42+
return result_img

script/display_image.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ def display_screen_resize(self, width=10, height=10, mode=0):
2121
elif mode == 1:
2222
self.display_screen = pygame.display.set_mode(self.image.get_size())
2323

24-
2524
def cv_to_pygame(self, cv_img):
2625
frame = cv2.cvtColor(cv_img, cv2.COLOR_BGR2RGB)
2726
frame = cv2.transpose(frame)

script/gui_helper.py

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,74 +14,99 @@ def __init__(self, display):
1414
self.image_path = resource_path(sample_image_path)
1515
self.cv_image = cv2.imread(self.image_path)
1616

17-
self.root.title('pixel art styler')
18-
self.root.geometry('300x300')
17+
self.root.title('pixel art styler像素風格濾鏡工具')
18+
self.root.geometry('500x300')
1919

2020
self.load_file_name = StringVar()
21-
self.load_file_name.set('no file')
21+
self.load_file_name.set('no file無檔案')
2222
self.now_file_name = Label(self.root,textvariable=self.load_file_name)
2323
self.now_file_name.grid(column=0, row=0, sticky=N+W)
2424

25-
self.import_img_btn = Button(self.root, text='import img', command=self.import_img)
25+
self.import_img_btn = Button(self.root, text='import img導入圖片', command=self.import_img)
2626
self.import_img_btn.grid(column=4, row=0, sticky=E+N)
2727

2828
# color num
29-
self.color_num_name = Label(self.root,text='Color nums')
29+
self.color_num_name = Label(self.root,text='Color nums色數')
3030
self.color_num_name.grid(column=0, row=1, sticky=N+W)
3131
self.color_num_select_box = Combobox(self.root, width=5)
3232
self.color_num_select_box.grid(column=0, row=2, sticky=N+W)
3333
self.color_num_select_box['values'] = ['2', '4', '8', '16']
3434
self.color_num_select_box.current(0)
3535
# pixel size
36-
self.pixel_size_name = Label(self.root,text='Pixel size')
37-
self.pixel_size_name.grid(column=4, row=1, sticky=N+W)
36+
self.pixel_size_name = Label(self.root,text='Pixel size像素尺寸')
37+
self.pixel_size_name.grid(column=4, row=1, sticky=N+E)
3838
self.pixel_size_select_box = Combobox(self.root, width=5)
39-
self.pixel_size_select_box.grid(column=4, row=2, sticky=N+W)
39+
self.pixel_size_select_box.grid(column=4, row=2, sticky=N+E)
4040
self.pixel_size_select_box['values'] = ['1', '2', '3', '4']
4141
self.pixel_size_select_box.current(0)
4242
# smoothing
43-
self.smoothing_name = Label(self.root,text='Smoothing')
43+
self.smoothing_name = Label(self.root,text='Smoothing光滑')
4444
self.smoothing_name.grid(column=0, row=3, sticky=N+W)
4545
self.smoothing_select_box = Combobox(self.root, width=5)
4646
self.smoothing_select_box.grid(column=0, row=4, sticky=N+W)
4747
self.smoothing_select_box['values'] = ['None', 'Less', 'Great']
4848
self.smoothing_select_box.current(0)
4949
# outlines
50-
self.outline_name = Label(self.root,text='Outline inflate')
51-
self.outline_name.grid(column=4, row=3, sticky=N+W)
50+
self.outline_name = Label(self.root,text='Outline輪廓')
51+
self.outline_name.grid(column=4, row=3, sticky=N+E)
5252
self.outline_select_box = Combobox(self.root, width=5)
53-
self.outline_select_box.grid(column=4, row=4, sticky=N+W)
53+
self.outline_select_box.grid(column=4, row=4, sticky=N+E)
5454
self.outline_select_box['values'] = ['None', 'Less', 'Great']
5555
self.outline_select_box.current(0)
56+
# saturation
57+
self.saturation_name = Label(self.root,text='Saturation飽和')
58+
self.saturation_name.grid(column=0, row=5, sticky=N+W)
59+
self.saturation_select_box = Combobox(self.root, width=5)
60+
self.saturation_select_box.grid(column=0, row=6, sticky=N+W)
61+
self.saturation_select_box['values'] = ['-200', '-100', '0', '100', '200']
62+
self.saturation_select_box.current(2)
63+
# contrast
64+
self.contrast_name = Label(self.root,text='Contrast對比')
65+
self.contrast_name.grid(column=4, row=5, sticky=N+E)
66+
self.contrast_select_box = Combobox(self.root, width=5)
67+
self.contrast_select_box.grid(column=4, row=6, sticky=N+E)
68+
self.contrast_select_box['values'] = ['-200', '-100', '0', '100', '200']
69+
self.contrast_select_box.current(2)
5670

5771
# dithering
5872
self.dithering_bool = BooleanVar()
59-
self.dithering_check = Checkbutton(self.root, text='Dithering', variable=self.dithering_bool)
73+
self.dithering_check = Checkbutton(self.root, text='Dithering顆粒抖動', variable=self.dithering_bool)
6074
self.dithering_check.grid(column=0, row=7, sticky=N+W)
6175

6276
# transform button
63-
self.transform_img_btn = Button(self.root, text='transform', command=self.transform_img)
77+
self.transform_img_btn = Button(self.root, text='transform變換', command=self.transform_img)
6478
self.transform_img_btn.grid(column=0, row=8, sticky=W+N)
6579
# save_img button
66-
self.save_img_btn = Button(self.root, text='save', command=self.save_img)
80+
self.save_img_btn = Button(self.root, text='save保存', command=self.save_img)
6781
self.save_img_btn.grid(column=0, row=9, sticky=W+N)
6882

6983
def import_img(self):
7084
image_path = filedialog.askopenfilename()
7185
if image_path:
7286
file_name = re.split('/|\.', image_path)[-2]
73-
print(image_path)
87+
# print(image_path)
7488
self.load_file_name.set(file_name)
7589
self.display.image_load(image_path)
7690
self.image_path = image_path
91+
self.cv_image = cv2.imread(self.image_path)
7792

7893
def transform_img(self):
7994
k = int(self.color_num_select_box.get())
8095
scale = int(self.pixel_size_select_box.get())
8196
blur = self.get_textbox_value(self.smoothing_select_box.get())
8297
erode = self.get_textbox_value(self.outline_select_box.get())
8398
dither = self.dithering_bool.get()
84-
self.cv_image = convert(self.image_path, k=k, scale=scale, blur=blur, erode=erode, dither=dither)
99+
saturation = int(self.saturation_select_box.get())
100+
contrast = int(self.contrast_select_box.get())
101+
102+
self.cv_image = convert(self.image_path,
103+
k=k,
104+
scale=scale,
105+
blur=blur,
106+
erode=erode,
107+
dither=dither,
108+
saturation=saturation,
109+
contrast=contrast)
85110
self.display.cv_to_pygame(self.cv_image)
86111

87112
def get_textbox_value(self, text):

script/pixel_converter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
from pixel_transform import transform
55
from settings import resource_path
66

7-
def convert(img_path, k=16, scale=1, blur=0, erode=0,alpha = True, to_tw = False, dither=False):
7+
def convert(img_path, k=16, scale=1, blur=0, erode=0,alpha = True, to_tw = False, dither=False, saturation=0, contrast=0):
88
# k is color num
99
# scale is pixel size
1010
# to_tw means to twitter
1111
img_path.replace("\\", "/")
1212
img_file_name = re.split("/", img_path)[-1]
13-
img_res = transform(img_path, k=k, scale=scale, blur=blur, erode=erode, alpha=alpha, to_tw=to_tw, dither=dither)
13+
img_res = transform(img_path, k=k, scale=scale, blur=blur, erode=erode, alpha=alpha, to_tw=to_tw, dither=dither, saturation=saturation, contrast=contrast)
1414
img_path = img_path.split(img_file_name)[0]
1515
return img_res
1616

1717
def save_cv_img(img, img_path, img_name):
1818
result_path = os.path.join(img_path + img_name + '.png')
19-
cv2.imwrite(result_path, img, [int(cv2.IMWRITE_PNG_COMPRESSION)])
19+
cv2.imwrite(result_path, img, [cv2.IMWRITE_PNG_COMPRESSION, 9])

0 commit comments

Comments
 (0)