Commit 65be7bdc authored by Bryce Hepner's avatar Bryce Hepner
Browse files

updated descriptions

parent 0f1af9da
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
%% Cell type:code id: tags:

``` python
import numpy as np
from matplotlib import pyplot as plt
from itertools import product
import os
import sys
from PIL import Image
from scipy.optimize import minimize,linprog
import time
import seaborn as sns
from sklearn.neighbors import KernelDensity
import pandas as pd
from collections import Counter
import time
import numpy.linalg as la
```

%% Cell type:markdown id: tags:

Round 1 : Using PIL to compress the tiff file.

Conclusion : The best that I could get using the lzw compressor from PIL was 61.5%. This is worse than the other one at 40, which is likely because it is using multiple channels instead of just one.

%% Cell type:code id: tags:

``` python
def file_extractor(dirname="images"):
    files = os.listdir(dirname)
    scenes = []
    for file in files:
        if file == '.DS_Store':
            continue
        else:
            scenes.append(os.path.join(dirname, file))
    return scenes

def image_extractor(scenes):
    image_folder = []
    for scene in scenes:
        files = os.listdir(scene)
        for file in files:
            if file[-5:] != ".tiff" or file[-7:] == "_6.tiff":
                continue
            else:
                image_folder.append(os.path.join(scene, file))
    return image_folder #returns a list of file paths to .tiff files in the specified directory given in file_extractor
```

%% Cell type:code id: tags:

``` python
scenes = file_extractor()
images = image_extractor(scenes)
```

%% Cell type:code id: tags:

``` python
first_source = images[0]
picture = Image.open(first_source)
print(picture.size)
```

%% Output

    (640, 513)

%% Cell type:code id: tags:

``` python
picture.save("Compressed_Test.tiff",compression='tiff_lzw')
```

%% Cell type:code id: tags:

``` python
compressed_location = "Compressed_Test.tiff"
```

%% Cell type:code id: tags:

``` python
compressed_image = Image.open(compressed_location)
```

%% Cell type:code id: tags:

``` python
print(compressed_image.size)
```

%% Output

    (640, 513)

%% Cell type:code id: tags:

``` python
from PIL import features
print(features.check('libtiff'))
```

%% Output

    True

%% Cell type:code id: tags:

``` python
size = os.path.getsize(compressed_location)
```

%% Cell type:code id: tags:

``` python
print(size)
```

%% Output

    473404

%% Cell type:code id: tags:

``` python
oldsize = os.path.getsize(first_source)
print(oldsize)
```

%% Output

    657197

%% Cell type:code id: tags:

``` python
print(size/oldsize)
```

%% Output

    0.7203380417135197

%% Cell type:code id: tags:

``` python
from PIL import TiffTags
TiffTags.LIBTIFF_CORE.add(317)
picture.save('Compressed_Round_2.tiff', compression='tiff_lzw', tiffinfo={317: 2})
```

%% Cell type:code id: tags:

``` python
print(os.path.getsize('Compressed_Round_2.tiff'))
```

%% Output

    404176

%% Cell type:code id: tags:

``` python
print(404176/oldsize)
```

%% Output

    0.6149997641498668

%% Cell type:code id: tags:

``` python
```

%% Cell type:markdown id: tags:

Round 2: Trying the same thing but with tifffile and doing grayscale

Conclusion: The documentation was terrible. It advertizes compression but doesn't say how it's done, and the specifics of it. Moving on to something else, but still worth a shot.

%% Cell type:code id: tags:

``` python
scenes = file_extractor()
images = image_extractor(scenes)
first_source = images[0]
picture = Image.open(first_source)
print(picture.size)
```

%% Output

    <PIL.TiffImagePlugin.TiffImageFile image mode=I;16B size=640x513 at 0x7F3520192160>

%% Cell type:code id: tags:

``` python
import tifffile
```

%% Cell type:code id: tags:

``` python
otherpic = tifffile.imread(first_source)
print(otherpic)
```

%% Output

    [[    2     1 54668 ... 65535 65535 65535]
     [22275 22292 22292 ... 22280 22212 22270]
     [22303 22301 22298 ... 22254 22248 22262]
     ...
     [21832 21820 21844 ... 21892 21852 21845]
     [21843 21821 21830 ... 21870 21865 21864]
     [21836 21829 21840 ... 21858 21857 21860]]

%% Cell type:markdown id: tags:

Round 3: Trying numcompress, something that compresses numbers. Should provide an ok benchmark, and is actually documented.

Conclusion: The headline spoofed, it said I could get over 80%, I got under 30%. So that's worth looking into. Also this algorithm doesn't look into verticle changes, just horizontal, so I don't know why they advertized it to be as good as it is. No way it could be better than PNG, which doesn't advertize anything that high.

%% Cell type:code id: tags:

``` python
from numcompress import compress_ndarray, decompress_ndarray
```

%% Cell type:code id: tags:

``` python
# print(list(otherpic).dtype)
otherpic = tifffile.imread(first_source)

compressed = compress_ndarray(otherpic, precision = 0)
# print(compressed)
decompressed = decompress_ndarray(compressed)
print(np.allclose(otherpic,decompressed))
print(len(compressed))
print(otherpic.size)
```

%% Output

    True
    452529
    328320

%% Cell type:code id: tags:

``` python
with open("ThirdTry.txt", 'w') as f:
    f.write(compressed)
```

%% Cell type:code id: tags:

``` python
thirdsize = os.path.getsize("ThirdTry.txt")
```

%% Cell type:code id: tags:

``` python
print((oldsize-thirdsize)/oldsize)
```

%% Output

    0.31142564558267916

%% Cell type:code id: tags:

``` python
sys.getsizeof(compressed)
```

%% Output

    452578

%% Cell type:code id: tags:

``` python
```

%% Output

    112

%% Cell type:code id: tags:

``` python
```