-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic_functions.py
More file actions
117 lines (95 loc) · 2.79 KB
/
basic_functions.py
File metadata and controls
117 lines (95 loc) · 2.79 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#These are some basic imagehandling codes in Pillow/PIL.
from PIL import Image
from PIL import ImageDraw as id
from collections import Counter
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import numpy as np
#Function to input the path of image. It returns array of pixels.
def open_image(path):
newImage = Image.open(path).convert("L")
pixel=newImage.getdata()
numarr=np.asarray(pixel,dtype="int16")
#channel=len(pixel[0])
width=newImage.size[0]
height=newImage.size[1]
arr=numarr.reshape(height,width) #important. when reshaping swap the width and height because width=no. of cols. etc
return arr
#Function to brighten image
def brighten(arr,value):
for i in range(arr.shape[0]):
for j in range(arr.shape[1]):
arr[i][j]+=value
if arr[i][j]>255:
arr[i][j]=255
return arr
#Function to create contrast in image
def contrast(arr,mul,value):
for i in range(arr.shape[0]):
for j in range(arr.shape[1]):
arr[i][j]*=mul
arr[i][j]+=value
if arr[i][j]>255:
arr[i][j]=255
return arr
#Function to binarize image
def binarize(arr,threshold1):
for i in range(arr.shape[0]):
for j in range(arr.shape[1]):
if arr[i][j]>threshold1:
arr[i][j]=255
else:
arr[i][j]=0
return arr
#Basic function to draw line on the opened image using PIL
def drawonimage(path):
im = Image.open(path)
draw = id.Draw(im)
draw.line((0, 0) + im.size, fill=128)
im.save("drawimage.jpg")
im.show()
#function to draw a specific shape on blank image
def drawonnew(i,j,r):
im = Image.new("RGB", (i, j), "white") #blank image
draw = id.Draw(im)
a=i/2
b=j/2
draw.line((a-r,b-r, a+r,b-r), fill=300)
draw.line((a-r,b-r, a-r,b+r), fill=300)
draw.line((a+r,b-r, a+r,b+r), fill=300)
draw.line((a-r,b+r, a+r,b+r), fill=300)
bbox = (a-r, b-r, a+r, b+r)
draw.ellipse(bbox, outline=120)
#draw.rectangle(bbox, outline=120)
im.save("drawnimage.jpg")
im.show()
#histogram
def hist(arr):
hist=np.zeros([256])
for i in range(arr.shape[0]):
for j in range(arr.shape[1]):
hist[arr[i][j]]=hist[arr[i][j]]+1
return hist
def save_image(arr,path):
img = Image.fromarray(arr.astype('uint8'))
img.save(path)
img.show()
#Histogram of pixel distribution
def plothist(arr):
num_bins = 256
n, bins, patches = plt.hist(arr, num_bins, facecolor='blue', alpha=0.5)
plt.xlabel('number of pixels')
plt.ylabel('pixel distribution')
plt.title('Histogram of pixel distribution')
plt.show()
a=open_image("download.jpg")
#b=contrast(a,1.3,20)
#save_image(b,"contrast.jpg")
#c=binarize(a,200)
#save_image(c,"binarize.jpg")
#d=hist(a)
#plothist(d)
#plt.imshow("download.jpg")
#save_image(a)
#drawonimage("download.jpg")
drawonnew(300,300,40)