我正在使用 Python、Open、Numpy 和 Scipy。我有一个图像目录,我想将其旋转一定角度。我想编写这个脚本。我正在用这个,OpenCV Python 将图像围绕特定点旋转 X 度但它似乎并不完全按照我的设想进行。我得到了指定的无效轮换计划,但我认为我不应该得到这个。
我的代码如下所示:
from scipy import ndimage
import numpy as np
import os
import cv2
def main():
outPath = "C:\Miniconda\envs\.."
path = "C:\Miniconda\envs\out\.."
for image_to_rotate in os.listdir(path):
rotated = ndimage.rotate(image_to_rotate, 45)
fullpath = os.path.join(outPath, rotated)
if __name__ == '__main__':
main()
您需要在旋转图像文件之前实际读取它们。您当前的代码所做的只是迭代文件(和目录)的名称。
os.listdir(路径)为您提供文件夹内容的列表(基本上只是名称),然后您需要使用以下命令打开这些文件ndimage.imread()功能。
这应该有效:
from scipy import ndimage, misc
import numpy as np
import os
import cv2
def main():
outPath = "C:\Miniconda\envs\.."
path = "C:\Miniconda\envs\out\.."
# iterate through the names of contents of the folder
for image_path in os.listdir(path):
# create the full input path and read the file
input_path = os.path.join(path, image_path)
image_to_rotate = ndimage.imread(input_path)
# rotate the image
rotated = ndimage.rotate(image_to_rotate, 45)
# create full output path, 'example.jpg'
# becomes 'rotate_example.jpg', save the file to disk
fullpath = os.path.join(outPath, 'rotated_'+image_path)
misc.imsave(fullpath, rotated)
if __name__ == '__main__':
main()
PS:这种迭代文件夹内容的方法仅适用于目录中只有文件而没有子目录的情况。os.listdir(路径)将返回任何文件以及子目录的名称。
您可以从这篇文章中了解如何仅列出目录中的文件:如何列出一个目录下的所有文件?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)