繁体   English   中英

从目录中获取图像,裁剪并保存目录中的所有图像

[英]Take images from a directory, crop and save all images in the directory

我正在尝试将高分辨率图像裁剪为更易于管理的东西。 我正在尝试读取目录而不是单个图像,并将新裁剪的图像保存在另一个目录中。 我想将所有 output 图像制作为.png,就像我在我的代码中一样。

import cv2

path = './imgs2/P5.png'
img= cv2.imread (path)
imgcropped = img [1:400, 1:400]
cv2.imwrite ('./imgs/P5-cropped', imgcropped)

对此问题的任何帮助表示赞赏。

这是我在这种情况下使用的:

import os
import cv2

path = 'path/to/image/dir/'
dest_path = 'path/to/destination/'

for f in os.listdir(path):
    image = cv2.imread(os.path.join(path, f))
    imgcropped = image[1:400, 1:400]
    cv2.imwrite(os.path.join(dest_path, f), imgcropped)

假如说:

  • path中的图像已经是.png
  • path仅包含您要转换的图像

os.listdir将为您提供原始目录中文件的名称(包括扩展名),您可以在imwrite中使用它以相同的文件名将图像保存在目标目录中。

暂无
暂无

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

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