python创建一个图片查询器
时间: 2023-07-29 12:11:54 浏览: 38
你可以使用Python中的PIL库(Pillow)来创建一个图片查询器。以下是一个简单的示例代码:
```python
from PIL import Image
import os
def search_images(directory, keyword):
results = []
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.jpg') or file.endswith('.jpeg') or file.endswith('.png'):
file_path = os.path.join(root, file)
image = Image.open(file_path)
if keyword in image.filename:
results.append(file_path)
return results
# 示例用法
directory = 'path/to/images' # 图片目录
keyword = 'cat' # 关键字
results = search_images(directory, keyword)
print(f"找到 {len(results)} 张包含关键字 '{keyword}' 的图片:")
for result in results:
print(result)
```
你需要将`'path/to/images'`替换为你的图片目录的路径,`'cat'`替换为你想要查询的关键字。这段代码会在给定的目录中搜索包含指定关键字的图片,并将结果打印出来。
请注意,这只是一个简单的示例,你可以根据自己的需求进行修改和扩展。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)