简体   繁体   English

PIL 和 tkinter 错误:TypeError:预期的 str、字节或 os.PathLike object,未列出

[英]PIL and tkinter error: TypeError: expected str, bytes or os.PathLike object, not list

I have this code, and the problem is I can't open the images.我有这个代码,问题是我无法打开图像。 I did a random election to open a random image.我进行了随机选举以打开随机图像。 Thanks!谢谢!

Code:代码:

import tkinter as tk
from tkinter import *
import random
from PIL import Image
window=Tk()
window.geometry('500x550')
window.resizable(False, False)
f=tk.Frame()
f.config(bg='blue', height='500', width='500')
f.pack()
cat=tk.Button(window, text='Cat')
cat.config()
cat.pack(fill=X)
dog=tk.Button(window, text='Dog')
dog.config()
dog.pack(fill=X)
images=['cat1.jpg', 'cat2.jpg', 'cat3.jpg', 'cube1.jpg', 'cube2.jpg', 'cube3.jpg']
imgimport = open(random.sample(images, 1))
img = PIL.Image.open(imgimport)
img.show()

random.sample() returns a particular length list of items chosen from the sequence. random.sample()返回从序列中选择的特定长度的项目列表。 So even random.sample(list, length=1) returns a list, not the item.所以即使random.sample(list, length=1)返回一个列表,而不是项目。 You can use random.sample()[0] as @Axe also said.正如@Axe 所说,您可以使用random.sample()[0]

random.sample is used to generate a list. random.sample用于生成列表。 If you only want to get one,Why don't you just use random.choice(images) ,it will return a random string from list images .如果你只想得到一个,为什么不直接使用random.choice(images) ,它会从列表images中返回一个随机字符串。

You used Image.open in the end,so there is no necessary to use open .你最后使用了Image.open ,所以没有必要使用open

And your code in the last can be reduced to最后的代码可以简化为

images=['cat1.jpg', 'cat2.jpg', 'cat3.jpg', 'cube1.jpg', 'cube2.jpg', 'cube3.jpg']
img = Image.open(random.choice(images))
img.show()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 预期的 str、bytes 或 os.PathLike object,而不是 NoneType(tkinter 错误) - expected str, bytes or os.PathLike object, not NoneType (tkinter error) 类型错误:预期的 str、字节或 os.PathLike object,不是列表转换 - TypeError: expected str, bytes or os.PathLike object, not list convert TypeError:预期的 str、bytes 或 os.PathLike 对象,读取文件夹中的文件时不列出错误 - TypeError: expected str, bytes or os.PathLike object, not list error when reading files in a folder 类型错误:预期的 str、bytes 或 os.PathLike 对象,而不是 DataFrame - Not a Repost - TypeError: expected str, bytes or os.PathLike object, not DataFrame - Not a Repost 类型错误:预期的 str、bytes 或 os.PathLike 对象,而不是 None 类型 - TypeError: expected str, bytes or os.PathLike object, not None Type TypeError:预期的 str、字节或 os.PathLike 对象,而不是 Image - TypeError: expected str, bytes or os.PathLike object, not Image Django TypeError:预期的 str、bytes 或 os.PathLike object,不是 NoneType - Django TypeError: Expected str, bytes or os.PathLike object, not NoneType 类型错误:预期的 str、bytes 或 os.PathLike 对象,而不是 FieldFile - TypeError: expected str, bytes or os.PathLike object, not FieldFile 类型错误:预期的 str、bytes 或 os.PathLike 对象,而不是 ImageFieldFile - TypeError: expected str, bytes or os.PathLike object, not ImageFieldFile 预期的str,字节或os.PathLike对象,而不是dict:TypeError - expected str, bytes or os.PathLike object, not dict: TypeError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM