#### Analyzer for a One-Dimensional Diffraction Pattern, by Hans-Otto Carmesin, May 2026 # # Please Proceed as follows # # 1. Generate a one-dimensional diffraction pattern and take a photo of it. Do not overexpose the photo. # 2. Cut out the pattern from the photo, so that the pattern is in the x-direction, and the y-direction can be summed. # 3. Save that separated part of the photo in a file 'photo.png' in a directory. # 4. Save the present program 'Pattern_Analyzer.py' with the ending '.py' in the same directory. # 5. Open that program with the Software 'Python', apply 'right mouse click' and choose the App 'Python'. # 6. The program generates the RGB values as a function of x and stores that pattern in the file 'rgb_columns'. # #### import subprocess import sys from pathlib import Path import csv # ------------------------------------------------- # Pillow import / Installation # ------------------------------------------------- try: from PIL import Image except ImportError: subprocess.check_call([sys.executable, "-m", "pip", "install", "pillow"]) from PIL import Image # ------------------------------------------------- # Lokaler Ordner # ------------------------------------------------- skript_ordner = Path(__file__).resolve().parent # ------------------------------------------------- # Dateien # ------------------------------------------------- bild_pfad = skript_ordner / "photo.png" rgb_spalten_pfad = skript_ordner / "rgb_columns.txt" # ------------------------------------------------- # Bild prüfen # ------------------------------------------------- if not bild_pfad.exists(): print("Photo not found:", bild_pfad) input("Enter to finish ...") sys.exit() # ------------------------------------------------- # Bild laden # ------------------------------------------------- bild = Image.open(bild_pfad).convert("RGB") breite, hoehe = bild.size # ------------------------------------------------- # Summen pro Spalte vorbereiten # ------------------------------------------------- r_sum = [0] * breite g_sum = [0] * breite b_sum = [0] * breite # ------------------------------------------------- # RGB Werte aufsummieren (spaltenweise) # ------------------------------------------------- for y in range(hoehe): for x in range(breite): r, g, b = bild.getpixel((x, y)) r_sum[x] += r g_sum[x] += g b_sum[x] += b # ------------------------------------------------- # CSV schreiben # ------------------------------------------------- with open(rgb_spalten_pfad, "w", newline="", encoding="utf-8") as datei: writer = csv.writer(datei, delimiter=";") # Kopfzeile writer.writerow(["Spalte", "R_sum", "G_sum", "B_sum"]) # Daten schreiben for x in range(breite): writer.writerow([x, r_sum[x], g_sum[x], b_sum[x]]) # ------------------------------------------------- # Ausgabe # ------------------------------------------------- print("Analysis finished!") print("Columns of RGB-Sums saved in:") print(rgb_spalten_pfad) input("Enter to finish ...")