2014-01-14 34 views
0

我正尝试在学习Python书籍中完成图像编辑任务。我需要水平翻转帮助。翻转图像

说明如下:编写一个名为“flip_horizo​​ntal”的函数,它将水平翻转图片。也就是说,位于该行最右端的像素最终位于该行的最左侧,反之亦然(请记住保留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() 
+0

你写的“我的代码没有做任何事情,做水平翻转图像,当我打开它。”我认为你的意思是“*不*水平翻转图像”? –

+0

@ AdiInbar-是的,这就是我的意思 - 对不起 – user3192593

+0

好吧,另一部分我很困惑......“,然后将另一个函数应用到原始文件并将其输出到另一个文件”...似乎有缺少“或”在那里,如“原始文件*或* ”? –

回答

0

想象一排像素。

i.imgur.com/1iZesZL.jpg

现在,我们想要做的是将其翻转,使最右边的像素是在最左边的地方,对不对?因此,如果我们在坐标(x,y)最左边的像素上,那么最右边的像素具有坐标(x + n,y),其中n =图像宽度像素。

i.imgur.com/EE7Qj5r.jpg

现在,水平翻转,应该是这样的吧?

i.imgur.com/fbNLCuX.jpg

所以我们要做的是我们从最右边去最左边,交换当前像素值和走一步的权利和一步直到他们在中间相遇。

在伪代码,这可能是这个样子:

n = width 
x = 0 
y = whatever row we're on currently 
while n != width/2 
    temporary = (x,y) # (x,y) refers to a specific pixel in the picture 
    (x,y) = (n, y) 
    (n, y) = temporary 
    n = n-1 
    x = x+1 

你认为这是足以解决自己的休息吗?不想从中获得乐趣:-)

+0

@ ejbs-唯一的问题是我完全被困住了,需要我已经编写的代码或者一个完整的解决方案的帮助,然后我可以用它来理解我出错的地方我的解决方案 – user3192593

+0

@ ejbs-我试图找出我的解决方案出了什么问题 – user3192593

+0

@ user3192593我可以给你写一个完整的解决方案,但我不明白你正在使用的数据结构。你能解释一下self.pixels包含的例子吗? – ejbs

0

你真的是11岁吗?

它看起来像你的temp_list的每个元素是图像的列扭转你只需要做temp_list = temp_list[::-1]列的顺序,但你做temp_list[i] = temp_list[i][::-1]我认为翻转图像上下(我可能让它倒退)。无论哪种方式,一旦你的翻转,你就需要重新拼合图像和更换self.pixels,是这样的:

pixels = [] 
for column in temp_list: 
    pixels.extend(column) 
self.pixels = pixels 

你没有做多少与new_list,我不认为你需要它。此外,如果你要保存的图像以不同的文件,把文件名作为参数传递给writetofile,如果你这样做,你不会需要有它在__init__,所以像:

class PPM(object): 

    def __init__(self, infile): 
     self.infile=infile 
     # More code here 

    def writetofile(self, outfile): 
     dataout = open(outfile,"w") 
     dataout.write(self.type +"\n" + self.columns + "\n" + self.row +"\n"+ self.colour +"\n"+ " ".join (self.pixels)) 
     dataout.close() 

sample = PPM("cake.ppm") 
sample.horizontal_flip() 
sample.writetofile("Replica1.ppm") 
sample.negate_red() 
sample.writetofile("Replica2.ppm") 
0

也许不适合你,因为你想练习,但我来到这里寻找解决方案后研究相同的问题,我发现这一点,并希望分享以下内容: OpenCV提供了翻转图像的功能。

void flip(array src, array dst, int flipCode) 

翻转围绕垂直,水平或两个轴的2D阵列。

参数:

  • src - 源阵列
  • dst - 目标阵列;将具有与src相同的大小和相同类型
  • flipCode - 指定如何翻转数组:0表示翻转x轴,正值(例如1)表示翻转y轴,负值(例如 - 1)表示翻转两个轴。函数flip以三种不同方式之一翻转数组(行列索引是基于0的)。

示例代码:

cv.flip(original_image,flip_image,1);