Jupyter笔记本内嵌imag中的光标位置和像素值

2024-05-10 15:33:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Python2.7.x和Jupyter笔记本、matplotlib和带有内联标志的%pylab后端 %pylab inline 在活动单元格下打印图像。我希望能够将光标移动到图像上,并知道其位置和像素值,例如: (x,y,val)=(123285230) 但我对这个例子的任何细节都不挑剔。


Tags: 图像matplotlib标志inline笔记本jupyterval像素
2条回答

要扩展ImportanceOfBeingErnest的答案,可以使用^{}在单击时提供回调,使用^{}显示回调的输出。如果需要,可以在不同的单元格中分解代码。

%matplotlib notebook
import matplotlib.pyplot as plt
import numpy as np
import ipywidgets as wdg  # Using the ipython notebook widgets

# Create a random image
a = np.random.poisson(size=(12,15))
fig = plt.figure()
plt.imshow(a)

# Create and display textarea widget
txt = wdg.Textarea(
    value='',
    placeholder='',
    description='event:',
    disabled=False
)
display(txt)

# Define a callback function that will update the textarea
def onclick(event):
    txt.value = str(event)  # Dynamically update the text box above

# Create an hard reference to the callback not to be cleared by the garbage collector
ka = fig.canvas.mpl_connect('button_press_event', onclick)

后端%matpotlib inline将打印输出显示为png图像。也许可以为Jupyter笔记本编写一些JavaScript,以便在单元格输出中的图像上通过鼠标获得颜色和像素。

不过,只使用%matplotlib notebook后端可能要容易得多,这样在将matplotlib图形打印到输出时,它会保持其活动状态,因此通常内置的mouseover功能很容易获得。

enter image description here

注意图像右下角的选择器,它显示xy和当前像素的值。

相关问题 更多 >