Skip to content

Commit

Permalink
Sample image format at random, if the format is not selected
Browse files Browse the repository at this point in the history
  • Loading branch information
mwawrzos committed Jul 22, 2024
1 parent 3e1dbb1 commit b43967e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import random
from enum import Enum, auto
from pathlib import Path
from typing import Optional, cast

from genai_perf import utils
from PIL import Image
Expand All @@ -50,9 +51,13 @@ def create_synthetic_image(
image_width_stddev: int,
image_height_mean: int,
image_height_stddev: int,
image_format: ImageFormat,
image_format: Optional[ImageFormat] = None,
) -> str:
"""Generate base64 encoded synthetic image using the source images."""
if image_format is None:
image_formats = list(ImageFormat)
else:
image_formats = [cast(ImageFormat, image_format)]
width = cls._sample_random_positive_integer(
image_width_mean, image_width_stddev
)
Expand All @@ -63,8 +68,9 @@ def create_synthetic_image(
image = cls._sample_source_image()
image = image.resize(size=(width, height))

img_base64 = utils.encode_image(image, image_format.name)
return f"data:image/{image_format.name.lower()};base64,{img_base64}"
selected_format = random.choice(image_formats)
img_base64 = utils.encode_image(image, selected_format.name)
return f"data:image/{selected_format.name.lower()};base64,{img_base64}"

@classmethod
def _sample_source_image(cls):
Expand Down
4 changes: 2 additions & 2 deletions src/c++/perf_analyzer/genai-perf/genai_perf/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,9 +476,9 @@ def _add_image_input_args(parser):
"--image-format",
type=str,
choices=utils.get_enum_names(ImageFormat),
default="png",
required=False,
help=f"The compression format of the images.",
help=f"The compression format of the images. "
"If format is not selected, format of generated image is selected at random",
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def test_generate_json(self, monkeypatch) -> None:
"image_width_stddev": 0,
"image_height_mean": 100,
"image_height_stddev": 0,
"image_format": "png",
"image_format": null,
"concurrency": 1,
"measurement_interval": 10000,
"request_rate": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,24 @@ def test_base64_encoding_with_different_formats(image_format):
img_bytes = BytesIO(img_data)
image = Image.open(img_bytes)
assert image.format == image_format.name


def test_null_format_is_accepted():
img_base64 = SyntheticImageGenerator.create_synthetic_image(
image_width_mean=100,
image_width_stddev=100,
image_height_mean=100,
image_height_stddev=100,
image_format=None,
)

# check prefix
expected_prefix = "data:image/"
assert img_base64.startswith(expected_prefix), "unexpected prefix"

# check image format
data = img_base64.split(",")[1]
img_data = base64.b64decode(data)
img_bytes = BytesIO(img_data)
image = Image.open(img_bytes)
assert image.format in [f.name for f in ImageFormat]

0 comments on commit b43967e

Please sign in to comment.