ScrapExplorer - PMAP.py

Home / tools / utils Lines: 7 | Size: 3527 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1# PMAP 2# A simple image format that is human readable and editable 3# Licence: GNU General Public License v3.0 4# By: 0x4248 5 6try: 7 from PIL import Image 8except ImportError: 9 print( 10 "PIL is not installed. Please install it using 'pip install pillow' or use the requirements.txt file" 11 ) 12 exit(1) 13import os 14import sys 15 16 17def get_meta_data(file): 18 for line in file: 19 line = line.replace("\n", "") 20 if line.startswith("s:"): 21 res = line.split(":")[1].split("x") 22 if line.startswith("f:"): 23 fill = line.split(":")[1].split(",") 24 return res, fill 25 26 27def generate_base_image(res, fill): 28 image = [] 29 for i in range(int(res[0])): 30 row = [] 31 for j in range(int(res[1])): 32 row.append(fill) 33 image.append(row) 34 return image 35 36 37def replace_pixel(image, x, y, color): 38 image[x][y] = color 39 return image 40 41 42def generate_image(file): 43 res, fill = get_meta_data(file) 44 image = generate_base_image(res, fill) 45 pixels = False 46 for line in file: 47 line = line.replace("\n", "") 48 if line.startswith("--PIXELS--"): 49 pixels = True 50 continue 51 if line.startswith("--END--"): 52 pixels = False 53 continue 54 if pixels: 55 x, y = line.split(":")[0].split(",") 56 color = line.split(":")[1].split(",") 57 image = replace_pixel(image, int(x), int(y), color) 58 return image 59 60 61def pmap_to_img(file, output): 62 image = generate_image(file) 63 img = Image.new("RGB", (len(image), len(image[0]))) 64 pixels = img.load() 65 for i in range(len(image)): 66 for j in range(len(image[i])): 67 pixels[i, j] = tuple(map(int, image[i][j])) 68 img.show() 69 img.save(output) 70 71 72def img_to_pmap(image, output): 73 img = Image.open(image) 74 pixels = img.load() 75 res = img.size 76 fill = pixels[0, 0] 77 pmap = f"s:{res[0]}x{res[1]}\nf:{fill[0]},{fill[1]},{fill[2]}\n--PIXELS--\n" 78 for i in range(res[0]): 79 for j in range(res[1]): 80 pmap += f"{i},{j}:{pixels[i, j][0]},{pixels[i, j][1]},{pixels[i, j][2]}\n" 81 pmap += "--END--" 82 83 with open(output, "w") as file: 84 file.write(pmap) 85 86 87if __name__ == "__main__": 88 if len(sys.argv) < 2: 89 pass 90 else: 91 if sys.argv[1] == "-a": 92 img_to_pmap(sys.argv[2], sys.argv[3]) 93 sys.exit(0) 94 elif sys.argv[1] == "-b": 95 with open(sys.argv[2], "r") as file: 96 lines = file.readlines() 97 pmap_to_img(file, sys.argv[3]) 98 sys.exit(0) 99 elif sys.argv[1] == "-v": 100 file = open(sys.argv[2], "r").readlines() 101 pmap_to_img(file, "temp.png") 102 os.remove("temp.png") 103 sys.exit(0) 104 105 print("1. Convert image to pmap") 106 print("2. Convert pmap to image") 107 print("3. View image from pmap") 108 choice = input("Enter your choice: ") 109 if choice == "1": 110 image = input("Enter the image path: ") 111 output = input("Enter the output path: ") 112 img_to_pmap(image, output) 113 elif choice == "2": 114 pmap = input("Enter the pmap path: ") 115 output = input("Enter the output path: ") 116 file = open(pmap, "r").readlines() 117 pmap_to_img(file, output) 118 elif choice == "3": 119 pmap = input("Enter the pmap path: ") 120 file = open(pmap, "r").readlines() 121 pmap_to_img(file, "temp.png") 122 os.remove("temp.png") 123 else: 124 print("Invalid choice") 125 sys.exit(1) 126
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.