1

I have the following code that generates a fractal image,


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[:,:])

data (pixel) as follow:

array([[  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.]])

after plotting the data (pixel), it looks as

plt.axis('off')
plt.imshow(pixel)

after obtaining the image and I did plt.savefig(), when I did image.open(), the data becomes as follows! I need to get rid of all the boundary unwanted white space that corresponding to all these 255 intensities

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 white spaces around the image, which has to be gotten rid. I have to get rid of the white space that around the image for doing further image processing analysis, do you know how to write this code to the above code, please help!

Daniel L
  • 31
  • 6

1 Answers1

0

You can use the following script to save the image without the white spaces around:

# Plotting
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')

# Saving image and matrix
img.save('julia.png')
np.save('julia.npy', pixel)

# Delete data
del(img, pixel)

# Loading image and matrix
img = Image.open('julia.png')
pixel = np.load("julia.npy")

# Show image
img.show()

print(pixel)
print(min_value, max_value)

You will get one pixel per array value. The outputs for 2 resilutions of the X and Y axes:

  • X,Y = (np.arange(-1.5, 1.5, 0.2),)*2: enter image description here
  • X,Y = (np.arange(-1.5, 1.5, 0.02),)*2: enter image description here
bousof
  • 1,241
  • 11
  • 13
  • @DanielL you should first copy paste the code of my answer as it is to ensure it works and then modify it for your convenience in a second step. You are getting a white square because you replaced line `lut = (plt.cm.viridis(np.arange(256)) * 255).astype(np.uint8)` by `lut = (plt.cm.viridis(np.arange(256)) * 255).astype(np.uint16)`. – bousof Jul 06 '20 at 07:55
  • Dear bousof! Yes, I did getting a (blue color) square because I replaced that line to uint16. At the moment, I replace that line back to uint8, but I get again a small square exactly like the one that at the left upper corner of your Julia image shown above. It is the same that small! You can obtain a regular size image, but I cannot obtain that same size regular image by using the same CODE! WHY?! What do you think? bousof! – Daniel L Jul 06 '20 at 10:01
  • It's a good question, I just have to paste my code at the end of your code and I obtain the small julia image. Maybe you have changed other things in the code ? Try creating a new file from scratch and paste my code behind yours to ensure nothing was changed. I'm using version `3.8.1` of python and I'm on a Mac. What do you obtain when you run `print(min_value, max_value)` ? – bousof Jul 06 '20 at 10:41
  • Yea! I follow your instruction to run the code, that works! The image now is readable with regular size as the image you gave. And the value of print(min_value, max_value) = 1.0, 1023.0 .Nonetheless, when I did i = Image.open('julia.tiff'), follow up by did i_matrix = np.asarray(i), the data has be compressed! They are print(min_value, max_value) = 0, 255. Dear bousof! I must convert it to be uint16 because I have to open the image in the other module to do image processing and the data must be the uncompressed original data. My python is 3.5. How can I rewrite to solve the problem? bousof – Daniel L Jul 06 '20 at 12:56
  • I am on a ThinkPad Laptop – Daniel L Jul 06 '20 at 13:02
  • Cool! I updated the answer and added the saving and loading of the index array also. It is saved in a separate file called `julia.npy`. Please take a look at this link for multiple options on how to save a numpy array: https://stackoverflow.com/questions/28439701/how-to-save-and-load-numpy-array-data-properly – bousof Jul 06 '20 at 15:58
  • Dear bousof, awesome! just my other module that built up straightly by using “image” input, not a npy file, that means it will not response to input data, but just response for input images. So I don’t know how to operate that module for an input data file "npy" instead of images. Do you have any further ideas of solving this problem, hence, the img = Image.open('julia.tiff') is uncompressed while it is opened in another module?! – Daniel L Jul 07 '20 at 06:10
  • Be precise. I will need to load and process the image (and reverses the compression process, if any), resulting in an image that is identical to the original. But when I check `data = np.asanyarray(Image.open('julia.tiff'))` `print(data)` **# output something like repeated row [ 68 1 84 255] matrix** `min_value = np.nanmin(data)` `max_value = np.nanmax(data)` `print(min_value, max_value)` **# output 0 255 not the original data 1.0 1023.0** Therefore I am not sure if the Julia image with compression or not when being loaded to another module? What do you think, bousof! – Daniel L Jul 07 '20 at 09:13
  • Hi Daniel L, please create a new question detailing your problem with a simple program and specifying the exact formats of the inputs of your signal processing function. I'm sure you'll be able to find a specialist of the Pillow packages to help you on how to solve this problem. I'll also try to. There is an infinity of image formats, from what I know, TIFF uses lossless compression. The information can only be lost at line `pixel_int = (255*(pixel-min_value)/(max_value-min_value)).astype(np.uint8)` where the `pixel` is scaled to go from `0` to `255` but I don't know how to avoid this step. – bousof Jul 07 '20 at 09:31
  • bousof, please look at https://stackoverflow.com/questions/62787527/cannot-load-and-reverses-the-compression-process-of-the-image-resulting-in-an-im – Daniel L Jul 08 '20 at 04:31