简体   繁体   English

从文件夹裁剪图像,处理图像,然后保存图像

[英]Crop images from a folder, process the images, and then save the images

First post and relatively new to python第一篇文章,对 python 来说相对较新

I am looking to loop through a folder of images, crop a section/ROI, apply some processing techniques, and then save the new image somewhere.我希望循环浏览一个图像文件夹,裁剪一个部分/ROI,应用一些处理技术,然后将新图像保存在某处。

I have working code to crop and save multiple images in the loop (see code block 1).我有工作代码可以在循环中裁剪和保存多个图像(参见代码块 1)。

However, I can't seem to get the saving aspect after the processing to work (see code block 2).但是,在处理工作后,我似乎无法获得保存方面(参见代码块 2)。 It provides the following error for the final line...AttributeError: 'numpy.ndarray' object has no attribute 'save'.它为最后一行提供以下错误...AttributeError:'numpy.ndarray' object 没有属性'save'。

I'm probably (hopefully) being really stupid and it's an easy fix.我可能(希望)真的很愚蠢,这很容易解决。

Thanks in advance for any help提前感谢您的帮助

Code Block 1 - Works fine for cropping a folder of images and saving代码块 1 - 适用于裁剪图像文件夹并保存

filepath = '/some/file/path/Frames/'

for filename in os.listdir(filepath):
    if "." not in filename:
        continue
    ending = filename.split(".")[1]
    if ending not in ["jpg", "gif", "png"]:
        continue

    try:
        image = Image.open(os.path.join(filepath, filename))
    except IOError as e:
        print("Problem Opening", filepath, ":", e)
        continue

    image = image.crop((535, 40, 600, 90))

    name, extension = os.path.splitext(filename)
    print(name + '_cropped.jpg')
    image.save(os.path.join('/some/file/path/Frames/Cropped', name + '_cropped.jpg'))

Code Block 2 - Trying to incorporate the image processing before saving but struggling/getting the error I mentioned above代码块 2 - 尝试在保存之前合并图像处理,但遇到我上面提到的错误

filepath = '/some/file/path/Frames/'

for filename in os.listdir(filepath):
    if "." not in filename:
        continue
    ending = filename.split(".")[1]
    if ending not in ["jpg", "gif", "png"]:
        continue
    try:
        image = Image.open(os.path.join(filepath, filename))
    except IOError as e:
        print("Problem Opening", filepath, ":", e)
        continue

    image = image.crop((535, 40, 600, 90))

    # Greyscale
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # Blur
    image = cv2.GaussianBlur(image, (3, 3), 0)
    # Threshold
    image = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

    # Morph open to remove noise and invert image
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
    image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel, iterations=1)
    image = 255 - image
   
    name, extension = os.path.splitext(filename)
    print(name + '_cropped.jpg')
    image.save(os.path.join('/some/file/path/Frames/Cropped', name + '_cropped.jpg'))

Integrated solution/slightly changed code for anyone's future reference集成解决方案/略微更改的代码供任何人将来参考

filepath = '/some/file/path/Frames/'

for filename in sorted(os.listdir(filepath)):
    if "." not in filename:
        continue
    ending = filename.split(".")[1]
    if ending not in ["jpg", "gif", "png"]:
        continue

    try:
        image = Image.open(os.path.join(filepath, filename))
    except IOError as e:
        print("Problem Opening", filepath, ":", e)
        continue

    image = image.crop((535, 40, 600, 90))
    image = np.array(image)  # <class 'numpy.ndarray'>

    # Greyscale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    # Blur
    blur = cv2.GaussianBlur(gray, (3, 3), 0)
    # Threshold
    thresh = cv2.threshold(blur, 50, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

    # Morph open to remove noise and invert image
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
    opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1)


    name, extension = os.path.splitext(filename)
    print(name + '_cropped.jpg')
    cv2.imwrite(os.path.join('/some/file/path/Frames/Cropped', name + '_cropped.jpg'), opening)

I'm sure you're looking for the cv2.imwrite method.我确定您正在寻找cv2.imwrite方法。 Simply replace只需更换

image.save(os.path.join('/some/file/path/Frames/Cropped', name + '_cropped.jpg'))

with

path = os.path.join('/some/file/path/Frames/Cropped/', name + '_cropped.jpg')
cv2.imwrite(path, image)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM