Plot circular gradients using numpy
You can use vectorization to very efficiently calculate the distance without the need for a for-loop:
x_axis = np.linspace(-1, 1, 256)[:, None]y_axis = np.linspace(-1, 1, 256)[None, :]arr = np.sqrt(x_axis ** 2 + y_axis ** 2)
or you can use a meshgrid:
x_axis = np.linspace(-1, 1, 256)y_axis = np.linspace(-1, 1, 256)xx, yy = np.meshgrid(x_axis, y_axis)arr = np.sqrt(xx ** 2 + yy ** 2)
and interpolate between inner
and outer
colors using broadcasting again
inner = np.array([0, 0, 0])[None, None, :]outer = np.array([1, 1, 1])[None, None, :]arr /= arr.max()arr = arr[:, :, None]arr = arr * outer + (1 - arr) * inner
Because of symmetry, actually just need to calculate one-fourth of image 256*256 which is 64*64, then rotate it with 90 degrees piece by piece and combine them. In this way, the total time is 1/4 times than calculating 256*256 pixel.
the following is example.
import numpy as npimport matplotlib.pyplot as plt##Just calculate 64*64arr = np.zeros((64,64,3), dtype=np.uint8)imgsize = arr.shape[:2]innerColor = (0, 0, 0)outerColor = (255, 255, 255)for y in range(imgsize[1]): for x in range(imgsize[0]): #Find the distance to the corner distanceToCenter = np.sqrt((x) ** 2 + (y - imgsize[1]) ** 2) #Make it on a scale from 0 to 1innerColor distanceToCenter = distanceToCenter / (np.sqrt(2) * imgsize[0]) #Calculate r, g, and b values r = outerColor[0] * distanceToCenter + innerColor[0] * (1 - distanceToCenter) g = outerColor[1] * distanceToCenter + innerColor[1] * (1 - distanceToCenter) b = outerColor[2] * distanceToCenter + innerColor[2] * (1 - distanceToCenter) # print r, g, b arr[y, x] = (int(r), int(g), int(b))#rotate and combinearr1=arrarr2=arr[::-1,:,:]arr3=arr[::-1,::-1,:]arr4=arr[::,::-1,:]arr5=np.vstack([arr1,arr2])arr6=np.vstack([arr4,arr3])arr7=np.hstack([arr6,arr5])plt.imshow(arr7, cmap='gray')plt.show()