Skip to content
Snippets Groups Projects
Commit 451d79c3 authored by Marc Feger's avatar Marc Feger
Browse files

Add first version

parents
No related branches found
No related tags found
No related merge requests found
Showing
with 214 additions and 0 deletions
*.idea/
*.xml
__pycache__/
.DS_Store
\ No newline at end of file
GUI.py 0 → 100644
from tkinter import Tk, Label, Button, StringVar, OptionMenu
from tkinter.filedialog import askopenfilename
from tokenize import String
from PIL import ImageTk, Image
from editor import *
from filter import *
class GUI(object):
def __init__(self, master):
self.master = master
self.master.title('Filter')
self.image_label_width = 500
self.image_label_height = 500
self.choices = {FILTER.box.value,
FILTER.gaussian.value,
FILTER.laplacian.value,
FILTER.mod_laplacian.value,
FILTER.median.value,
FILTER.min.value,
FILTER.max.value}
self.filter = StringVar(root)
self.filter.set(FILTER.empty.value)
self.filter.trace('w', self.change_filter)
# Images
self.image_path = './images/white-plane-sky.jpeg'
self.edit_path = './images/edit.jpeg'
self.image = self.read_image(self.image_path)
# Labels
self.original_image_label = Label(self.master, image=self.image)
self.original_image_label.pack()
self.filtered_image_label = Label(self.master, image=self.image)
self.filtered_image_label.pack()
# Buttons
self.file_dialog = Button(master, text='Choose', command=lambda: self.set_new_file())
self.option_menu = OptionMenu(self.master, self.filter, *self.choices)
# Layout
self.file_dialog.grid(row=0, column=0)
self.option_menu.grid(row=0, column=1)
self.original_image_label.grid(row=1, column=0)
self.filtered_image_label.grid(row=1, column=1)
def read_image(self, path: String):
return ImageTk.PhotoImage(
Image.open(path).resize(
(self.image_label_width, self.image_label_height),
Image.ANTIALIAS
)
)
def _update_label_image(self, label: Label, new_image, row: int, column: int):
label.destroy()
label = Label(self.master, image=new_image)
label.image = new_image
label.pack()
label.grid(row=row, column=column)
def set_new_file(self):
# Todo: Specify more formats
self.image_path: String = askopenfilename(initialdir='/', title='Select file',
filetypes=(('jpeg files', '*.jpeg'), ('all files', '*.*')))
new_image = self.read_image(self.image_path)
self._update_label_image(self.original_image_label, new_image, 1, 0)
self.filter.set(FILTER.empty.value)
self._update_label_image(self.filtered_image_label, new_image, 1, 1)
def change_filter(self, *args):
self.edit_image()
edited_image = self.read_image(self.edit_path)
self._update_label_image(self.filtered_image_label, edited_image, 1, 1)
def edit_image(self):
print('From: ' + self.image_path + ' to ' + self.edit_path + ' with ' + self.filter.get())
Editor(self.image_path, self.edit_path).do_convolution_with(self.filter.get())
if __name__ == '__main__':
root = Tk()
root.resizable(False, False)
my_gui = GUI(root)
root.mainloop()
from enum import Enum
class COLOR(Enum):
red = 0
green = 1
blue = 2
from skimage import io
from pixel import *
class Editor(object):
def __init__(self, origin_path, edit_path):
self.image_path = origin_path
self.edit_path = edit_path
def do_convolution_with(self, option):
image = io.imread(fname=self.image_path)
edited_image = io.imread(fname=self.image_path) # copy.deepcopy(image)
# if image is grey scale then there is only width and height
height, width, _ = image.shape
for v in range(height):
for u in range(width):
pixel = Pixel(image, v, u)
edited_image[v, u] = pixel.filter_with(option)
io.imsave(arr=edited_image, fname=self.edit_path)
import numpy as np
from color import *
from mask import *
class FILTER(Enum):
empty = '---'
mod_laplacian = 'Modified Laplacian'
laplacian = 'Laplacian'
gaussian = 'Gaussian'
median = 'Median'
box = 'Box'
min = 'Min'
max = 'Max'
class Filter(object):
def __init__(self, image, v, u):
self.image = image
self.v = v
self.u = u
def use(self, option, linear=True, dim=(5, 5)):
if linear:
return self.linear_filter(option)
else:
return self.non_linear_filter(option, dim)
def linear_filter(self, option):
# just filter with uneven dimensions
height_I, width_I, _ = self.image.shape
H = np.matrix(Mask().H[option])
divider = abs(H.sum())
if divider == 0:
divider = 1
height_H, width_H = H.shape
center_i = center_j = int(height_H / 2)
new_pixel = [0, 0, 0]
for i in range(-center_i, center_i + 1):
for j in range(-center_j, center_j + 1):
if (self.u + i >= 0) and (self.u + i <= width_I - 1) and \
(self.v + j >= 0) and (self.v + j <= height_I - 1):
for color in COLOR:
new_pixel[color.value] += self.image[self.v + j, self.u + i, color.value] * \
np.multiply(1 / divider, H[center_j + j, center_i + i])
return new_pixel
def non_linear_filter(self, option, dim=(5, 5)):
# just filter with uneven dimensions
height_I, width_I, _ = self.image.shape
height_R, width_R = dim
center_i = center_j = int(height_R / 2)
colors_neighbors = [[], [], []]
for i in range(-center_i, center_i + 1):
for j in range(-center_j, center_j + 1):
if (self.u + i >= 0) and (self.u + i <= width_I - 1) and \
(self.v + j >= 0) and (self.v + j <= height_I - 1):
for color in COLOR:
colors_neighbors[color.value] += [self.image[self.v + j, self.u + i, color.value]]
if option == FILTER.median.value:
return [
np.median(colors_neighbors[COLOR.red.value]),
np.median(colors_neighbors[COLOR.green.value]),
np.median(colors_neighbors[COLOR.blue.value])
]
elif option == FILTER.min.value:
return [
np.min(colors_neighbors[COLOR.red.value]),
np.min(colors_neighbors[COLOR.green.value]),
np.min(colors_neighbors[COLOR.blue.value])
]
elif option == FILTER.max.value:
return [
np.max(colors_neighbors[COLOR.red.value]),
np.max(colors_neighbors[COLOR.green.value]),
np.max(colors_neighbors[COLOR.blue.value])
]
images/Example/White_Plain_In_White_Sky/Box.jpeg

13.2 KiB

images/Example/White_Plain_In_White_Sky/Diff_Gaussian_to_Box.jpeg

44.6 KiB

images/Example/White_Plain_In_White_Sky/Diff_Normal_Laplace.jpeg

45 KiB

images/Example/White_Plain_In_White_Sky/Gaussian.jpeg

12.4 KiB

images/Example/White_Plain_In_White_Sky/Laplace.jpeg

44.9 KiB

images/Example/White_Plain_In_White_Sky/Max.jpeg

12.7 KiB

images/Example/White_Plain_In_White_Sky/Median.jpeg

11.1 KiB

images/Example/White_Plain_In_White_Sky/Min.jpeg

12 KiB

images/Example/White_Plain_In_White_Sky/Modified_Laplace.jpeg

27.9 KiB

images/Example/White_Plain_In_White_Sky/Normal.jpeg

24.2 KiB

images/Example/White_Plain_In_White_Sky/Threshold_100_Modified_Laplce.jpeg

18.6 KiB

images/Example/White_Plain_In_White_Sky/Threshold_80_Modified_Laplce.jpeg

14.5 KiB

images/Example/White_Plain_In_White_Sky/Threshold_90_Modified_Laplce.jpeg

16.5 KiB

images/Rocket_Launch.jpeg

20.3 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment