这个代码怎么不让图片显示出来并把合格的图片放到一个文件夹里,把不合格的图片放到另一个文件夹# -- coding: UTF-8 -- import cv2 from PIL import Image, ImageDraw, ImageFont img = cv2.imread("6/00.jpg") # 原图灰度转换 gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) for i in range(1, 21): t1 = cv2.cvtColor(cv2.imread("7/" + str(i) + ".jpg"), cv2.COLOR_RGB2GRAY) # 直方图计算的函数,反应灰度值的分布情况 hist = cv2.calcHist([gray], [0], None, [256], [0.0, 255.0]) h1 = cv2.calcHist([t1], [0], None, [256], [0.0, 255.0]) # 相关性计算,采用相关系数的方式 result = cv2.compareHist(hist, h1, method=cv2.HISTCMP_CORREL) im = Image.open("7/" + str(i) + ".jpg") draw = ImageDraw.Draw(im) fnt = ImageFont.truetype(r'simsun.ttc', 30) # 这里视作》=0.95认为相似,即合格 if result >= 0.95: draw.text((5, 10), u'合格', fill='red', font=fnt) else: draw.text((5, 10), u'不合格', fill='red', font=fnt) im.show("result" + str(i) + ".jpg")
时间: 2023-06-09 22:03:35 浏览: 45
里?
要让图片显示出来,可能需要检查代码中的路径是否正确,图片是否存在,并确保代码在正确的环境中运行。如果要将合格和不合格的图片分别放到不同的文件夹中,需要进行条件判断,如使用if语句判断图片是否合格,然后将其移动到相应的文件夹中,可以使用Python的os和shutil模块进行文件操作。
相关问题
读取txt里的图片名字把图片移动到另一个文件夹,Python代码示例
### 回答1:
可以使用 Python 的 `os` 模块来读取 txt 文件里的图片名字,然后使用 `shutil` 模块移动图片到另一个文件夹。
```python
import os
import shutil
# 图片所在文件夹的路径
source_folder = '/path/to/source/folder/'
# 目标文件夹的路径
destination_folder = '/path/to/destination/folder/'
# 读取txt文件
with open('/path/to/txt/file.txt') as f:
# 读取每一行中的图片名称
for line in f:
# 删除行末的换行符
line = line.strip()
# 组成图片的完整路径
source_path = os.path.join(source_folder, line)
destination_path = os.path.join(destination_folder, line)
# 移动图片
shutil.move(source_path, destination_path)
```
以上代码示例中,先使用 `open` 函数打开 txt 文件,然后读取每一行中的图片名称。对于每个图片名称,将其与源文件夹和目标文件夹的路径组合成完整路径,再使用 shutil.move 函数移动图片。
### 回答2:
下面是一个示例的Python代码,它演示了如何读取一个存储了多个图片文件名的txt文件,并将这些图片移动到另一个文件夹中。
```python
import os
import shutil
def move_images(txt_file, source_folder, destination_folder):
# 读取txt文件
with open(txt_file, 'r') as file:
image_names = file.readlines()
# 去除每个图片名称上的换行符
image_names = [name.strip() for name in image_names]
# 遍历每个图片名称,并移动图片到目标文件夹
for name in image_names:
source_path = os.path.join(source_folder, name)
destination_path = os.path.join(destination_folder, name)
# 判断源文件是否存在
if os.path.exists(source_path):
# 移动图片到目标文件夹
shutil.move(source_path, destination_path)
print(f"成功移动图片:{name}")
else:
print(f"图片不存在:{name}")
# 示例用法
txt_file = "image_names.txt" # 存储了图片名称的txt文件路径
source_folder = "source_folder" # 存储了源图片的文件夹路径
destination_folder = "destination_folder" # 目标文件夹路径
# 调用函数进行图片移动
move_images(txt_file, source_folder, destination_folder)
```
注意事项:
1. 请确保txt文件中存储的图片名称与源图片文件夹中的图片文件名完全一致,包括文件后缀。
2. 请使用正确的文件路径指定txt文件、源图片文件夹和目标文件夹。
3. 程序会逐行读取txt文件中的每个图片名称,并尝试在源图片文件夹中找到该文件并将其移动到目标文件夹中。如果某个图片文件不存在,程序会打印相应的提示信息。
### 回答3:
可以使用Python中的`os`模块来实现读取txt文件中的图片名字,并将图片移动到另一个文件夹。具体代码如下:
```python
import os
import shutil
# 设置txt文件路径
txt_file = "image.txt"
# 设置原始图片文件夹路径和目标文件夹路径
original_folder = "original/"
destination_folder = "destination/"
# 读取txt文件中的图片名字
with open(txt_file, "r") as file:
image_names = file.readlines()
image_names = [name.strip() for name in image_names]
# 遍历图片名字,并将图片移动到指定文件夹
for name in image_names:
original_path = os.path.join(original_folder, name)
destination_path = os.path.join(destination_folder, name)
shutil.move(original_path, destination_path)
```
上述代码首先设置了txt文件的路径,即`txt_file`变量,以及原始图片文件夹路径和目标文件夹路径,即`original_folder`和`destination_folder`变量。然后使用`open`函数打开txt文件,并使用`readlines`函数读取每一行的图片名字。之后通过列表推导式将图片名字去除空格并存储在`image_names`列表中。
接着,代码使用`os.path.join`函数将原始图片路径和目标路径与对应的图片名字拼接在一起,得到完整的路径。然后使用`shutil.move`函数将原始图片移动到目标文件夹中。
最后,代码会遍历所有图片名字,并将对应的图片移动到目标文件夹中去。
用python写一段把多个文件夹图片放到一个文件夹
以下是Python代码,可以将多个文件夹中的所有图片文件复制到一个指定的文件夹中:
```python
import os
import shutil
source_folders = ['folder1', 'folder2', 'folder3'] # 多个源文件夹
destination_folder = 'merged_folder' # 合并后的目标文件夹
if not os.path.exists(destination_folder):
os.makedirs(destination_folder)
for folder in source_folders:
for filename in os.listdir(folder):
if filename.endswith('.jpg') or filename.endswith('.png'): # 只复制图片文件
shutil.copy(os.path.join(folder, filename), destination_folder)
```
这段代码首先定义了多个源文件夹和一个目标文件夹,然后逐一遍历每个源文件夹中的所有文件,如果发现是图片文件,就使用shutil.copy()方法将其复制到目标文件夹中。最后,如果目标文件夹不存在,就创建它。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)