-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuraperture.py
132 lines (103 loc) · 2.79 KB
/
uraperture.py
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import numpy as np
import scipy.fftpack as sfft
import matplotlib.pyplot as plt
from PIL import Image
def image_to_array(path):
"""
A function which inputs an image and returns an array of the pixels.
Parameters
----------
path : string
Returns
-------
arr - numpy array
"""
#Opens the image.
image = Image.open(path)
#Converts to greyscale.
image = image.convert('1')
#Establishes the size of the array.
X = image.size[0]
Y = image.size[1]
#Creates an array where each value is the value of the
#pixel of the image.
arr = [[image.getpixel((x,y)) for x in range(X)] for y in range(Y)]
#Converts to numpy array.
arr = np.array(arr)
return arr/255
def compute_intensity(aperture):
"""
Computes the screen diffraction pattern.
Parameters
----------
aperture : 2D numpy array
Returns
-------
screen : a 2D numpy array
"""
#Computes a 2D fast fourier transform of the aperture
screen = sfft.fft2(aperture)
#Scipy magic
screen = np.abs(sfft.fftshift(screen))
return screen
def crop(screen, k):
"""
Crops the screen (or any numpy array) around its centre leaving k-percent
of the original array.
Parameters
----------
screen : numpy array
the array needed to be cropped
k : float
the percentage by which the array is cropped
"""
x = screen.shape[0]
y = screen.shape[1]
cropx = int(k*x)
cropy = int(k*y)
startx = x//2 - cropx//2
starty = y//2 - cropy//2
screen = screen[starty:starty+cropy, startx:startx+cropx]/screen.max()
return screen
def plot(aperture, screen, figsize=None, title=None, titlesize=None):
"""
A simple plotting function, designed to facillitate the plotting of results.
Parameters
----------
aperture : 2D numpy array
the aperture array to be plotted
screen : 2D numpy array
the screen array to be plotted
figsize : tuple; optional
size of the figure in inches
title : string; optional
title of the figure
titlesize : int; optional
font size for the title
Returns
-------
fig : matplotlib.pyplot.figure instance
"""
#Presets figure
fig = plt.figure(figsize=figsize)
plt.suptitle(title, size=titlesize)
#Getting axes object for first plot
ax1 = plt.subplot(121)
#Displays image
plt.imshow(aperture)
plt.title('Aperture')
#Removes pixel numbers on axes
ax1.set_yticklabels([])
ax1.set_xticklabels([])
#Removes axes ticks
plt.xticks([])
plt.yticks([])
#Analogously
ax2 = plt.subplot(122)
plt.imshow(screen)
plt.title('Screen')
ax2.set_yticklabels([])
ax2.set_xticklabels([])
plt.xticks([])
plt.yticks([])
return fig