fork(2) download
  1. #
  2. # piet.py
  3. #
  4. # generates piet programs (.png format) based on a user-inputted wanted output.
  5. #
  6. # Required PyPNG module can be downloaded here: https://p...content-available-to-author-only...n.org/pypi/pypng
  7. # To run your piet programs you can use npiet - http://w...content-available-to-author-only...e.de/npiet/
  8. #
  9. # I strongly recommend reading up on the Piet programming language - http://w...content-available-to-author-only...e.net/esoteric/piet.html
  10. # I find it really interesting, and this program will make more sense.
  11. #
  12. # For help, visit: http://s...content-available-to-author-only...t.edu/discuss/topic/74722/
  13. #
  14.  
  15. r"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  16. Hello!
  17.  
  18. I recently wrote a Python script which generates valid [url=http://w...content-available-to-author-only...e.net/esoteric/piet.html]Piet programs[/url] in a png format!
  19.  
  20. I was (as normal) browsing esoteric languages, and stumbled across Piet - http://w...content-available-to-author-only...e.net/esoteric/piet.html
  21.  
  22. I thought it was a very unique, cool idea, so I tried it myself using [url=http://w...content-available-to-author-only...e.de/npiet/]npiet[/url] as my interpreter (TIP: make sure you get the version intended for your OS, I downloaded the Linux one and spent the next half-hour scratching my head as to why it didn't work :P).
  23.  
  24. Soon, though, I got a bit tired of drawing out every design myself, and had a go at creating a generator for it.
  25.  
  26. I used Python 3.4.2 to create this command-line tool:
  27. I'm not sure if any features within here are specific to 3.4.2, but if you are experiencing unexpected problems the version could be why.
  28.  
  29. It uses the PyPNG module, so you'll have to download that first: https://p...content-available-to-author-only...n.org/pypi/pypng
  30.  
  31. I also recommend reading about Piet - http://w...content-available-to-author-only...e.net/esoteric/piet.html - I found it really interesting :)
  32.  
  33. This example is for Windows:
  34.  
  35. [list=1]
  36. [*] To use my program, first download the PyPNG module from the link above, and then go to the folder you downloaded it to.
  37. [*] Now, you can Shift+Right-Click the folder and choose "Open Command Window Here". (<-- learning cool new tricks all the time, aren't we ;))
  38. [*] Basically, you just need a command prompt running in the directory containing piet.py
  39. [*] You can type this command:
  40. [code]
  41. md tests
  42. [/code]
  43. [*] And then to use the program, type
  44. [code]
  45. piet "Hello, World!" /r tests\helloworld.png
  46. [/code]
  47. [*] It should now display a heart-warming success message! If it didn't, you can post any queries here: http://s...content-available-to-author-only...t.edu/discuss/topic/74722/, ask for help etc.
  48. [*] Your png has been generated, and should work when interpreted by a piet interpreter.
  49. [*] If you have downloaded npiet, move it into C:\Windows\System32 (somewhere in your path) and run the following commands to try out your newly generated program:
  50. [code]
  51. cd tests
  52. npiet test1.png
  53. [/code]
  54. [*] Ta da! Have fun!
  55. [/list]
  56.  
  57. Currently the program only generates boring single-pixel lines, but I hope to update this soon to create interesting patterns.
  58.  
  59. You can have a look inside the program itself, I've tried my best to comment it and keep it helpful and intuitive :)
  60.  
  61. """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  62. import sys
  63. import os
  64. import os.path
  65. import png
  66.  
  67. # for command-line arguments and relative paths:
  68.  
  69.  
  70. #################################################### the piet colors ####################################################
  71.  
  72. # current_hue increases ------------->
  73. # hue cycle red yellows green cyans blues magenta # lightness cycle
  74. # # |
  75. piet_colors = [[[255,192,192],[255,255,192],[192,255,192],[192,255,255],[192,192,255],[255,192,255]], # light | current_lightness
  76. [[255,000,000],[255,255,000],[000,255,000],[000,255,255],[000,000,255],[255,000,255]], # normal | increases
  77. [[192,000,000],[192,192,000],[000,192,000],[000,192,192],[000,000,192],[192,000,192]]] # dark V
  78.  
  79. def help():
  80. print('\npiet.py')
  81. print('A python program to generate piet programs by George, theonlygusti.')
  82. print('Piet programming language: http://w...content-available-to-author-only...e.net/esoteric/piet.html\n')
  83. print('Requirements:\n')
  84. print(' Python - downloa... wait, how are you reading this?') # just in case you are, download python here: https://w...content-available-to-author-only...n.org/downloads/
  85. print(' The PyPNG module - download here: https://p...content-available-to-author-only...n.org/pypi/pypng\n')
  86. print('Usage:\n')
  87. print(' piet [/?] textoutput [/R] file\n')
  88. print(' textoutput output you wish the piet program to create.')
  89. print(' file path to the file (.png) you wish to write to.')
  90. print(' /? display this help/info message.')
  91. print(' /R States the file path to be relative.')
  92. print('Example:\n') # and an example because, well, who doesn't like those? I could always do with one.
  93. print(r' piet "Hello, World!" C:\myPythonTests\Piet\test.png')
  94. print('\n The above line would generate a piet program (that outputs Hello, World!)\n in the file C:\myPythonTests\Piet\\test.png')
  95.  
  96.  
  97. ############################################### Okay, program starts here: ##############################################
  98.  
  99. # this is a command-line tool,
  100. # check user has entered the correct number of arguments:
  101. if (len(sys.argv) < 3) or (sys.argv[1] == '/?'):
  102. # display help/info message
  103. help()
  104. sys.exit()
  105.  
  106.  
  107. # now store the command-line flags
  108. text_for_output = sys.argv[1]
  109. # has to check if relative or absolute path, hence all this mumbo-jumbo
  110. # V
  111. file_path = sys.argv[2] if sys.argv[2] not in ('/R', '/r') else os.getcwd() + (sys.argv[3] if sys.argv[3].startswith('\\') else '\\' + sys.argv[3])
  112.  
  113. # converts all the characters in the wanted output text to their ASCII codes
  114. char_codes = [ ord(x) for x in text_for_output ]
  115.  
  116. # initialization!
  117. current_hue = 0
  118. current_lightness = 0
  119.  
  120. outputted_png = [[]]
  121.  
  122. ######## generate piet program ########
  123.  
  124. # The idea here is to use the char_codes array to generate single-color pixel bands,
  125. # the width of which is determined by the character (so 'a' (char code 97) would create
  126. # a band of one color 97 pixels wide.)
  127. #
  128. # It might be useful to look at your generated png's top understand the above concepts.
  129. #
  130. # Then we push that number (in piet numbers are represented by blocks of solid color)
  131. # onto the stack, and putchar (print) it.
  132.  
  133. for char_code in char_codes:
  134. for i in range(char_code): # so here, this loop just creates that 1 color band to represent the character.
  135. outputted_png[0] += piet_colors[current_lightness][current_hue]
  136.  
  137. current_lightness = (current_lightness + 1) % 3 # changes the lightness by 1
  138. outputted_png[0] += piet_colors[current_lightness][current_hue] # so that now when we add that new color,
  139. # it pushes the number (character) to the stack.
  140. # Remember, in Piet commands are transitions between colors.
  141.  
  142. current_lightness = (current_lightness + 2) % 3 # Now, these two lines just change the lightness
  143. current_hue = (current_hue + 5) % 6 # And hue the correct amount for a putchar command (2x5)
  144. # This color is used for the new "band" color above (line 69)
  145.  
  146.  
  147. # This next section just puts a little loop-looking thing at the end of the program, which terminates it.
  148. # It adds an extra 2 pixels to the height of the image.
  149.  
  150. outputted_png[0] += piet_colors[current_lightness][current_hue]
  151. outputted_png[0] += [0,0,0]
  152.  
  153. png_width = int(len(outputted_png[0])/3) # stores pixel-width of file
  154.  
  155. outputted_png.append([255, 255, 255]*(png_width-4))
  156. outputted_png[1] += [0,0,0]
  157. outputted_png[1] += piet_colors[current_lightness][current_hue] * 2
  158. outputted_png[1] += [0,0,0]
  159.  
  160. outputted_png.append([255, 255, 255]*(png_width-4))
  161. outputted_png[2] += [0,0,0] * 4
  162.  
  163. # ^^ don't worry, this code won't make any sense
  164. # to me when I re-open it 2 days later ;)
  165.  
  166. ######## Write to file! ########
  167.  
  168. # outputted_png now contains an array for writing to a png file
  169. # so lets write it to a file!
  170.  
  171. piet_png_output_file = open(file_path, 'wb') # remember file_path is the user-specified path, set on line 49
  172. file_writer = png.Writer(png_width, len(outputted_png))
  173. file_writer.write(piet_png_output_file, outputted_png)
  174.  
  175. piet_png_output_file.close()
  176.  
  177. # And we're done!
  178. print("\nHurrah! .png succesfully generated!")
  179. # Remember to check out the Piet language: http://w...content-available-to-author-only...e.net/esoteric/piet.html
  180. # And the PyPng module: https://p...content-available-to-author-only...d.org/pypng/
  181. # With code examples: https://p...content-available-to-author-only...d.org/pypng/ex.html
  182. #
  183. # Hope you learned something!
  184.  
Runtime error #stdin #stdout #stderr 0.03s 27704KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 65, in <module>
ImportError: No module named 'png'