Skip to content

Commit 9866b01

Browse files
authoredDec 17, 2017
Add files via upload
1 parent 616064d commit 9866b01

File tree

3 files changed

+50
-29
lines changed

3 files changed

+50
-29
lines changed
 

‎imageManipulation.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
from PIL import Image
22
from tkinter import filedialog
3-
import imghdr
43

5-
def resizeImage(im, width, height, skipdialog=False):
6-
imNN = im.resize((width, height), Image.NEAREST) # use nearest neighbour
7-
# imBL = im.resize((width, height), Image.BILINEAR) # linear interpolation in a 2x2 environment
8-
# imBC = im.resize((width, height), Image.BICUBIC) # cubic spline interpolation in a 4x4 environment
9-
# imAA = im.resize((width, height), Image.ANTIALIAS) # best down-sizing filter
4+
5+
def resizeImage(im, filtr, width, height, skipdialog=False):
6+
if filtr == "Nearest Neighbour":
7+
image = im.resize((width, height), Image.NEAREST) # nearest neighbour
8+
elif filtr == "Bilinear":
9+
image = im.resize((width, height), Image.BILINEAR) # bilinear interpolation
10+
elif filtr == "Bicubic":
11+
image = im.resize((width, height), Image.BICUBIC) # bicubic interpolation
12+
elif filtr == "Anti-Alias":
13+
image = im.resize((width, height), Image.ANTIALIAS) # antialiasing
1014

1115
if skipdialog:
12-
file = "Test.png"
16+
file = (filtr + ".png")
1317
else:
1418
myFormats = [('Portable Network Graphics (.png)', '*.png'),
1519
('JPEG / JFIF (.jpg)', '*.jpg'),
@@ -18,5 +22,5 @@ def resizeImage(im, width, height, skipdialog=False):
1822
file = filedialog.asksaveasfilename(defaultextension=".png", filetypes=myFormats)
1923

2024
if file:
21-
imNN.save(file)
25+
image.save(file)
2226
return file

‎main.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from tkinter import Tk, Label, Entry, Button, StringVar, W, E, filedialog, messagebox
1+
from tkinter import Tk, Label, Entry, Button, StringVar, W, E, filedialog, messagebox, ttk
22
import imageManipulation
33
from PIL import Image
44
import imghdr
@@ -13,6 +13,7 @@ def __init__(self, master):
1313
self.filename = "..."
1414
self.xOrig = "X"
1515
self.yOrig = "Y"
16+
self.filter = "Nearest Neighbour"
1617

1718
self.filename_label_text = StringVar()
1819
self.filename_label_text.set(self.filename)
@@ -38,6 +39,11 @@ def __init__(self, master):
3839
self.contentY = StringVar()
3940
self.entryY = Entry(master, text="Height: ", validate='key', validatecommand=vc, textvariable=self.contentY)
4041

42+
self.combobox_value = StringVar()
43+
self.combobox = ttk.Combobox(master, textvariable=self.combobox_value)
44+
self.combobox['values'] = ('Nearest Neighbour', 'Bilinear', 'Bicubic', 'Anti-Alias')
45+
self.combobox.current(0)
46+
4147
self.browse_button = Button(master, text="Browse...", command=lambda: self.update("browse"))
4248
self.generate_button = Button(master, text="Generate", command=lambda: self.update("generate"))
4349
self.reset_button = Button(master, text="Reset", command=lambda: self.update("reset"))
@@ -54,8 +60,9 @@ def __init__(self, master):
5460
self.label_y_orig.grid(row=3, column=1, sticky=W+E)
5561
self.entryX.grid(row=2, column=4, sticky=W+E)
5662
self.entryY.grid(row=3, column=4, sticky=W+E)
57-
self.generate_button.grid(row=4, column=0, columnspan=4, sticky=W+E)
58-
self.reset_button.grid(row=4, column=4, sticky=W+E)
63+
self.combobox.grid(row=4, column=0, columnspan=5, sticky=W+E)
64+
self.generate_button.grid(row=5, column=0, columnspan=4, sticky=W+E)
65+
self.reset_button.grid(row=5, column=4, sticky=W+E)
5966

6067
self.im = Image
6168

@@ -89,7 +96,8 @@ def update(self, method):
8996
root.withdraw()
9097
elif method == "generate":
9198
if not (self.contentX.get() == "") or (self.contentY.get() == ""):
92-
imageManipulation.resizeImage(self.im, int(self.contentX.get()), int(self.contentY.get()))
99+
self.filter = self.combobox.get()
100+
imageManipulation.resizeImage(self.im, self.filter, int(self.contentX.get()), int(self.contentY.get()))
93101
else: # reset
94102
self.clear()
95103

@@ -102,6 +110,7 @@ def clear(self):
102110
self.label_y_orig_text.set(self.yOrig)
103111
self.contentX.set("")
104112
self.contentY.set("")
113+
self.combobox.current(0)
105114

106115
def validate(self, action, index, value_if_allowed,
107116
prior_value, text, validation_type, trigger_type, widget_name):

‎test.py

+25-17
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@
55
import imageManipulation
66

77

8-
class TestMethods(unittest.TestCase):
8+
class TestMethods(unittest.TestCase):
9+
10+
def test_test_file_exists(self):
11+
self.assertTrue(os.path.isfile("C:\\test.png"))
912

10-
def setUp(self):
11-
global filepath
12-
filepath = "C:\\test.png"
13-
14-
def test_file_exists(self):
15-
self.assertTrue(os.path.isfile(filepath))
16-
17-
def test_file_is_image(self):
18-
self.assertNotEqual(imghdr.what(filepath), None)
13+
def test_test_file_is_image(self):
14+
self.assertNotEqual(imghdr.what("C:\\test.png"), None)
1915

2016
def test_diallow_non_images(self):
2117
fpath = "C:\\test.txt"
@@ -26,15 +22,27 @@ def test_diallow_non_images(self):
2622
height = 500
2723
imageManipulation.resizeImage(im, width, height, True)
2824

25+
def test_create(self):
26+
im = Image.open("C:\\test.png")
27+
filtr = "Nearest Neighbour"
28+
self.width = 500
29+
self.height = 500
30+
self.im2path = imageManipulation.resizeImage(im, filtr, self.width, self.height, True)
31+
self.assertIsNotNone(self.im2path)
32+
33+
def test_created_file_exists(self):
34+
self.assertTrue(os.path.isfile("Nearest Neighbour.png"))
35+
36+
def test_created_file_is_image(self):
37+
self.assertNotEqual(imghdr.what("Nearest Neighbour.png"), None)
38+
2939
def test_resized_dimensions_correct(self):
30-
im = Image.open(filepath)
31-
width = 500
32-
height = 500
33-
im2path = imageManipulation.resizeImage(im, width, height, True)
34-
im2 = Image.open(im2path)
40+
im2 = Image.open("Nearest Neighbour.png")
41+
self.width = 500
42+
self.height = 500
3543
newWidth, newHeight = im2.size
36-
self.assertEqual(height, newHeight)
37-
self.assertEqual(width, newWidth)
44+
self.assertEqual(self.height, newHeight)
45+
self.assertEqual(self.width, newWidth)
3846

3947

4048
if __name__ == '__main__':

0 commit comments

Comments
 (0)