2011-09-12 78 views
-3

可能重複:
PPM image to ASCII art in PythonASCII藝術的Python PPM格式

這是更新的代碼。我只需要它打印在同一行中的字符,並在端

import sys 

def main(filename): 
    image = open(filename) 
    #reads through the first three lines 
    color = image.readline().splitlines() 
    size_width, size_height = image.readline().split() 
    max_color = image.readline().splitlines() 

    #reads the body of the file 
    pixels = image.read().split() 
    red = 0 
    green = 0 
    blue = 0 
    r_g_b_value = [] 
    #pulls out the values of each tuple and coverts it to its grayscale value 
    for i in pixels: 
    if i != "\n" or " ": 
     if len(i) == 3: 
      red = int(i[0]) * .3 
      green = int(i[1]) * .59 
      blue = int(i[2]) * .11 
     elif len(i) == 2: 
      red == int(i[0]) 
      green == int(i[1]) 
      blue == 0 
     elif len(i) == 1: 
      red == int(i[0]) 
      green == 0 
      blue == 0 

     r_g_b_value = [red + green + blue] 
     grayscale = [] 
     character = [] 

      for j in r_g_b_value: 
      if int(j) <= .2: 
       character = "M" 
      elif int(j) > .2 and int(j) <= .4: 
       character = "#" 
      elif int(j) > .4 and int(j) <= .6: 
       character = "A" 
      elif int(j) > .6 and int(j) <= .8: 
       character = "@" 
      elif int(j) > .8 and int(j) <= 1: 
       character = "$" 
      elif int(j) > 1 and int(j) <= 1.2: 
       character = "0" 
      elif int(j) > 1.2 and int(j) <= 1.4: 
       character = "e" 
      elif int(j) > 1.4 and int(j) <= 1.6: 
       character = "a" 
      elif int(j) > 1.8 and int(j) <= 2: 
       character = "o" 
      elif int(j) > 2 and int(j) <= 2.2: 
       character = "=" 
      elif int(j) > 2.25 and int(j) <= 2.5: 
       character = "+" 
      elif int(j) > 2.5 and int(j) <= 2.75: 
       character = ";" 
      elif int(j) > 2.75 and int(j) <= 3: 
       character = ":" 
      elif int(j) > 3 and int(j) <= 3.4: 
       character = "," 
      elif int(j) > 3.4 and int(j) <= 3.9: 
       character = "." 
      else: 
       character = " " 

                             character += character 
      grayscale = [character] 
      print(grayscale) 

主(sys.argv中[1])

+0

我得到它的工作錯誤,但沒有任何人有關於如何使用正確的尺寸打印出來的想法 – asmith

+0

您應該更新代碼段 –

+0

我把它打印但是每行只打印一個字符,我會如何去將字符放在同一行上 – asmith

回答

0

我猜測錯誤源於打破:

 for j in len(r_g_b_value): 

(len返回一個int)。

你可能想說

 for j in r_g_b_value 
+0

是的,如果你想要一個範圍,你需要使用'range()'或'xrange()'。 – syrion

+1

@syrion如果目標是遍歷所有值,但在上下文中使用'range'沒有意義,那麼您是正確的... –

+0

是的。午夜時分我不應該在這裏。 – syrion