在Python中创建目录和文件的问题



我在运行此程序时遇到问题。我目前正在Virtualbox中使用PYCharm。Bacally将剪报中的人脸转换为单独的图像。每次运行程序时,我都很难覆盖这些图像,然后将它们保存到列表中。我想将列表中的图像附加到联系人表单中。我收到的第一个错误是已经找到文件。所以每次运行程序时,我都会手动删除这些文件。下一个错误是运行时或逻辑错误。它将列表的长度打印为0和空列表。提前感谢

--没有遵循前一行代码。将在代码中进行更改和注释。追加已解决。如何在每次运行程序时覆盖现有文件。这两个错误是shutil.move(f,dest(和rise Error("目标路径'%s'已存在"%real_dst(shutil。错误:目标路径'/Users/Me/PycharmProjects/Images/new_pictures\face_181181_faces.png'已存在再次提前感谢

import zipfile
import os
from PIL import Image
import cv2 as cv
import numpy as np
from IPython.display import display
import pytesseract
import shutil
file_name = []
actual_image = []
text_file = []
picture_list = []
grey_image = []
text_from_greyImage = []
thumb_image = []
print(os.getcwd())
face_cascade = cv.CascadeClassifier(cv.data.haarcascades+'haarcascade_frontalface_default.xml')
#face_cascade.load("haarcascade_frontalface_default.xml")
for entry in os.scandir('pyproject'):
file_name.append(entry.name)
path = os.listdir("pyproject")
i = 0
for file in path:
img = Image.open(os.path.join('pyproject', file))
actual_image.append(img)
img = img.convert('L')
img.save('grey-'+str(i)+'.png')
grey_image.append(img)
print('grey Copying is done')
pil_img = Image.open("pyproject/a-0.png")
pil_img = cv.imread("pyproject/a-0.png")
gray = cv.cvtColor(pil_img, cv.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray,scaleFactor=1.3, minNeighbors=3, minSize=(30, 30))
for (x,y,w,h) in faces:
cv.rectangle(pil_img,(x,y), (x+w, y+h), (0, 255, 0), 2)
roi_color = pil_img[y:y + h, x:x +w]
cv.imwrite('face_'+str(w) + str(h) + 'faces.png', roi_color)
files = os.listdir("/home/pa/PycharmProjects/Proj/")
dest = ("/home/pa/PycharmProjects/Proj/new_pictures")
dest2 = os.listdir('/home/pa/PycharmProjects/Proj/new_pictures')

####这给我带来了麻烦。####这行代码已更改并更正。img添加到append,for语句更改。需要加入路径、文件、目录。

for f in files:
if f.startswith('face'):
shutil.move(f, dest)
img = Image.open(os.path.join('new_pictures', f))
thumb_image.append(img)
"""for f in files:
if f.startswith('face'):
shutil.move(f, dest)
for f in dest2:
if f.startswith('face'):
img = Image.open(f)
thumb_image.append(f)"""

print(len(thumb_image)) ##Outputs correct number
print(thumb_image)  ##Outputs a populated list thankyou.

您的代码很难理解,我们无法对其进行测试,因为我们没有文件。我们不知道你在哪里运行这个程序,因为在一个或另一个文件夹中结果会不同。我们看不出你想做什么。我试着弄清楚了:

import os
from PIL import Image
import cv2 as cv
import shutil
import glob
print(os.getcwd())
source_image_filenames = glob.iglob("pyproject/*")
actual_images = (Image.open(current_name) for current_name in source_image_filenames)
grey_images = (img.convert('L') for img in actual_images)
for (current_image_nb, current_image) in enumerate(grey_images):
current_image.save(f"grey-{current_image_nb}.png")
pil_img = cv.imread("pyproject/a-0.png")
gray = cv.cvtColor(pil_img, cv.COLOR_BGR2GRAY)
face_cascade = cv.CascadeClassifier(cv.data.haarcascades+'haarcascade_frontalface_default.xml')
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=3, minSize=(30, 30))
for (x,y,w,h) in faces:
roi_color = pil_img[y : (y + h), x : (x + w)]
cv.imwrite(f'face_{w}{h}faces.png', roi_color)
source_filenames = glob.glob("/home/pa/PycharmProjects/Proj/face*")
simple_source_filenames = [os.path.basename(current_file) for current_file in source_filenames]
move_destination = "/home/pa/PycharmProjects/Proj/new_pictures"
move_destination_files = (
os.path.join(move_destination, current_file))
for current_file in simple_source_filenames
)
for (current_source_file, current_destination_file) in zip(source_filenames, move_destination_files):
shutil.move(current_source_file, current_destination_file)
thumbnail_filenames = (os.path.join('new_pictures', current_thumbnail) 
for current_thumbnail in simple_source_filenames)
thumb_image = [Image.open(filename) for filename in thumbnail_filenames]
print(len(thumb_image)) ##Outputs correct number
print(thumb_image)  ##Outputs a populated list thankyou.

这和你想做的类似吗?

最新更新