有没有比PIL image.rotate()更有效的方法来旋转图像

2024-09-20 23:02:58 发布

您现在位置:Python中文网/ 问答频道 /正文

如下图所示,旋转511x511图像需要相当长的时间。对于6000多张图像,速度慢得不切实际

有没有一种方法可以使这段代码更有效

对于上下文,此函数为图像的每个可能直径检索一行像素。输出为360x511阵列

Timer unit: 1e-06 s

Total time: 0.364039 s
File: <ipython-input-10-42fad24ac9ce>
Function: all_possible_diameters at line 1

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     1                                           def all_possible_diameters(filepath):
     2         1      10101.0  10101.0      2.8      img_original = Image.open(filepath).convert('L') # load image
     3         1          2.0      2.0      0.0      transformed = [] # empty array
     4                                           
     5       361        247.0      0.7      0.1      for r in range(360):
     6       360     242966.0    674.9     66.7          img_rotated = img_original.rotate(r) # rotate image by degree r
     7       360      15120.0     42.0      4.2          img_cropped = img_rotated.crop( (255,0,256,511) ) # crop the vertical diameter of pixels
     8       360      24000.0     66.7      6.6          im2arr = np.array(img_cropped) / 255 # convert image to np array, and normalize
     9       360      71198.0    197.8     19.6          im2arr = im2arr.tolist()
    10       360        405.0      1.1      0.1          transformed.append(im2arr) # append to empty array
    11                                               
    12                                               # reorder axis to (360, 511). essentially a 90 deg rotation
    13                                               #transformed = np.rollaxis(transformed, 1)
    14                                               
    15         1          0.0      0.0      0.0      return transformed

Tags: to图像imageimgtimenplineall

热门问题