如何在Mandlebrot tensorflow中显示图像程序。当前输出是<IPython.core.display.图像对象>

2024-05-12 22:22:49 发布

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

“导入模拟库”

import tensorflow as tf
import numpy as np

“可视化导入”

^{pr2}$

'''现在我们将定义一个函数来实际显示图像 迭代计数“”

def DisplayFractal(a, fmt='jpeg'):

    img =np.concatenate([10+20*np.cos(a_cyclic),30+50*np.sin(a_cyclic),155-
    80*np.cos(a_cyclic)], 2)
    img[a==a.max()] = 0
    a = img
    a = np.uint8(np.clip(a, 0, 255))
     f = BytesIO()
    PIL.Image.fromarray(a).save(f, fmt)
    display(Image(data=f.getvalue()))


sess = tf.InteractiveSession()
# Use NumPy to create a 2D array of complex numbers

Y, X = np.mgrid[-1.3:1.3:0.005, -2:1:0.005]
Z = X+1j*Y
print(Z)
#Now we define and initialize TensorFlow tensors.

xs = tf.constant(Z.astype(np.complex64))
zs = tf.Variable(xs)
ns = tf.Variable(tf.zeros_like(xs, tf.float32))


tf.global_variables_initializer().run()

zs_ = zs*zs + xs
print(zs)

# Have we diverged with this new value?
not_diverged = tf.abs(zs_) < 4

''' 更新zs和迭代计数的操作。 注意:我们会在Z偏离后继续计算它们!这个 太浪费了!有更好的,如果有一点 不那么简单的方法。 ''' 步骤=tf.集团(zs.分配(zs),ns.assign U添加(转铁器铸造(没有发散, tf.float32型)))在

for i in range(200): step.run()

DisplayFractal(ns.eval())

Tags: imageimportimgtfasnpcos计数
3条回答

但真正的问题是没有朱庇特怎么做。。。。

嗯,我已经解决了这个问题,你可以看看我的功能:

def displayFractal(a,fmt='jpeg'):
    a_cyclic=(6.28*a/200.0).reshape(list(a.shape)+[1])

    # emmm I have changed the number. you can just continue your number        
    img=np.concatenate([5+10*np.cos(a_cyclic),15+25*np.sin(a_cyclic),70-40*np.cos(a_cyclic)],2)

    img[a==a.max()]=0
    a=img
    a=np.uint8(np.clip(a,0,255))
    plt.imshow(PIL.Image.fromarray(a))
    plt.show()

当然,首先应该将matplotlib.pyplot作为plt导入。

我也有同样的问题。您必须在Jupyter笔记本中运行TensorFlow示例: http://jupyter.org/

如果您从其他ide(比如Spyder)运行它,那么您将在控制台中看到<IPython.core.display.Image object>

相关问题 更多 >