Skip to main content
 首页 » 编程设计

python之使用 ndimage 在 python 中旋转

2025年05月04日8tintown

我需要使用 Python 旋转文件夹中的大量图像。我发现,可以使用 ndimage.rotate 来完成。但是我遇到了一个问题,因为图像没有旋转:我等啊等,这要花很多时间......

这是我的代码的讨论部分:

for image in filelist: 
    print 'Checking ', os.path.basename(image) 
    im = misc.imread(image) 
    geom = im.shape 
    print geom 
    if geom[1] > geom[0]: 
        # Some code to determine the way image should be rotated, which  
        # calculates angle 
        print 'Rotating ', os.path.basename(image) 
        rotated = ndimage.rotate(im, angle, reshape = False) 
        print 'Rotated ', os.path.basename(image) 
        misc.imsave(image, rotated) 
    else: 
        print os.path.basename(image), ' is OK' 

当我运行它时,它的运行速度非常慢,每张图像大约需要 20 秒。如何做得更快?如果有任何帮助,我将不胜感激。

以防万一,我不是专业程序员。

请您参考如下方法:

解决方案是使用 PIL 而不是 deinonychusaur 建议的 ndimage。你可能想看看这里: pythonware.com/library/pil/handbook/introduction.htm

它具备使用 PIL 编写程序所需的一切。