Skip to content

Commit

Permalink
a bit more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephBARBIERDARNAL committed Dec 3, 2024
1 parent e689b6e commit b60d69d
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 16 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

> A lightweight python tool for creating GIFs
<br>
<br>

Let's make a gif with these images of different sizes.

<p align="center">
<img src="tests/img/image1.jpg" width="30%" />
<img src="tests/img/image2.jpg" width="30%" />
<img src="tests/img/image3.jpg" width="30%" />
</p>

```python
from gifing import Gif

Expand All @@ -15,10 +26,12 @@ gif = Gif(
gif.set_labels(
["print", "hello", "world"],
font_size=60,
loc="top left",
loc="bottom left",
text_padding=40,
box_padding=20,
box_color="#fffcee",
shadow_offset=15,
font="Urbanist",
)
gif.set_background_color("red")
gif.set_size((900, 700), scale=1)
Expand All @@ -34,6 +47,8 @@ This package offers:
- ability to set a background color during resizing
- frame-by-frame label customization

It's a basic prototype of the functionality I envision for this tool. The API is still unstable.

<br>

### Installation
Expand Down
29 changes: 16 additions & 13 deletions gifing/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def __init__(
file_path: List[str],
frame_duration: int = 1000,
n_repeat_last_frame: int = 1,
verbose: bool = True,
):
"""
Initialize the GIF maker.
Expand All @@ -23,6 +24,7 @@ def __init__(
- file_path: List of file paths to the images to be included in the GIF.
- frame_duration: Duration of each frame in milliseconds.
- n_repeat_last_frame: The number of additional frames to append with the last image.
- verbose: whether to print things in the console.
"""
self.file_path = file_path
self.frame_duration = frame_duration
Expand All @@ -32,9 +34,10 @@ def __init__(
self.background_color = (255, 255, 255)
self.label_loc = None
self.labels = None
self.images_for_gif = None
self.images_for_gif = [None] * len(file_path)
self.shadow_offset = 10
self.box_padding = 10
self.verbose = verbose
ImageFile.LOAD_TRUNCATED_IMAGES = True

def set_size(
Expand Down Expand Up @@ -74,8 +77,8 @@ def set_labels(
labels: List[str],
font_size: int = 20,
loc: str = "top left",
text_padding: int = 10,
box_padding: int = 10,
text_padding: int = 40,
box_padding: int = 20,
box_color: tuple = (255, 255, 255),
shadow_offset: int = 10,
font: str = "Arial",
Expand Down Expand Up @@ -136,25 +139,30 @@ def make(
images_for_gif.append(img_array)

last_frame = images_for_gif[-1]
for _ in range(self.n_repeat_last_frame):
for _ in range(self.n_repeat_last_frame - 1):
images_for_gif.append(last_frame)

self.images_for_gif = images_for_gif

imageio.mimsave(
f"{output_path}",
f"{self.output_path}",
self.images_for_gif,
duration=[self.frame_duration] * len(self.images_for_gif),
format="GIF",
loop=0,
)
print(f"GIF created and saved at {output_path}")

if self.verbose:
print(f"GIF created and saved at {output_path}")

def get_images(self) -> List:
"""
Retrieve a list with all the images
"""
return self.images_for_gif
if not np.any(self.images_for_gif):
raise Exception("Unable to retrieve images before calling Gif.make()")
else:
return self.images_for_gif

def _format_image(self, image):
img_w, img_h = image.size
Expand Down Expand Up @@ -195,9 +203,6 @@ def _draw_label(self, img, frame_idx: Optional[int] = None) -> None:
x = self.bg_w - text_width - self.text_padding
y = self.bg_h - text_height - self.text_padding

baseline_correction = (text_height - (text_bbox[3] - text_bbox[1])) // 2
y_centered = y + baseline_correction

box_coords = [
x - self.box_padding,
y - self.box_padding,
Expand All @@ -209,6 +214,4 @@ def _draw_label(self, img, frame_idx: Optional[int] = None) -> None:

draw.rectangle(shadow_coords, fill=(50, 50, 50, 128))
draw.rectangle(box_coords, fill=self.box_color)
draw.text(
(x, y_centered - 10), label_text, font=font, fill=(0, 0, 0), align="center"
)
draw.text((x, y - 10), label_text, font=font, fill=(0, 0, 0), align="center")
Binary file modified img/output.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/img/image1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 19 additions & 2 deletions tests/test_get_images.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import os
import pytest
from gifing import Gif


def test_get_images():
def test_get_images_before_make():
gif = Gif(
file_path=[
"tests/img/image1.jpg",
"tests/img/image2.jpg",
"tests/img/image3.jpg",
]
)
gif.set_size((500, 500), scale=2)
gif.set_background_color("yellow")

with pytest.raises(Exception):
gif.get_images()


def test_get_images_after_make():
gif = Gif(
file_path=[
"tests/img/image1.jpg",
Expand All @@ -16,4 +32,5 @@ def test_get_images():
os.remove(gif.output_path)

images = gif.get_images()
assert len(images) == 4
assert len(images) == 3
assert len(images) == len(gif.file_path)
34 changes: 34 additions & 0 deletions tests/test_make.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest
import os
from gifing import Gif


def test_make_with_wrong_path_raises_warning():
gif = Gif(
file_path=[
"tests/img/image1.jpg",
"tests/img/image2.jpg",
"tests/img/image3.jpg",
]
)

with pytest.warns():
gif.make("output")
os.remove(gif.output_path)


def test_resizing_and_scaling():
gif = Gif(
file_path=[
"tests/img/image1.jpg",
"tests/img/image2.jpg",
"tests/img/image3.jpg",
]
)
gif.set_size((500, 600), scale=2)
gif.make()
os.remove(gif.output_path)

images = gif.get_images()
for i in range(len(gif.file_path)):
assert len(images[i]) == 1200

0 comments on commit b60d69d

Please sign in to comment.