<jupyter_start><jupyter_text>Setup<jupyter_code>pip install -r requirements.txt
from PIL import Image
from tqdm import tqdm
from sklearn.decomposition import PCA
from matplotlib.pyplot import imshow, figure
from tifffile import imsave
import numpy as np
import os
input_path = './input'
output_path = './output'
input_files = [filename for filename in os.listdir(input_path)]
print(f'input_files ({len(input_files)}): {input_files}')<jupyter_output><empty_output><jupyter_text>Files parsingFor each file:1. read file1. get image size1. "parse" the image with PCA1. save the output to a .tif file<jupyter_code>new_files = []
images = {key: {} for key in input_files}

for image_file in tqdm(input_files, desc='Parsing files'):
    # Read image
    image_path = os.path.join(input_path, image_file)
    image = Image.open(image_path)
    image_sequence = image.getdata()
    image_array = np.array(image_sequence)
    h, w, _ = np.asarray(image).shape
    images[image_file]['input'] = np.asarray(image)
    images[image_file]['size'] = (h, w)
    
    # Use pca transformation to parse image
    pca = PCA(n_components=3)
    pca.fit(image_array)
    output = pca.transform(image_array)
    images[image_file]['pca'] = pca
    
    # Save output as tif
    new_filename = f'{image_file.split(".")[0]}.tif'
    imsave(os.path.join(output_path, new_filename), output.reshape(h, w, 3))
    new_files.append(new_filename)
    images[image_file]['output'] = output.reshape(h, w, 3)
print(f'Parsed files: {new_files}')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12394548994159636, 'fraction_numerical': 0.005191434133679429, 'mean_word_length': 4.818867924528302, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '586757', 'license': 'MIT', 'n_tokens_mistral': 552, 'n_tokens_neox': 541, 'n_words': 127, 'path': 'Test_PCA_applied_on_RGB_image.ipynb', 'repo_name': 'Wasp971/PCA_Test', 'size': '1541'}