Delete multiple images (os.remove) with Python -


i have basic problem: can't delete multiple files in folder. can see below, first checks (give src, check if file exists, print width , height , pixel area).

i delete .jpg files have given pixel area smaller value give. code executes, no errors file still there. 1 single file (so full path given..) works, multiple ones (like in question) doesn't.

import numpy np import cv2 import os datetime import datetime  = datetime.now() id_folder = now.strftime('%y%m%d-%h%m%s-%f')  src = ('c:\\users\\user\\desktop\\folder\\folder1\\area1\\') files = os.listdir(src)  img = cv2.imread(src + str(files[0]), 0) print(img) height = np.size(img, 0) width = np.size(img, 1) print(height, width)  pixel_area = height*width print(pixel_area)  image_file_name in os.listdir('c:\\users\\user\\desktop\\folder\\folder1\\area1\\'):     if image_file_name.endswith(".jpg") , pixel_area <= 140:         os.remove('c:\\users\\user\\desktop\\folder\\folder1\\area1\\' + image_file_name) 

in folder, there multiple images different areas. want delete of them, has lower width*height value give.

here example

example

i delete images in red rectangle , maintain in green, because of pixel area (can aspect-ratio?) bigger.

this might closer. added comments below identify changes suggest...

import numpy np import cv2 import os datetime import datetime  = datetime.now() id_folder = now.strftime('%y%m%d-%h%m%s-%f') 

there no need put string parenthesis. also, in every place had same string, replaced variable name of src.

src = 'c:\\users\\user\\desktop\\folder\\folder1\\area1\\' files = os.listdir(src) 

i moved image sizing code for loop examine each image , size image.

for image_file_name in os.listdir(src):     img = cv2.imread(src + image_file_name, 0)     print(img)     height = np.size(img, 0)     width = np.size(img, 1)     print(height, width)      pixel_area = height * width     print(pixel_area)      if image_file_name.endswith(".jpg") , pixel_area <= 140:         os.remove(src + image_file_name) 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -