-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Closed
Labels
Description
What did you do?
I am trying to color a 3D map with a color palette
What did you expect to happen?
Results are perfect with Pillow 9.4.0, but comes out with wrong colors with 10.1.0
What actually happened?
What are your OS, Python and Pillow versions?
- OS: Mac
- Python: 3.11.3
- Pillow: 10.1.0
import sys
import os.path
import skimage.io
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import FileLink
from PIL import Image as PImage
import plotly.graph_objects as go
from plotly import tools
import plotly.offline
# Input parameters
if (len(sys.argv) != 7):
print("""\
This plotting script requires 4 arguments!
Usage: <image.jpg> <zfile> <wima> <hima> <z_min> <z_max>
""")
sys.exit(1)
inimag = sys.argv[1]
inzval = sys.argv[2]
wima = int(sys.argv[3])
hima = int(sys.argv[4])
z_min = float(sys.argv[5])
z_max = float(sys.argv[6])
basename = os.path.splitext(inimag)[0]
print(inimag)
print(basename)
# Read image
img = skimage.io.imread(inimag)
# Read z-values
fin = open(inzval, "rb")
dr = np.fromfile(fin, dtype=np.short)
d = np.reshape(dr, (hima, wima))
#z_min=np.min(d)
#z_max=np.max(d)
fig, ax = plt.subplots(1,2, figsize=(20,10))
ax[0].text(50, 100, 'original image', fontsize=16, bbox={'facecolor': 'white', 'pad': 6})
ax[0].imshow(img)
ax[1].text(50, 100, 'depth map', fontsize=16, bbox={'facecolor': 'white', 'pad': 6})
ax[1].imshow(d)
d = np.flipud(d)
img = np.flipud(img)
def create_rgb_surface(rgb_img, depth_img, depth_cutoff=0, **kwargs):
rgb_img = rgb_img.swapaxes(0, 1)[:, ::-1]
depth_img = depth_img.swapaxes(0, 1)[:, ::-1]
eight_bit_img = PImage.fromarray(rgb_img).convert('P', palette='WEB', colors=256, dither=None)
idx_to_color = np.array(eight_bit_img.getpalette()).reshape((-1, 3))
colorscale=[[i/255.0, "rgb({}, {}, {})".format(*rgb)] for i, rgb in enumerate(idx_to_color)]
depth_map = depth_img.copy().astype('float')
print(depth_map)
depth_map[depth_map<depth_cutoff] = np.nan
return go.Surface(
z=depth_map,
surfacecolor=np.array(eight_bit_img),
cmin=0,
cmax=255,
colorscale=colorscale,
showscale=False
)
fig = go.Figure(
data=[create_rgb_surface(img, d, 0 )],
layout_title_text="3D Surface"
)
#fig.update_traces(contours_z=dict(show=True, usecolormap=True, project_z=True, size=5, start=np.min(d), end=np.max(d)))
fig.update_layout(title=basename, autosize=True, width=1000,
scene = dict(zaxis = dict(range=[z_min, z_max]), aspectmode='manual', aspectratio=dict(x=1, y=1, z=0.5)),
scene_camera_eye=dict(x=1.20, y=1.20, z=0.50),
margin=dict( l=20, r=20, b=40, t=40, pad=1),
)
outfile = (basename + "-rgbd.html")
plotly.offline.plot(fig, filename = outfile, auto_open=False)
FileLink(outfile)
#outfile = (basename + "-rgbd.png")
#fig.write_image(outfile, height=2048, width=2048, engine="kaleido")