I have the following code that generates a fractal image, the problem is how to reconstruct numbers to be a matrix.
from PIL import Image
from pylab import *
from numpy import NaN
import numpy as np
import matplotlib.pyplot as plt
def julia(C):
X = arange(-1.5, 1.5, 0.05)
Y = arange(-1.5, 1.5, 0.05)
pixel = zeros((len(Y), len(X)))
plt.axis('off')
for x_iter, x in enumerate(X):
for y_iter, y in enumerate(Y):
z = x + 1j * y
intensity = NaN
r = np.empty((100, 100))
for n in range(1, 1024):
if abs(z) > 2:
intensity = n
break
z = z**2 + C
pixel[y_iter, x_iter] = intensity
r.fill(intensity)
print("intensity_matrix : ",r)
julia(-0.7 + 0.27015j)
I want to print r, but all the same intensity elements rush together looks
[4. 4. 4. ... 4. 4. 4.]
[4. 4. 4. ... 4. 4. 4.]]
intensity_matrix : [[5. 5. 5. ... 5. 5. 5.]
[5. 5. 5. ... 5. 5. 5.]
[5. 5. 5. ... 5. 5. 5.]
...
[5. 5. 5. ... 5. 5. 5.]
[5. 5. 5. ... 5. 5. 5.]
[5. 5. 5. ... 5. 5. 5.]]
intensity_matrix : [[7. 7. 7. ... 7. 7. 7.]
[7. 7. 7. ... 7. 7. 7.]
[7. 7. 7. ... 7. 7. 7.]
...
[7. 7. 7. ... 7. 7. 7.]
[7. 7. 7. ... 7. 7. 7.]
[7. 7. 7. ... 7. 7. 7.]]
intensity_matrix : [[965. 965. 965. ... 965. 965. 965.]
[965. 965. 965. ... 965. 965. 965.]
[965. 965. 965. ... 965. 965. 965.]
...
[965. 965. 965. ... 965. 965. 965.]
[965. 965. 965. ... 965. 965. 965.]
[965. 965. 965. ... 965. 965. 965.]]
intensity_matrix : [[6. 6. 6. ... 6. 6. 6.]
[6. 6. 6. ... 6. 6. 6.]
[6. 6. 6. ... 6. 6. 6.]
How can I correct the line
r.fill(intensity)
to obtain a regular matrix? looks as for example
Out[56]:
array([0, 0, 0, 0, 0, 0, 0, 0, 24, 88, 3, 121, 121,
4, 12, 15, 1, 19, 22, 2, 8, 31, 21, 12, 11, 110,
40, 53, 43, 43, 81, 41, 122, 20, 32, 21, 122, 6, 8,
18, 40, 4, 4, 2, 45, 45, 5, 46, 86, 20, 19, 119,
10, 20, 46, 37, 11, 50, 35, 7, 21, 7, 8, 9, 11,
46, 94, 76, 69, 31, 67, 46, 57, 43, 35, 48, 86, 116,
32, 20, 40, 46, 14, 52, 37, 11, 11, 10, 50, 26, 83,
25, 7, 6, 5, 5, 12, 12, 10, 31, 12, 113, 7, 4,
14, 104, 48, 89, 1, 1, 1, 1, 1, 1, 1, 95, 31,
18, 46, 4, 1, 0, 0, 0, 0, 0, 0, 34, 1, 2,
0, 0, 0, 0, 0, 0, 1, 48, 13, 19, 103, 4, 71,
1, 1, 0, 0, 1, 2, 10, 11, 66, 11, 24, 10, 62,
1, 1, 0, 0, 0, 0, 1, 11, 10, 6, 55, 19, 34,
74, 122, 74, 32, 7, 25, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
], dtype=uint16)
Please show me how can I fix this problem? done.
# another question
this is the pixel data
import numpy as np
import matplotlib.pyplot as plt
def julia(C):
X = np.arange(-1.5, 1.5, 0.2)
Y = np.arange(-1.5, 1.5, 0.2)
pixel = np.zeros((len(Y), len(X)))
for x_iter, x in enumerate(X):
for y_iter, y in enumerate(Y):
z = x + 1j * y
intensity = np.nan
r = np.empty((100, 100)) # Unused at the moment
for n in range(1, 1024):
if abs(z) > 2:
intensity = n
break
z = z**2 + C
pixel[y_iter, x_iter] = intensity
r.fill(intensity) # Unused at the moment
# We return pixel matrix
return pixel
# Compute Julia set image
pixel = julia(-0.7 + 0.27015j)
# Plotting
print(pixel[:,:])
print(pixel[:,:].shape)
[[ 1. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.
2.]
[ 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.
2.]
[ 2. 2. 2. 2. 2. 2. 2. 3. 3. 3. 3. 3. 2. 2.
2.]
[ 2. 2. 2. 2. 3. 3. 3. 4. 5. 4. 4. 3. 3. 3.
2.]
[ 2. 2. 3. 3. 3. 4. 4. 7. 209. 6. 5. 4. 4. 3.
3.]
[ 2. 3. 3. 3. 4. 5. 6. 37. 59. 220. 13. 7. 10. 6.
4.]
[ 3. 3. 4. 10. 7. 8. 9. 13. 408. 99. 126. 401. 537. 437.
10.]
[ 3. 4. 6. 23. 40. 112. 68. 685. 48. 591. 567. 290. 117. 353.
11.]
[ 4. 11. 353. 117. 290. 567. 591. 48. 685. 68. 112. 40. 23. 6.
4.]
[ 4. 10. 437. 537. 401. 126. 99. 408. 13. 9. 8. 7. 10. 4.
3.]
[ 3. 4. 6. 10. 7. 13. 220. 59. 37. 6. 5. 4. 3. 3.
3.]
[ 2. 3. 3. 4. 4. 5. 6. 209. 7. 4. 4. 3. 3. 3.
2.]
[ 2. 2. 3. 3. 3. 4. 4. 5. 4. 3. 3. 3. 2. 2.
2.]
[ 2. 2. 2. 2. 3. 3. 3. 3. 3. 2. 2. 2. 2. 2.
2.]
[ 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.
2.]]
(15, 15)
after obtaining the image and I did plt.savefig(), when I did image.open(), the data becomes as follows!
array([[[255, 255, 255, 255],
[255, 255, 255, 255],
[255, 255, 255, 255],
...,
[255, 255, 255, 255],
[255, 255, 255, 255],
[255, 255, 255, 255]],
[[255, 255, 255, 255],
[255, 255, 255, 255],
[255, 255, 255, 255],
...,
[255, 255, 255, 255],
[255, 255, 255, 255],
[255, 255, 255, 255]],
[[255, 255, 255, 255],
[255, 255, 255, 255],
[255, 255, 255, 255],
...,
[255, 255, 255, 255],
[255, 255, 255, 255],
[255, 255, 255, 255]],
...,
[[255, 255, 255, 255],
[255, 255, 255, 255],
[255, 255, 255, 255],
...,
[255, 255, 255, 255],
[255, 255, 255, 255],
[255, 255, 255, 255]],
[[255, 255, 255, 255],
[255, 255, 255, 255],
[255, 255, 255, 255],
...,
[255, 255, 255, 255],
[255, 255, 255, 255],
[255, 255, 255, 255]],
[[255, 255, 255, 255],
[255, 255, 255, 255],
[255, 255, 255, 255],
...,
[255, 255, 255, 255],
[255, 255, 255, 255],
[255, 255, 255, 255]]], dtype=uint16)
The shape now becomes (around 240, around 280) something like that. The original data dimension is just (15, 15). The extra shape I believe they are the unwanted white spaces boundary that corresponding to all the 255 intensities. I even can not check if the pixel matrix has the values between 1 and 1022 with that (200 more, 200 more) shape. I need the image with removed white space around the image. I have to get rid of the white space that around the image for doing further image processing analysis, do you know how to rewrite the code? bousof!
bousof! I checked
min_value = np.nanmin(pixel)
min_value
Out[4]: 1.0
max_value = np.nanmax(pixel)
max_value
Out[6]: 685.0
they are OK
but when I checked
pixel_int = (255*(pixel-min_value)/(max_value-min_value)).astype(np.uint8)
pixel_int
Out[9]:
array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 1, 1, 2, 77, 1, 1, 1, 1,
0, 0],
[ 0, 0, 0, 0, 1, 1, 1, 13, 21, 81, 4, 2, 3,
1, 1],
[ 0, 0, 1, 3, 2, 2, 2, 4, 151, 36, 46, 149, 199,
162, 3],
[ 0, 1, 1, 8, 14, 41, 24, 255, 17, 219, 211, 107, 43,
131, 3],
[ 1, 3, 131, 43, 107, 211, 219, 17, 255, 24, 41, 14, 8,
1, 1],
[ 1, 3, 162, 199, 149, 46, 36, 151, 4, 2, 2, 2, 3,
1, 0],
[ 0, 1, 1, 3, 2, 4, 81, 21, 13, 1, 1, 1, 0,
0, 0],
[ 0, 0, 0, 1, 1, 1, 1, 77, 2, 1, 1, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0]], dtype=uint8)
there are problems. For all the elements that greater than 255 would be compressed to 255/(that means losing some information). The problem should be because of using astype(np.uint8) instead of astype(np.uint16). Therefore, I modify the line to be
pixel_int = (255*(pixel-min_value)/(max_value-min_value)).astype(np.uint16)
pixel_int
Out[11]:
array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 1, 1, 2, 77, 1, 1, 1, 1,
0, 0],
[ 0, 0, 0, 0, 1, 1, 1, 13, 21, 81, 4, 2, 3,
1, 1],
[ 0, 0, 1, 3, 2, 2, 2, 4, 151, 36, 46, 149, 199,
162, 3],
[ 0, 1, 1, 8, 14, 41, 24, 255, 17, 219, 211, 107, 43,
131, 3],
[ 1, 3, 131, 43, 107, 211, 219, 17, 255, 24, 41, 14, 8,
1, 1],
[ 1, 3, 162, 199, 149, 46, 36, 151, 4, 2, 2, 2, 3,
1, 0],
[ 0, 1, 1, 3, 2, 4, 81, 21, 13, 1, 1, 1, 0,
0, 0],
[ 0, 0, 0, 1, 1, 1, 1, 77, 2, 1, 1, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0]], dtype=uint16)
The problem is still!
The pixel matrix(original data) without losing any information/or without any compression is as follows:
[[ 1. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.
2.]
[ 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.
2.]
[ 2. 2. 2. 2. 2. 2. 2. 3. 3. 3. 3. 3. 2. 2.
2.]
[ 2. 2. 2. 2. 3. 3. 3. 4. 5. 4. 4. 3. 3. 3.
2.]
[ 2. 2. 3. 3. 3. 4. 4. 7. 209. 6. 5. 4. 4. 3.
3.]
[ 2. 3. 3. 3. 4. 5. 6. 37. 59. 220. 13. 7. 10. 6.
4.]
[ 3. 3. 4. 10. 7. 8. 9. 13. 408. 99. 126. 401. 537. 437.
10.]
[ 3. 4. 6. 23. 40. 112. 68. 685. 48. 591. 567. 290. 117. 353.
11.]
[ 4. 11. 353. 117. 290. 567. 591. 48. 685. 68. 112. 40. 23. 6.
4.]
[ 4. 10. 437. 537. 401. 126. 99. 408. 13. 9. 8. 7. 10. 4.
3.]
[ 3. 4. 6. 10. 7. 13. 220. 59. 37. 6. 5. 4. 3. 3.
3.]
[ 2. 3. 3. 4. 4. 5. 6. 209. 7. 4. 4. 3. 3. 3.
2.]
[ 2. 2. 3. 3. 3. 4. 4. 5. 4. 3. 3. 3. 2. 2.
2.]
[ 2. 2. 2. 2. 3. 3. 3. 3. 3. 2. 2. 2. 2. 2.
2.]
[ 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.
2.]]
bousof! what do you think about this line?
pixel_int = (255*(pixel-min_value)/(max_value-min_value)).astype(np.uint16)
how should we rewrite it?