2

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?

Daniel L
  • 31
  • 6
  • Hi Daniel. Could you explain what is r for in your code? Why do you create a 100x100 array that you fill with the same value ? – bousof Jun 29 '20 at 11:35
  • In your example, you already constructed an array `pixel` containing intensities that looks like the output you are looking for. – bousof Jun 29 '20 at 13:24
  • Thanks bousof ! r is just an empty matrix that was created for intending later to contain all the values of the intensity. If I don't have to assign such an empty matrix r and can be able to make an intensity matrix, I won't set it up. 100x100 just, for instance, it can be (10,10) or whatever format (n, n) a matrix is. No matter what, I just want to write the intensity to be a matrix, and that is all I want. Do you know how to write that code matrix[intensity], bousof? – Daniel L Jun 29 '20 at 13:26
  • it may be because of my code print(pixel) is not correct! bousof, do you know how to print the pixel out correctly? – Daniel L Jun 29 '20 at 13:32
  • print (pixel) is what I am looking for, for I try to print pixel again and this time it works. thanks again bousof! – Daniel L Jun 29 '20 at 15:01
  • You are welcome ! I put a code for you on how to plot the answer. Using `print(pixel)` will be difficult to read. I tried your code on some of the wikipedia examples and it seems to give the good results (https://en.wikipedia.org/wiki/Julia_set#Quadratic_polynomials). – bousof Jun 29 '20 at 15:13

1 Answers1

0

Your code actually works you are just focusing on the wrong array r instead of pixel. Here is the code, I modified the function julia to return the pixel array. This array is then plotted:

import numpy as np
import matplotlib.pyplot as plt

def julia(C):
    X = np.arange(-1.5, 1.5, 0.05)
    Y = np.arange(-1.5, 1.5, 0.05)
    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
plt.imshow(pixel)
plt.colorbar()
plt.show()

Output:

enter image description here


EDIT


You can save and load your pixel as a png using this script (for further details it's a mix between this and this):

# Small script saving the image as a png
from PIL import Image
min_value = np.nanmin(pixel)
max_value = np.nanmax(pixel)
pixel_int = (255*(pixel-min_value)/(max_value-min_value)).astype(np.uint8)
# sample LUT from matplotlib
lut = (plt.cm.viridis(np.arange(256)) * 255).astype(np.uint8) # CHOOSE COLORMAP HERE viridis, jet, rainbow
pixel_rgb = lut[pixel_int]
# changing NaNs to a chosen color
nan_color = [0,0,0,0] # Transparent NaNs
for i,c in enumerate(nan_color):
  pixel_rgb[:,:,i] = np.where(np.isnan(pixel),c,pixel_rgb[:,:,i])
# apply LUT and display
img = Image.fromarray(pixel_rgb, 'RGBA')
img.save('julia.png')
Image.open('julia.png').show()

Here is what you get for the viridis and jet colormaps:

enter image description here enter image description here

bousof
  • 1,241
  • 11
  • 13
  • Another question for you bousof! Can you show me how can I save the image in unit16? so that I can later do data = np.arrarry(image) that would give me back the pixel (original data) without losing any information/or without any compression. – Daniel L Jul 01 '20 at 03:52
  • bousof! the matrix element is seen in uint16, but I have to get rid of the white spaces around the image! the white space corresponding to 255 that seriously affects my analysis on the image, for they are not the desired data! there, therefore, do you know how to get rid of these white spaces while coding the image? – Daniel L Jul 03 '20 at 13:50
  • Hi Daniel. Sorry I don't understand what white spaces you are referring to ? The `pixel` matrix has values between `1` and `1022` (excluding NaNs). – bousof Jul 03 '20 at 14:12
  • bousof, let me show you what white spaces I am referring to in my question area – Daniel L Jul 05 '20 at 01:52
  • @DanielL I didn't know about the Pillow package. I edited solution and added a small script that saves only the content of the `pixel` array as a png image. – bousof Jul 05 '20 at 05:39
  • bousof! please look at my new edit content in my question area (due to I am not able to write anything in EDIT area/your answer area) – Daniel L Jul 05 '20 at 12:43