I am inputting a numpy array, which was generated from an image into a function. That function makes an autostereogram by horizontally repeating the image and shifting certain pixels.
This is done through another numpy array, with coordinates [r, c] of the autostereogram array being based on coordinates of the texture array, which some math modifying the coordinates.
The problem is, the code is treating one of the coordinates as a sequence. The code has some problem with the indexing.
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 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 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)
for r in np.arange(autostereogram.shape[0]):
for c in np.arange(autostereogram.shape[1]):
if c < pattern.shape[1]:
autostereogram[r, c] = pattern[int(r % pattern.shape[0]), c] #pattern[r, c] #
else:
shift = int(depthmap[r, c] * shift_amplitude * pattern.shape[1])
autostereogram[r, c] = autostereogram[r, int(c - pattern.shape[1] + shift)] #autostereogram[r, c]#
return autostereogram
img = blank_image()
texture= Image.open('marble.png')
texture.show()
depthmap = create_circular_depthmap(radius=150)
#print("depthmap: ", depthmap)
depthmapImg = Image.fromarray(normalize(depthmap))
newPattern = np.array(texture)
print("new pattern: ", type(newPattern))
autostereogram2 = make_autostereogram(depthmap, newPattern)
"""
display(autostereogram)
autostereogram2.show()"""
error message:
TypeError: only length-1 arrays can be converted to Python scalars
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:UsersgeniuOneDriveGWSpring_2024CSCI_6527final_project.py", line 171, in <module>
autostereogram2 = make_autostereogram(depthmap, newPattern)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersgeniuOneDriveGWSpring_2024CSCI_6527final_project.py", line 85, in make_autostereogram
autostereogram[r, c] = pattern[int(r % pattern.shape[0]), c] #pattern[r, c] #
~~~~~~~~~~~~~~^^^^^^
ValueError: setting an array element with a sequence.
1
The culprit:
texture= Image.open('marble.png')
I’m assuming your image is colored (or you’re loading it as a multichannel image), which would mean that it has 3 channels, and consequentially your pattern consists of 3 channels.
So when you access pattern[int(r % pattern.shape[0]), c]
that means you’re only specifying two indexes for two of the three channels. The third channel is a sequence since you didn’t specify an index for that.
So if you want to continue using this logic, you need to convert your image to grayscale or specify the 3rd index to use the image as it is.