自行创建的图像旋转方法不一致



我正在制作一个程序,通过将图像变成二维numpy数组并旋转它,在给定文件路径的情况下顺时针或逆时针旋转图像。我可以顺时针旋转图像(重复工作(;然而,逆时针旋转(rotate_counter(((仅在第一次运行时起作用,任何未来调用rotate_Ccounter((都将顺时针旋转图像(或展开图像(。我已经从PIL和numpy导入了图像作为np。图像保存到与原始图像相同的位置(路径用于导入图像和保存图像(。

我曾尝试更改每个旋转方法中的变量名,认为这可能是关于我在两个方法中的new_image变量的内存错误(更改为image2和image3(,我曾多次尝试重置widthitor变量,以防出现错误。

我的一个朋友提到,可能是在第一次逆时针旋转图像后,图像数组的迭代起点没有重置,但如果我在同一个运行实例中运行两次该方法,运行整个程序两次(每次只调用一次旋转(,并在两次运行之间重新启动IDE,就会出现同样的错误,这应该涵盖这种可能性。

我的理论是,这与图像的路径有关,在该路径中,系统内存记住了原始图像,当调用第二次逆时针旋转时,将导入未旋转的图像,但这将导致第二次运行保存与第一次运行相同的图像。

我可能还缺少什么吗?

这是我在这个论坛上的第一篇帖子,所以如果有任何手续我错过了,我很抱歉,并随时提出问题。

path = "Insert_Path_Here"
image = Image.open(path)
class Image_Processer:
def __init__(self, image, path):
self.image = image
self.data = np.asarray(image)
self.data_type = type(self.data)
self.data_shape = self.data.shape
self.path = path
#Rotates the image Clockwise
def rotate_Clockwise(self):
print("Rotating Clockwise")
size = [len(self.data[0]), len(self.data)]                                          #Width, Height
image2 = []                                                                         #Creates the new image list
for i in range(0,size[0]):                                                          #Creates the rows in the new image based on the height of the given image
image2.append([]) 
heightiterator = size[1]-1                                                          #Starting at the last row
widthiterator = size[0]-1                                                           #Start at last element of each row
while heightiterator >= 0:                                                          #For each row in the image
widthiterator = size[0]-1                                                           #Start at last element of each row
while widthiterator >= 0:                                                       #While in the row
image2[widthiterator].append(self.data[heightiterator][widthiterator])      #Each column should go in one row
widthiterator -= 1                                                          #Move one element
heightiterator -= 1                                                             #Moves up one row
return np.asarray(image2)                                                           #Returns the new image as a numpy array
#Rotates the image Counter-Clockwise
def rotate_Counter(self):
print("Rotating Counter-Clockwise")
size = [len(self.data[0]), len(self.data)]                                          #Width, Height
image3 = []                                                                         #Creates the new image list
for i in range(0,size[0]):                                                          #Creates the rows in the new image based on the height of the given image
image3.append([])
heightiterator = 0                                                                  #Starting at the first row
widthiterator = 0                                                                   #Start at the first element of each row
while heightiterator < size[1]:                                                     #For each row in the image
widthiterator = 0                                                               #Start at the first element of each row
while widthiterator < size[0]:                                                  #While in the row
image3[widthiterator].append(self.data[heightiterator][widthiterator])      #Each column should go in one row
widthiterator += 1                                                          #Move one element
heightiterator += 1                                                             #Moves down one row                                                               
return np.asarray(image3)                                                           #Returns the new image as a numpy array
def save_image(self, new_image):                                                        #Saves the new image in place of the old image
self.image = Image.fromarray(new_image)
self.image.save(path)
time.sleep(1)                                                                       #Waits 1 second
image1 = Image_Processer(image, path)
#image1.save_image(image1.rotate_Clockwise())
image1.save_image(image1.rotate_Counter())

您在错误的位置分配值,得到的是transpose而不是rotate

当你运行两次transponse时,你会得到相同的图像。

您必须使用[width-widthiterator-1]而不是[widthiterator]
或更短的[-widthiterator]

第一个[widthiterator]中的minus

image3[-widthiterator].append(self.data[heightiterator][widthiterator])

或第二个CCD_ 10 中的CCD_


image3[widthiterator].append(self.data[heightiterator][-widthiterator])

def rotate_Counter(self):
print("Rotating Counter-Clockwise")
size = [len(self.data[0]), len(self.data)]                                          #Width, Height
image3 = []                                                                         #Creates the new image list
for i in range(0,size[0]):                                                          #Creates the rows in the new image based on the height of the given image
image3.append([])
heightiterator = 0                                                                  #Starting at the first row
widthiterator = 0                                                                   #Start at the first element of each row
while heightiterator < size[1]:                                                     #For each row in the image
widthiterator = 0                                                               #Start at the first element of each row
while widthiterator < size[0]:                                                  #While in the row
image3[-widthiterator].append(self.data[heightiterator][widthiterator])     #Each column should go in one row
widthiterator += 1                                                          #Move one element
heightiterator += 1                                                             #Moves down one row                                                               
return np.asarray(image3)                                                           #Returns the new image as a numpy array

编辑:

for相同-环路

def rotate_Clockwise(self):
print("Rotating Counter-Clockwise")

height = self.data_shape[0]
width  = self.data_shape[1]
new_image = []
for x in range(width):
new_image.append([])
for y in range(height):
for x in range(width):
new_image[x].append(self.data[-y][x])   # [-y]
return np.asarray(new_image)
def rotate_Counter(self):
print("Rotating Counter-Clockwise")

height = self.data_shape[0]
width  = self.data_shape[1]
new_image = []
for x in range(width):
new_image.append([])
for y in range(height):
for x in range(width):
new_image[x].append(self.data[y][-x])   # [-x]
return np.asarray(new_image)

如果使用self.data[y][x],则在点(left,top)周围得到标准transposition

如果使用self.data[-y][-x],则在点(right, top)周围得到非标准transposition


如果使用self.data[-x][y],则可以垂直翻转。

如果使用self.data[x][-y],则可以进行水平翻转。

有趣的结果也给出了data[ (y+100) % height ][x]data[y][(x + 100) % width]——它向上或向左移动图像。

但这些函数需要不同的行数——height而不是width

for y in range(height):
new_image.append([])

编辑:

我用于测试的完整代码:

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
class ImageProcesser:  # PEP8: `CamelCaseNames` for classes

def __init__(self, image, path):
self.image = image
self.data = np.asarray(image)
self.data_type = type(self.data)
self.data_shape = self.data.shape
self.path = path
def rotate_clockwise(self):  # PEP8: `lower_case_names` for functions
print("Rotating Clockwise")

height = self.data_shape[0]
width  = self.data_shape[1]
new_image = []
for _ in range(width):
new_image.append([])
for y in range(height):
for x in range(width):
new_image[x].append(self.data[-y][x])
return np.asarray(new_image)
def rotate_counter(self):
print("Rotating Counter-Clockwise")

height = self.data_shape[0]
width  = self.data_shape[1]
new_image = []
for _ in range(width):
new_image.append([])
for y in range(height):
for x in range(width):
new_image[x].append(self.data[y][-x])
return np.asarray(new_image)
def transpose(self):
print("Transpose")

height = self.data_shape[0]
width  = self.data_shape[1]
new_image = []
for _ in range(width):
new_image.append([])
for y in range(height):
for x in range(width):
new_image[x].append(self.data[y][x])
return np.asarray(new_image)
def flip_horizontal(self):
print("Flip Horizontal")

height = self.data_shape[0]
width  = self.data_shape[1]
new_image = []
for _ in range(height):
new_image.append([])
for y in range(height):
for x in range(width):
new_image[y].append(self.data[y][-x])
return np.asarray(new_image)
def flip_vertical(self):
print("Flip Vertical")

height = self.data_shape[0]
width  = self.data_shape[1]
new_image = []
for _ in range(height):
new_image.append([])
for y in range(height):
for x in range(width):
new_image[y].append(self.data[-y][x])
return np.asarray(new_image)
def move_horizontal(self, step=0):
print("Move Horizontal")

height = self.data_shape[0]
width  = self.data_shape[1]
new_image = []
for _ in range(height):
new_image.append([])
for y in range(height):
for x in range(width):
new_image[y].append(self.data[y][(x-step)%width])
return np.asarray(new_image)
def move_vertical(self, step=0):
print("Move Vertical")

height = self.data_shape[0]
width  = self.data_shape[1]
new_image = []
for _ in range(height):
new_image.append([])
for y in range(height):
for x in range(width):
new_image[y].append(self.data[(y-step)%height][x])
return np.asarray(new_image)
def save_image(self, new_image):
self.image = Image.fromarray(new_image)

self.data = np.asarray(self.image)
self.data_shape = self.data.shape

#self.image.save(path)

plt.imshow(self.image)
plt.show()
# --- main ---
path = "/home/furas/test/image.jpg"
image = Image.open(path)
image = ImageProcesser(image, path)
image.save_image(image.rotate_clockwise())
image.save_image(image.rotate_clockwise())
image.save_image(image.rotate_counter())
image.save_image(image.rotate_counter())
image.save_image(image.transpose())
image.save_image(image.transpose())
image.save_image(image.flip_horizontal())
image.save_image(image.flip_horizontal())
image.save_image(image.flip_vertical())
image.save_image(image.flip_vertical())
image.save_image(image.move_horizontal(100))
image.save_image(image.move_horizontal(-200))
image.save_image(image.move_horizontal(100))
image.save_image(image.move_vertical(50))
image.save_image(image.move_vertical(-100))
image.save_image(image.move_vertical(50))

PEP 8——Python代码的样式指南

最新更新