<jupyter_start><jupyter_text>Praca domowa 7 <NAME> Będę używać `matplotlib` do wczytania obrazka.<jupyter_code>import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
from sklearn.decomposition import PCA

img = mpimg.imread("pic.png")<jupyter_output><empty_output><jupyter_text>Tak wygląda wczytany plik:<jupyter_code>img
def compress(img, n_comps):
    img1 = np.reshape(img, (img.shape[0], img.shape[1]*img.shape[2]))
    pca = PCA(n_comps).fit(img1)
    img_pca = pca.transform(img1)
    img2 = pca.inverse_transform(img_pca) 
    img_compressed = np.reshape(img2, (img.shape[0], img.shape[1], img.shape[2]))
    return img_compressed, pca, img_pca

def size_compare(img, img_compressed, pca, img_pca):
    
    original_size = img.shape[0] * img.shape[1] * img.shape[2]
    compressed_size = img_pca.shape[0] * img_pca.shape[1] + pca.components_.shape[0] * pca.components_.shape[1]
    
    print("Original size: ")
    print(original_size)
    print("Compressed size: ")
    print(compressed_size)
    print("\nCompressed image ratio to original: ")
    print(compressed_size / original_size)
img_compressed, pca, img_pca = compress(img, 1)

plt.axis(False)
plt.imshow(img_compressed)
size_compare(img, img_compressed, pca, img_pca)<jupyter_output>Original size: 
210675
Compressed size: 
1060

Compressed image ratio to original: 
0.005031446540880503<jupyter_text>Obrazek zyskał bardzo dużo na rozmiarze, stracił na jakości, ale zyskał tez na śmieszności.<jupyter_code>img_compressed, pca, img_pca = compress(img, 10)

plt.axis(False)
plt.imshow(img_compressed)

size_compare(img, img_compressed, pca, img_pca)
img_compressed, pca, img_pca = compress(img, 100)

plt.axis(False)
plt.imshow(img_compressed)

size_compare(img, img_compressed, pca, img_pca)
img_compressed, pca, img_pca = compress(img, 265)

plt.axis(False)
plt.imshow(img_compressed)

size_compare(img, img_compressed, pca, img_pca)<jupyter_output>Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
{'fraction_non_alphanumeric': 0.11618862421001458, 'fraction_numerical': 0.030627126883811376, 'mean_word_length': 5.681818181818182, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '353808', 'license': 'Apache-2.0', 'n_tokens_mistral': 858, 'n_tokens_neox': 815, 'n_words': 164, 'path': 'Prace_domowe/Praca_domowa7/Grupa3/WdowskiMichal/pd7.ipynb', 'repo_name': 'kstaron/2020L-WUM', 'size': '2065'}