Commit 18e8282a authored by Bryce Hepner's avatar Bryce Hepner
Browse files

more tests

parent 669a9a33
Loading
Loading
Loading
Loading
+67 −5
Original line number Original line Diff line number Diff line
%% Cell type:code id: tags:
%% Cell type:code id: tags:


``` python
``` python
from WorkingPyDemo import *
from WorkingPyDemo import *
from time import time
from time import time
from scipy import misc
from scipy import misc
import imageio
import imageio
import png
import png
```
```


%% Cell type:code id: tags:
%% Cell type:code id: tags:


``` python
``` python
scenes = file_extractor(folder_name)
scenes = file_extractor(folder_name)
images = image_extractor(scenes)
images = image_extractor(scenes)
print(len(images))
print(len(images))
newnamesforlater = []
newnamesforlater = []
file_sizes_new = []
file_sizes_new = []
file_sizes_old = []
file_sizes_old = []
list_dic = np.load("first_dic.npy", allow_pickle="TRUE")
list_dic = np.load("first_dic.npy", allow_pickle="TRUE")
bins = [21,32,48]
bins = [21,32,48]
for i in range(len(images)):
for i in range(len(images)):


    if images[i][-5:] == ".tiff":
    if images[i][-5:] == ".tiff":
        newname = images[i][:-5]
        newname = images[i][:-5]
    else:
    else:
        newname = images[i][:-4]
        newname = images[i][:-4]
    newnamesforlater.append(newname + "_Compressed.txt")
    if images[i][-18:] != "Compressedlzw.tiff":
        newnamesforlater.append(newname + "_Compressed.txt")
    # with open(newname + "_Compressed.txt", 'wb') as f:
    # with open(newname + "_Compressed.txt", 'wb') as f:
    #     f.write(inletters)
    #     f.write(inletters)
    file_sizes_new.append((os.path.getsize(newname + "_Compressed.txt")))
        file_sizes_new.append((os.path.getsize(newname + "_Compressed.txt")))
    file_sizes_old.append((os.path.getsize(images[i])))
        file_sizes_old.append((os.path.getsize(images[i])))
file_sizes_new.append(os.path.getsize("first_dic.npy"))
file_sizes_new.append(os.path.getsize("first_dic.npy"))
print(len(newnamesforlater))
print(np.sum(file_sizes_new)/np.sum(file_sizes_old))
print(np.sum(file_sizes_new)/np.sum(file_sizes_old))
```
```


%% Output
%% Output


    262
    262
    262
    0.3968581030135283
    0.3968581030135283


%% Cell type:code id: tags:
%% Cell type:code id: tags:


``` python
``` python
pngsizes = []
pngsizes = []
for i, filename in enumerate(images):
for i, filename in enumerate(images):
    newimage = Image.open(filename)
    newimage = Image.open(filename)
    newimage = np.array(newimage)
    newimage = np.array(newimage)
    newimage = newimage[1:]
    newimage = newimage[1:]
    with open(newnamesforlater[i][:-4] + ".png", 'wb') as f:
    with open(newnamesforlater[i][:-4] + ".png", 'wb') as f:
        writer = png.Writer(newimage.shape[1], newimage.shape[0], greyscale=True, bitdepth=16)
        writer = png.Writer(newimage.shape[1], newimage.shape[0], greyscale=True, bitdepth=16)
        writer.write(f, newimage)
        writer.write(f, newimage)
    # imageio.imwrite(newnamesforlater[i][:-4] + ".png", newimage)
    # imageio.imwrite(newnamesforlater[i][:-4] + ".png", newimage)
    # newimage.close()
    # newimage.close()
    pngsizes.append(os.path.getsize(newnamesforlater[i][:-4] + ".png"))
    pngsizes.append(os.path.getsize(newnamesforlater[i][:-4] + ".png"))
print(np.sum(pngsizes)/np.sum(file_sizes_old))
print(np.sum(pngsizes)/np.sum(file_sizes_old))
```
```


%% Output
%% Output


    0.6104417990437042
    0.6104417990437042


%% Cell type:code id: tags:
%% Cell type:code id: tags:


``` python
``` python
print(os.path.getsize("1626032610_393963_0.xcf")/os.path.getsize("images/1626032610_393963/1626032610_393963_0.tiff"))
print(os.path.getsize("1626032610_393963_0.xcf")/os.path.getsize("images/1626032610_393963/1626032610_393963_0.tiff"))
```
```


%% Output
%% Output


    0.5676821409714287
    0.5676821409714287


%% Cell type:code id: tags:
%% Cell type:code id: tags:


``` python
``` python
print(os.path.getsize("1626032610_393963_0.png")/os.path.getsize("images/1626032610_393963/1626032610_393963_0.tiff"))
print(os.path.getsize("1626032610_393963_0.png")/os.path.getsize("images/1626032610_393963/1626032610_393963_0.tiff"))
```
```


%% Output
%% Output


    0.4855910784741866
    0.4855910784741866


%% Cell type:code id: tags:
%% Cell type:code id: tags:


``` python
``` python
for i, filename in enumerate(images):
for i, filename in enumerate(images):
    original = Image.open(filename)
    original = Image.open(filename)
    compressed = Image.open(newnamesforlater[i][:-4] + ".png")
    compressed = Image.open(newnamesforlater[i][:-4] + ".png")
    original = np.array(original)[1:]
    original = np.array(original)[1:]
    compressed = np.array(compressed)
    compressed = np.array(compressed)
    if not np.allclose(original, compressed):
    if not np.allclose(original, compressed):
        print(i)
        print(i)
```
```


%% Cell type:code id: tags:
%% Cell type:code id: tags:


``` python
``` python
print((np.sum(pngsizes) - np.sum(file_sizes_new))/np.sum(pngsizes))
print((np.sum(pngsizes))/np.sum(file_sizes_old))
```
```


%% Output
%% Output


    0.34740136372336333
    0.6104417990437042


%% Cell type:code id: tags:
%% Cell type:code id: tags:


``` python
``` python
```
```


%% Cell type:code id: tags:
%% Cell type:code id: tags:


``` python
``` python
from PIL import TiffTags
from PIL import TiffTags
TiffTags.LIBTIFF_CORE.add(318)
TiffTags.LIBTIFF_CORE.add(318)
```
```


%% Cell type:code id: tags:
%% Cell type:code id: tags:


``` python
``` python
lwz_sizes = []
lwz_sizes = []
for i, filename in enumerate(images):
for i, filename in enumerate(images):
    newimage = Image.open(filename)
    newimage = Image.open(filename)
    newimage.save(newnamesforlater[i][:-4]+ "lzw" + ".tiff", compression='tiff_lzw', tiffinfo={317: 2})
    newimage.save(newnamesforlater[i][:-4]+ "lzw" + ".tiff", compression='tiff_lzw', tiffinfo={317: 2})


    lwz_sizes.append(os.path.getsize(newnamesforlater[i][:-4]+ "lzw" + ".tiff"))
    lwz_sizes.append(os.path.getsize(newnamesforlater[i][:-4]+ "lzw" + ".tiff"))
print(np.sum(lwz_sizes)/np.sum(file_sizes_old))
print(np.sum(lwz_sizes)/np.sum(file_sizes_old))
```
```


%% Output
%% Output


    0.6185872808807661
    0.6185872808807661


%% Cell type:code id: tags:
%% Cell type:code id: tags:


``` python
``` python
print(len(images))
for i, item in enumerate(images):
    # print(item[-18:])
    if item[-18:] == "Compressedlzw.tiff":
        os.remove(item)
scenes = file_extractor(folder_name)
images = image_extractor(scenes)
print(len(images))
```

%% Output

    524
    262

%% Cell type:code id: tags:

``` python
def all_image_extractor(scenes):
    """
    This function gives the list of all of the valid images
    Parameters:
        scenes (list): a list of all the files in the directory
    Returns:
        images (list): a list of all the images in the directory
    """
    image_folder = []
    for scene in scenes:
        files = os.listdir(scene)
        for file in files:
            if 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
all_files = all_image_extractor(scenes)
print(len(all_files))
for i, item in enumerate(all_files):
    # print(item[-18:])
    if item[-4:] == "..png":
        os.remove(item)
scenes = file_extractor(folder_name)
all_files = all_image_extractor(scenes)
print(len(all_files))
```

%% Output

    1178
    1178

%% Cell type:code id: tags:

``` python
```
```