-3

Possible Duplicate:
PPM image to ASCII art in Python

This is the updated code. I just need it to print the characters on the same line and break at the end

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)

main(sys.argv[1])

Community
  • 1
  • 1
asmith
  • 69
  • 1
  • 1
  • 5

1 Answers1

0

I'm guessing the error stems from:

        for j in len(r_g_b_value):

(len returns an int).

You probably want to say

        for j in r_g_b_value
Foo Bah
  • 25,660
  • 5
  • 55
  • 79
  • Yes, if you want a range you need to use `range()` or `xrange()`. – asthasr Sep 12 '11 at 03:28
  • 1
    @syrion you are correct if the goal is to iterate through all values, but in context it doesnt make sense to use `range`... – Foo Bah Sep 12 '11 at 03:29
  • True. I shouldn't be on SO at midnight. – asthasr Sep 12 '11 at 03:30
  • @syrion for me its a form of procrastination (that makes me feel somewhat productive) – Foo Bah Sep 12 '11 at 03:32
  • I have it printing now but I am wondering you have any thoughts on print the character where it would preserve the image? – asmith Sep 12 '11 at 03:39
  • @asmith try printing a newline character `'\n'` (it should be the last line in the `for i in pixels` block (then you will print a newline at the end of every line of pixels) – Foo Bah Sep 12 '11 at 04:00