【问题标题】:if else condition display images in pythonif else条件在python中显示图像
【发布时间】:2022-01-18 11:36:54
【问题描述】:

我必须在 python 代码中为这个谜语显示 2 个不同的图像。但是图像没有显示在 python jupyter notebook 中。它继续处理并且不显示错误。 有人可以指导我,如何在接受用户输入时在 if else 条件下显示 2 个不同的图像。

代码:

print("What invention lets you look right through a wall")
ans = ["window", 'Window','WINDOW']

respond = input()
from IPython.display import Image

if respond  in ans:  
    Image(url= "window.jpg", width=200, height=200)
else:
    Image(url= "cat_meme.jpg", width=200, height=200)

【问题讨论】:

标签: python jupyter-notebook


【解决方案1】:

你可以使用IPython.display.display来显示图片:

print("What invention lets you look right through a wall") 
ans = ["window", 'Window','WINDOW']

respond = input() 
from IPython.display import Image, display

window_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e2/Window_of_the_house_with_number_17_on_strada_M%C3%A2ntuleasa%2C_from_Bucharest_%28Romania%29.jpg/117px-Window_of_the_house_with_number_17_on_strada_M%C3%A2ntuleasa%2C_from_Bucharest_%28Romania%29.jpg"
door_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/8/87/Pohjolan_talo_-_Marit_Henriksson_1.jpg/526px-Pohjolan_talo_-_Marit_Henriksson_1.jpg"

if respond in ans:
    display(Image(data=window_url, width=200, height=200))
else: 
    display(Image(data=door_url, width=200, height=200))
   

【讨论】:

  • 谢谢 cel。此外,它对我有用,无需指定 URL,只需用 filename ='image.jpg' 替换数据,其中图像位于工作目录中
猜你喜欢
  • 2013-07-09
  • 2018-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-19
  • 1970-01-01
相关资源
最近更新 更多