Search code examples
pythonnumpyimagemultidimensional-arraypython-imaging-library

3d numpy array output, meant to emulate an image, can't convert to Image


I am creating an autostereogram with the function make_autostereogram. It is being output as a 3d NumPy array (rows, colums, RGB).

It is then input into the Pillow function fromarray(). It has array with the shape being output.

code:

import numpy as np
import matplotlib.pyplot as plt
import skimage, skimage.io
from PIL import Image
#%matplotlib inline

plt.rcParams['figure.dpi'] = 150

def blank_image(shape=(600, 800, 4),
               rgba=(255, 255, 255, 0)):
    "Returns a blank image, of size defined by shape and background color rgb."
    return np.ones(shape, dtype=float) * np.array(rgba) / 255

def normalize(depthmap):
    "Normalizes values of depthmap to [0, 1] range."
    if depthmap.max() > depthmap.min():
        return (depthmap - depthmap.min()) / (depthmap.max() - depthmap.min())
    else:
        return depthmap
    
def display(img, colorbar=False):
    "Displays an image."
    
    plt.figure(figsize=(10, 10))
    if len(img.shape) == 2:
        i = skimage.io.imshow(img, cmap='gray')
        #pass
    else:
        #pass
        i = skimage.io.imshow(img)
    i = skimage.io.imshow(img)
    if colorbar:
        plt.colorbar(i, shrink=0.5, label='depth')
    plt.tight_layout()
    print("display called")

def insert_pattern(background_img, pattern, location):
    #Inserts a pattern onto a background, at given location. Returns new image.
    img = background_img.copy()
    r0, c0 = location
    r1, c1 = r0 + pattern.shape[0], c0 + pattern.shape[1]
    if r1 < background_img.shape[0] and c1 < background_img.shape[1]:
        img[r0:r1, c0:c1, :] = skimage.img_as_float(pattern)
    return img



def tile_horizontally(background_img, pattern, start_location, repetitions, shift):
    "Tiles a pattern on a background image, repeatedly with a given shift."
    img = background_img.copy()
    for i in range(repetitions):
        r, c = start_location
        c += i * shift
        img = insert_pattern(img, pattern, location=(r, c))
    return img

def make_pattern(shape=(16, 16), levels=64):
    "Creates a pattern from gray values."
    return np.random.randint(0, levels - 1, shape) / levels


def create_circular_depthmap(shape=(600, 800), center=None, radius=100):
    "Creates a circular depthmap, centered on the image."
    depthmap = np.zeros(shape, dtype=float)
    r = np.arange(depthmap.shape[0])
    c = np.arange(depthmap.shape[1])
    R, C = np.meshgrid(r, c, indexing='ij')
    if center is None:
        center = np.array([r.max() / 2, c.max() / 2])
    d = np.sqrt((R - center[0])**2 + (C - center[1])**2)
    depthmap += (d < radius)
    return depthmap  


def make_autostereogram(depthmap, pattern, shift_amplitude=0.1, invert=False):
    "Creates an autostereogram from depthmap and pattern."
    print("make_autostereogram called")
    depthmap = normalize(depthmap)
    if invert:
        depthmap = 1 - depthmap
    
    #autostereogram = np.zeros_like(depthmap, dtype=pattern.dtype)
    autostereogram = np.zeros((depthmap.shape[0], depthmap.shape[1], 3))

    for r in np.arange(autostereogram.shape[0]):
        for c in np.arange(autostereogram.shape[1]):
            if c < pattern.shape[1]:
                for I in range(autostereogram.shape[2]): 
                     autostereogram[r, c, I] = pattern[r % pattern.shape[0], c, I] 
            else:
                shift = int(depthmap[r, c] * shift_amplitude * pattern.shape[1])
                for I in range(autostereogram.shape[2]):
                    autostereogram[r, c, I] = autostereogram[r, c - pattern.shape[1] + shift, I] 

    return autostereogram
   



img = blank_image()


"""

"""
texture= Image.open('marble.png')
texture.show()

"""

test_img = insert_pattern(img, coin, (10, 20))


pattern = make_pattern(shape=(128, 64))

#print(type(pattern))

patternImg = Image.fromarray(pattern) 

#patternImg.show()

print("pattern: ", type(pattern))
plt.imshow(pattern)
display(pattern)

depthmap = create_circular_depthmap(radius=150)

#print("depthmap: ", depthmap)

depthmapImg = Image.fromarray(normalize(depthmap))

#depthmapImg.show()

display(depthmap, colorbar=True)



#display(autostereogram)


print("depthmap array shape: ", depthmap.shape)

#print("new pattern: ", newPattern)





autostereogram2 = make_autostereogram(depthmap, newPattern)

print(autostereogram2)

#display(autostereogram2)

#print("stereogram shape: ", autostereogram2.shape())

autostereogramImg = patternImg = Image.fromarray(autostereogram2)

autostereogramImg.save('autostereogramOutput.png')

autostereogramImg.show()

error:

 File "C:\Users\geniu\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\PIL\Image.py", line 3102, in fromarray
    raise TypeError(msg) from e
TypeError: Cannot handle this data type: (1, 1, 3), <f8

error when I use the display() function in the code:

C:\Users\geniu\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\skimage\io\_plugins\matplotlib_plugin.py:158: UserWarning: Float image out of standard range; displaying image with stretched contrast.
  lo, hi, cmap = _get_display_range(image)
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
C:\Users\geniu\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\skimage\io\_plugins\matplotlib_plugin.py:171: UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.
  ax.get_figure().tight_layout()
C:\Users\geniu\OneDrive\GW\Spring_2024\CSCI_6527\final_project.py:34: UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.
  plt.tight_layout()

or, should I just use a black-and-white texture for this source code, which was obtained here, with modifications

https://flothesof.github.io/making-stereograms-Python.html

I am just trying to display or save an image from that array being made, with RGB colors.


Solution

  • From the PIL documentation, you can (and, in this case, you need to) specify a Mode. So, you should have

    Image.fromarray(autostereogram2, mode='RGB')