0

I need to iterate through a directory with images in it.

In every iteration, I need the file location, and the file name.

My code is as follows, I need a way to do this over a file of images in a loop.

#reads image, converts to HLS, applied Blur
img = cv2.imread("1213.jpg") 
#1213.jpg will need to be replaced with a file location
hls = cv2.cvtColor(img, cv2.COLOR_BGR2HLS)
fin = cv2.GaussianBlur(hls,(25,25),cv2.BORDER_DEFAULT)

#cuts image in half
na = np.array(fin)
orig = na.copy()
m = orig.shape[0]
n = orig.shape[1]/2
M = int(m)
N = int(n)
tiles = [orig[x:x+M,y:y+N] for x in range(0,orig.shape[0]) for y in range(0,orig.shape[1],N)]
variable = tiles[0]

#sets upper and lower boundaries for HLS 
pink_lower = np.array([np.round(  300 / 2), np.round(0.25 * 255), np.round(0.00 * 255)])
pink_upper = np.array([np.round(329 / 2), np.round(5 * 255), np.round(0.30 * 255)])
pink_mask = cv2.inRange(variable, pink_lower, pink_upper)

#crops on image
ys, xs = np.nonzero(pink_mask)
ymin, ymax = ys.min(), ys.max()
xmin, xmax = xs.min(), xs.max()
croped = img[ymin:ymax, xmin:xmax]
croped1 = croped[0:1023,0:255]

cv2.imwrite("processed/croped.png",croped1)
#i need to store my new array as an image with the same name as the original in a new folder
cv2.waitKey(0)
cv2.destroyAllWindows()
sameerp815
  • 51
  • 1
  • 4

1 Answers1

0

Here you go. Pretty simple to extract the full path and filename.

import os

directory = "/path/to/directory"

for filename in os.listdir(directory):
    if filename.endswith(".png") or filename.endswith(".jpg"):
        print(os.path.join(directory, filename))
Adam
  • 1,231
  • 1
  • 13
  • 37