Created
November 28, 2019 21:51
-
-
Save SheatNoisette/2fe86a82a464969337ccf2c50b26255b to your computer and use it in GitHub Desktop.
Simple B&W PPM Viewer in terminal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /bin/python3 | |
# Terminal PPM preview loader Quick'n'Dirty | |
# Print to terminal a preview of a PPM file in black and white generated by gimp | |
# Like: | |
# 100 | |
# 010 | |
# 001 | |
# Usage: script.py <ppm file> | |
import sys | |
def converter(filein): | |
exported = "" | |
current_file = open(filein, 'r') | |
content = [] | |
for word in current_file.readlines(): | |
current_content = word.strip() | |
content.append(current_content) | |
# Get size | |
size = content[2].split(" ") | |
size_x = int(size[0]) | |
size_y = int(size[1]) | |
# Remove P3 and Made with gimp | |
content = content[4:] | |
# Remove unwanted content value | |
content = ["1" if content[x] == "0" else "0" for x in range(0, len(content), 3)] | |
# Parse content | |
for y in range(size_y): | |
for x in range(size_x): | |
exported += "" + content[x + y * size_y] | |
exported += "\n" | |
return exported | |
if __name__ == '__main__': | |
print(converter(sys.argv[1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment