Skip to content

Instantly share code, notes, and snippets.

@rep-movsd
Created July 28, 2021 10:18
Show Gist options
  • Save rep-movsd/f220d85e44c003d53f94c2ecd79c0176 to your computer and use it in GitHub Desktop.
Save rep-movsd/f220d85e44c003d53f94c2ecd79c0176 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# Abcdexter, 7/7/2021, 1142
############################
# imports
############################
import sys
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import scipy.fftpack
############################
# functions
############################
def cart2pol(x, y):
'''
:param x: x coordinate
:param y: y coordinate
:return: rho and phi as per polar coordinate (distance and angle)
'''
rho = np.sqrt(x**2 + y**2)
phi = np.rad2deg(np.arctan2(y, x))
# degrees is important
return(rho, phi)
def pol2cart(rho, phi):
'''
:param rho: polar distance
:param phi: counterclock angle from x axis
:return: x,y coordinate
'''
phi = np.deg2rad(phi)
x = rho * np.cos(phi)
y = rho * np.sin(phi)
return(int(x), int(y))
def iterate(img, X, Y, W, H):
'''
iterate 360 degrees
'''
R = [] # array of distances
print(f"image is of size : {W}x{H}")
count = 0
s = 5
for theta in range(0, 360*s): # 0 to 2pi
for r in range(1,2000):
x, y = pol2cart(r, theta/s)
px = x + X
py = y + Y
if (px < W) and (px >= 0) and (py < H) and (py >= 0) and img[px, py] == (0, 0, 0):
R.append(r)
break
return R
############################
# main
############################
if __name__ == '__main__':
USAGE = \
'''
Usage = python3 fft.py <img> [X] [Y]
img : absolute path of the image
X : X coordinate
Y : Y coordinate
'''
try:
pic = Image.open(str(sys.argv[1])).convert('RGB')
W, H = pic.size
img = pic.load()
X, Y = int(sys.argv[2]), int(sys.argv[3])
R = iterate(img, X, Y, W, H)
# R = R*5
# Number of samplepoints
N = len(R)
# print(R)
xf = np.linspace(0.0, N, N)
yf = R # scipy.fftpack.fft(R)
fig, ax = plt.subplots()
ax.plot(xf, yf)
plt.show()
except IndexError:
print(USAGE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment