Skip to content

Commit 09e0cb7

Browse files
committed
update readme; fix tests
1 parent dc27e74 commit 09e0cb7

File tree

2 files changed

+59
-21
lines changed

2 files changed

+59
-21
lines changed

README.md

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,67 @@
11

22
[![Build Status](https://github.com/adusachev/QOI-image-compression/actions/workflows/testing.yml/badge.svg)](https://github.com/adusachev/QOI-image-compression/actions/workflows/testing.yml)
33

4+
45
# QOI image compression
56

67
Python implementation of QOI encoder and decoder
78

89
QOI format: https://qoiformat.org/
910

11+
## Installation
1012

11-
## Usage
12-
13-
**1) Encode single .png image and save .qoi image**
13+
```
14+
git clone https://github.com/adusachev/QOI-image-compression.git
15+
```
1416

15-
At the end of `qoi_encoder.py` enter path to .png image and run:
1617
```
17-
python3 qoi_encoder.py
18+
python setup.py install
1819
```
1920

2021

21-
**2) Decode single .qoi image**
22+
## Usage
2223

23-
At the end of `qoi_decoder.py` enter path to .qoi image and run:
24-
```
25-
python3 qoi_decoder.py
24+
25+
1) Encode: convert png image to qoi image
26+
```python
27+
from qoi_compress import qoi_encoder
28+
29+
png_file = "./png_images/doge.png"
30+
qoi_file = "./qoi_images/doge.qoi" # where to save qoi image
31+
32+
qoi_encoder.run_encoder(png_file, qoi_file)
2633
```
2734

35+
2) Decode: import .qoi file into numpy array
36+
```python
37+
from qoi_compress import qoi_decoder
2838

29-
**3) Encode and decode all .png images in some directory, compare decoded .qoi image with original .png image**
39+
qoi_file = "./qoi_images/doge.qoi"
3040

31-
At the end of `main.py` choose `dir_with_png` and uncomment line with `run_multiple_experiments(dir_with_png)`, then run:
41+
img_decoded, time_elapsed = qoi_encoder.run_decoder(qoi_file)
42+
img_decoded
3243
```
33-
python3 main.py
44+
45+
3) Test: encode image `png_file` and save it as `qoi_file`, then decode `qoi_file` and compare decoded qoi image with original png image
46+
```python
47+
from qoi_compress.main import run_single_experiment
48+
49+
png_file = "./png_images/doge.png"
50+
qoi_file = "./qoi_images/doge.qoi"
51+
52+
run_single_experiment(png_file, qoi_file)
3453
```
3554

55+
56+
## Benchmarks
57+
58+
| Image size | Encoding time | Decoding time | Compression ratio |
59+
| ---------- | ------------- | ------------- | ----------------- |
60+
| 460x460 | 0.7 sec | 0.3 sec | 2.2 |
61+
| 1920x1080 | 5.5 sec | 2.5 sec | 2.56 |
62+
| 3840x2400 | 42 sec | 19 sec | 1.83 |
63+
64+
65+
## TODO
66+
67+
Add support for RGBA images

tests/test_qoi_full.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,26 @@
22
from pathlib import Path
33
import unittest
44
import numpy as np
5-
65
from qoi_compress.qoi_decoder import run_decoder
76
from qoi_compress.qoi_encoder import run_encoder
87
from qoi_compress.read_png import read_png
98

109

1110
BASE_DIR = Path(__file__).resolve().parent.parent
1211

12+
if not os.path.exists(BASE_DIR / "qoi_images"):
13+
os.mkdir(BASE_DIR / "qoi_images")
14+
1315

1416
class TestQOI(unittest.TestCase):
1517

1618
def test_single_image(self):
1719

1820
png_filename = str(BASE_DIR / "png_images/R_video.png")
21+
qoi_filename = str(BASE_DIR / "qoi_images/R_video.qoi")
1922

2023
# encoding
21-
qoi_filename, _ = run_encoder(png_filename)
24+
qoi_filename, _ = run_encoder(png_filename, qoi_filename)
2225

2326
# decoding
2427
img_decoded, _ = run_decoder(qoi_filename)
@@ -32,20 +35,23 @@ def test_single_image(self):
3235

3336
def test_multiple_images(self):
3437

35-
dir_with_png = str(BASE_DIR / "png_images/")
38+
dir_with_png = str(BASE_DIR / "png_images")
39+
dir_with_qoi = str(BASE_DIR / "qoi_images")
3640

37-
for name in os.listdir(dir_with_png):
38-
if Path(name).suffix == ".png":
39-
png_filename = os.path.join(dir_with_png, name)
41+
for filename in os.listdir(dir_with_png):
42+
if Path(filename).suffix == ".png":
43+
name = Path(filename).stem
44+
png_file = os.path.join(dir_with_png, filename)
45+
qoi_file = os.path.join(dir_with_qoi, f"{name}.qoi")
4046

4147
# encoding
42-
qoi_filename, _ = run_encoder(png_filename)
48+
qoi_filename, _ = run_encoder(png_file, qoi_file)
4349

4450
# decoding
45-
img_decoded, _ = run_decoder(qoi_filename)
51+
img_decoded, _ = run_decoder(qoi_file)
4652

4753
# compare with original image
48-
orig_img, _, _, _ = read_png(png_filename)
54+
orig_img, _, _, _ = read_png(png_file)
4955

5056
self.assertTrue(np.all(img_decoded == orig_img),
5157
f"Decoding of image {qoi_filename} failed at indexes {np.where(img_decoded != orig_img)[0]}")

0 commit comments

Comments
 (0)