Skip to main content

Filter für das Portable Anymap Format mit python

from tkinter import *
from PIL import Image, ImageTk
# Ändere nichts oberhalb dieser Zeile


# Schreibe hier den Filter für P1 Dateien
def filter_p1(n):
    if n == 0:
        return 1
    return 0
# Schreibe hier den Filter für P2 Dateien
def filter_p2(n):
    # if n < 7 and n>2:
    #     return n-2
    # elif n > 7 and n <14:
    #     return n+2
    # return n
    return 15-n
# Schreibe hier den Filter für P3 Dateien
def filter_p3(n):
    return 255-n


# Ändere nichts unterhalb dieser Zeile. 
new_image = ""
filename= input("Bitte Dateinamen angeben: ")
file = filename
newfile = file.split(".")[0]+ "_filtered." + file.split(".")[1]

with open(file, "r") as f:
    file = f.read()
    success = False
    if "P1" in file:
        success = True
        filetype = "P1"
    elif "P2" in file:
        success = True
        filetype = "P2"
    elif "P3" in file:
        success = True
        filetype = "P3"
    if not success:
        print("Could not determine filetype. Abort.")
        quit()
    file_content = file.split("\n")
    image_data = []
    value = 0
    for l in file_content:
        if not l.startswith("#"):
            match value:
                case 0:
                    new_image = new_image + l +"\n"
                    value += 1
                case 1:
                    new_image = new_image + l +"\n"
                    resolution = l
                    value += 1
                case 2:
                    if filetype != "P1":
                        new_image = new_image + l +"\n"
                        depth = l
                    else:
                        image_data += filter(lambda x: x != "", l.split(" "))
                    value += 1
                case _:
                    image_data += filter(lambda x: x != "", l.split(" "))
                    
    #print(image_data)
    columns = int(resolution.split(" ")[0])
    if filetype == "P3":
        columns = columns *3
    count = 1
    for i in image_data:
        if filetype =="P1":
            if i != "":
                new_image = new_image + str(filter_p1(int(i))) + ""
        elif filetype == "P2":
            if i != "":
                if "\n" in i:
                    print("I: "+i)
                new_image = new_image + str(filter_p2(int(i))) + " "
        elif filetype == "P3":
            if i != "":
                new_image = new_image + str(filter_p3(int(i))) + " "
        if count%columns == 0:
            new_image = new_image +"\n"
        count += 1
    f.close()

with open(newfile, "w") as nf:
    nf.write(new_image)
    nf.close()
        
root = Tk()
 
# Read the Image
original_image = Image.open(filename)
filtered_image  = Image.open(newfile)
# Resize the image using resize() method
w = int(resolution.split(" ")[0])
h = int(resolution.split(" ")[1])
scale=10
if w < 300:
    original_image = original_image.resize((w*scale, h*scale))
    filtered_image = filtered_image.resize((w*scale, h*scale))

oimg = ImageTk.PhotoImage(original_image)
fimg = ImageTk.PhotoImage(filtered_image)
# create label and add resize image
label1 = Label(root, image=oimg)
label1.image = oimg
label1.pack()
label2 = Label(root, image=fimg)
label2.image = fimg
label2.pack() 
# Execute Tkinter
root.mainloop()
print("Bye, bye")