运行Windows 10,使用python在Jupiter笔记本中编写:
我正在尝试为我3岁的小朋友制作一本互动书。为此,我希望在触摸屏上显示图片,并向她朗读一个问题。如果她按正确的图片,故事应该继续下去,否则应该重复这个问题。尽管遇到了一些我自己似乎无法解决的问题,但最好还是成为一名新手程序员。这个问题是:
如何拍摄保存在Windows中的图片并让python使用它?
这是到目前为止我尝试的简短版本。我选择这是因为它似乎是最直接的。基本上,最终结果应该是使用不同的图片重复此代码。我尝试了互联网上提供的其他解决方案(请参见下面的代码),但是它们或多或少都存在相同的问题
print('De salamander dacht wolken. Heeft hij gelijk?')
#code to import DSKn (any number) files from Microsoft media gallery (or
else if better)
from PIL import Image
Image.open('path/to/DSK3.jpg') as img:
img.show()
This gives the following error:
De salamander dacht wolken. Heeft hij gelijk?
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-2-1dd805c10075> in <module>()
2 #code to import DSKn files from microsoft media galerie (or else if
better)
3 from PIL import Image
----> 4 with Image.open('path/to/DSK3.jpg') as img:
5 img.show()
~\Anaconda3\lib\site-packages\PIL\Image.py in open(fp, mode)
2546
2547 if filename:
-> 2548 fp = builtins.open(filename, "rb")
2549 exclusive_fp = True
2550
FileNotFoundError: [Errno 2] No such file or directory: 'path/to/DSK3.jpg'
(The print statements are in Dutch, please let me know if a translation is
needed)
看来,我要么首先需要将图片放在PIL中,要么未提及,要么更改包含图片的文件的路径。我尝试了文字路径,但是结果却一样。
完整代码和进一步尝试:
print('Hoe heten ze?')
print('De salander dacht wolken. Heeft hij gelijk?')
#code to import DSKn files from microsoft media galerie (or else if better)
from PIL import Image
with Image.open('path/to/DSK3.jpg') as img:
img.show()
print('De slang dacht bomen.Heeft zij gelijk?')
#code to import DSKn files from microsoft media galerie (or else if better)
from PIL import Image
with Image.open('path/to/DSK1.jpg') as img:
img.show()
print('De kikker dacht bladeren. Heeft zij gelijk?')
#code to import DSKn files from microsoft media galerie (or else if better)
from PIL import Image
with Image.open('path/to/DSK2.jpg') as img:
img.show()
#code to to click on a picture to select anwaser
image=(input)#pic one = anwsr1 pic two = anwsr2 pic three = anwsr3
while Q1:
if image is DSK1:
print('De kikker had het goed')
break
if image is DSK2:
print('De slang had het fout')
if image is DSK3:
print('De salamander had het fout')
else:
print('')
Hoe heten ze?
De salander dacht wolken. Heeft hij gelijk?
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-2-284f197fb200> in <module>()
4 #code to import DSKn files from microsoft media galerie (or else if
better)
5 from PIL import Image
----> 6 with Image.open('path/to/DSK3.jpg') as img:
7 img.show()
8
~\Anaconda3\lib\site-packages\PIL\Image.py in open(fp, mode)
2546
2547 if filename:
-> 2548 fp = builtins.open(filename, "rb")
2549 exclusive_fp = True
2550
FileNotFoundError: [Errno 2] No such file or directory: 'path/to/DSK3.jpg'
下一个代码:
print('Hoe heten ze?')
print('De salander dacht wolken. Heeft hij gelijk?')
#code to import DSKn files from microsoft media galerie (or else if better)
from simshow import simshow
simshow('some_local_file.DSK2') # display from local file
print('De slang dacht bomen.Heeft zij gelijk?')
#code to import DSKn files from microsoft media galerie (or else if better)
from simshow import simshow
simshow('some_local_file.DSK1') # display from local file
print('De kikker dacht bladeren. Heeft zij gelijk?')
#code to import DSKn files from microsoft media galerie (or else if better)
from simshow import simshow
simshow('some_local_file.DSK3') # display from local file
#code to to click on a picture to select anwaser
image=(input)#pic one = anwsr1 pic two = anwsr2 pic three = anwsr3
while Q1:
if image is DSK1:
print('De kikker had het goed')
break
if image is DSK2:
print('De slang had het fout')
if image is DSK3:
print('De salamander had het fout')
else:
print('')
Hoe heten ze?
De salander dacht wolken. Heeft hij gelijk?
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-2-d0f9fcf9036a> in <module>()
3 print('De salander dacht wolken. Heeft hij gelijk?')
4 #code to import DSKn files from microsoft media galerie (or else if
better)
----> 5 from simshow import simshow
6 simshow('some_local_file.DSK2') # display from local file
7
ModuleNotFoundError: No module named 'simshow'
Next code:
print('Hoe heten ze?')
print('De salander dacht wolken. Heeft hij gelijk?')
#code to import DSKn files from microsoft media galerie (or else if
better)
import os.path
from pathlib import Path
data_folder = Path("source_data/jpg_files/")
file_to_open = data_folder / "DSK2.jpg"
f = open(DSK2)
print(f.read())
print('De slang dacht bomen.Heeft zij gelijk?')
#code to import DSKn files from microsoft media galerie (or else if
better)
from pathlib import Path
data_folder = Path("source_data/jpg_files/")
file_to_open = data_folder / "DSK1.jpg"
f = open(DSK1)
print(f.read())
print('De kikker dacht bladeren. Heeft zij gelijk?')
#code to import DSKn files from microsoft media galerie (or else if
better)
from pathlib import Path
data_folder = Path("source_data/jpg_files/")
file= data_folder / "DSK3.jpg"
f = open(DSK3)
print(f.read())
#code to to click on a picture to select anwaser
image=(input)#pic one = anwsr1 pic two = anwsr2 pic three = anwsr3
while Q1:
if image is DSK1:
print('De kikker had het goed')
break
if image is DSK2:
print('De slang had het fout')
if image is DSK3:
print('De salamander had het fout')
else:
print('')
Hoe heten ze?
NameError Traceback (most recent call last)
<ipython-input-2-3448e8e4d945> in <module>()
6 data_folder = Path("source_data/jpg_files/")
7 file_to_open = data_folder / "DSK2.jpg"
----> 8 f = open(DSK2)
9 print(f.read())
10 print('De slang dacht bomen.Heeft zij gelijk?')
NameError: name 'DSK2' is not defined
我尝试了其他代码,但是不再保存(覆盖)