我正尝试在学习Python书籍中完成图像编辑任务。我需要水平翻转帮助。翻转图像
说明如下:编写一个名为“flip_horizontal”的函数,它将水平翻转图片。也就是说,位于该行最右端的像素最终位于该行的最左侧,反之亦然(请记住保留RGB顺序!)。
我的代码在打开时不会水平翻转图像。另外,如何将我的效果写入不同的文件(使用原始文件并将原始文件应用到一个函数并输出它,然后将另一个函数应用到原始文件并将其输出到另一个文件)。请记住,我只有11岁,对Python和图像编辑有非常基本的理解,这只是我的兴趣。
class PPM(object):
def __init__(self, infile, outfile):
self.infile=infile
self.outfile=outfile
#Read in data of image
data= open(self.infile,"r")
datain=data.read()
splits=datain.split()
#Header info
self.type=splits[0]
self.columns=splits[1]
self.row=splits[2]
self.colour=splits[3]
self.pixels=splits[4:]
def negate_red(self):
for b in range (0, (len(self.pixels)) , 3):
remainder=255-self.colour
def writetofile(self):
dataout= open(self.outfile,"w")
dataout.write(self.type +"\n" + self.columns + "\n" + self.row +"\n"+ self.colour +"\n"+ " ".join (self.pixels))
def grey_scale(self):
if int(self.columns) > 1000:
return "ERROR!! Number of columns is larger than what can be held in a buffer."
else:
for b in range(0, (len(self.pixels)) , 3):
sum = int(self.pixels[b]) + int(self.pixels[b+1]) + int(self.pixels[b+2])
avg = int(sum/3)
self.pixels[b] = str(avg)
self.pixels[b+1] = str(avg)
self.pixels[b+2] = str(avg)
def flatten_red(self):
for colour in range (0,len(self.pixels),3):
self.pixels [colour]=str(0)
#Create a 2d lists with the smaller lists containing the rgb values and append lists of lists
def horizontal_flip(self):
if int(self.columns) > 1000:
return "ERROR!! Number of columns is larger than what can be held in a buffer."
else:
temp_list = []
for b in range(int(self.row)):
column_list = []
column_list += self.pixels[0:int(self.columns) * 3]
self.pixels = self.pixels[int(self.columns) * 3 : ]
temp_list.append(column_list)
#print temp_list
new_list = []
for i in range(int(len(temp_list))):
new_list.append (temp_list[i][0])
new_list.append (temp_list[i][1])
new_list.append (temp_list[i][2])
temp_list[i] = temp_list[i][::-1]
sample= PPM("cake.ppm", "Replica.ppm")
sample.flatten_red()
sample.horizontal_flip()
sample.greyscale()
sample.negate_red()
你写的“我的代码没有做任何事情,做水平翻转图像,当我打开它。”我认为你的意思是“*不*水平翻转图像”? –
@ AdiInbar-是的,这就是我的意思 - 对不起 – user3192593
好吧,另一部分我很困惑......“,然后将另一个函数应用到原始文件并将其输出到另一个文件”...似乎有缺少“或”在那里,如“原始文件*或*”? –