-
Notifications
You must be signed in to change notification settings - Fork 438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FEAT Add image overlay converter #507
Closed
+192
−0
Closed
Changes from 13 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
d6ba5b5
created image_overlay_converter.py
u7702792 947ef15
update __init__.py to add the new converter
u7702792 7a7bfad
minor change on orders
u7702792 47f58e0
Merge branch 'Azure:main' into main
u7702792 d210747
update on image_overlay_converter.py, to make the whole class more co…
u7702792 b14883e
Merge branch 'Azure:main' into main
u7702792 53bd55c
Merge branch 'Azure:main' into feat/add_image_overlay_converter
u7702792 21138d9
Merge branch 'main' of https://github.com/u7702792/PyRIT into feat/ad…
u7702792 c42d9e4
Merge remote-tracking branch 'origin/feat/add_image_overlay_converter…
u7702792 09efd5a
add some test cases of image_overlay_converter.py to git
u7702792 ec3b110
update the image_overlay_converter.py
u7702792 3af29ea
update
u7702792 8b080be
Merge branch 'Azure:main' into main
u7702792 9cd8f13
Merge branch 'main' of https://github.com/u7702792/PyRIT into feat/ad…
u7702792 2929d5e
Merge branch 'Azure:main' into feat/add_image_overlay_converter
u7702792 0ffe26a
Merge branch 'Azure:main' into feat/add_image_overlay_converter
u7702792 e09e20a
update on image_overlay_converter.py to fix the issue from PR comments
u7702792 46de42f
Merge remote-tracking branch 'origin/feat/add_image_overlay_converter…
u7702792 615e7fb
update on test_image_overlay_converter.py to fix the issue from PR co…
u7702792 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT license. | ||
|
||
import base64 | ||
from typing import Optional | ||
|
||
from PIL import Image | ||
from io import BytesIO | ||
|
||
from pyrit.models import data_serializer_factory | ||
from pyrit.models import PromptDataType | ||
from pyrit.prompt_converter import PromptConverter, ConverterResult | ||
from pyrit.memory import MemoryInterface, DuckDBMemory | ||
|
||
|
||
class ImageOverlayConverter(PromptConverter): | ||
""" | ||
A converter that takes in a base image, and a secondary image to embed within the main image. | ||
|
||
Args: | ||
base_image_path (str): File path of the base image | ||
x_pos (int, optional): X coordinate to place second image on the base image (0 is left most). Defaults to 0. | ||
y_pos (int, optional): Y coordinate to place second image on the base image (0 is upper most). Defaults to 0. | ||
memory: (memory, optional): Memory to store the chat messages. DuckDBMemory will be used by default. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
base_image_path: str, | ||
x_pos: Optional[int] = 0, | ||
y_pos: Optional[int] = 0, | ||
memory: Optional[MemoryInterface] = None, | ||
): | ||
if not base_image_path: | ||
u7702792 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
raise ValueError("Please provide valid image path") | ||
|
||
self._base_image_path = base_image_path | ||
self._x_pos = x_pos | ||
u7702792 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self._y_pos = y_pos | ||
self._memory = memory or DuckDBMemory() | ||
|
||
def _add_overlay_image(self, overlay_image: Image.Image) -> Image.Image: | ||
""" | ||
Embed the second image onto the base image | ||
|
||
Args: | ||
overlay_image(Image.Image): The second image to lay on the base one. | ||
|
||
Returns: | ||
Image.Image: The combined image with overlay. | ||
""" | ||
if not overlay_image: | ||
raise ValueError("Please provide a valid image") | ||
# Open the images | ||
base_image = Image.open(self._base_image_path) | ||
u7702792 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Make a copy of base image, in case the user want to keep their input images unchanged | ||
copied_base_image = base_image.copy() | ||
|
||
# Paste the second image onto the base image | ||
copied_base_image.paste(overlay_image, (self._x_pos, self._y_pos), overlay_image) | ||
|
||
return copied_base_image | ||
|
||
async def convert_async(self, *, prompt: str, input_type: PromptDataType = "image_path") -> ConverterResult: | ||
""" | ||
Converter the base image to embed the second image onto it. | ||
|
||
Args: | ||
prompt (str): The filename of the second image | ||
input_type (PromptDataType): type of data, should be image_path | ||
|
||
Returns: | ||
ConverterResult: converted image with file path | ||
""" | ||
if not self.input_supported(input_type): | ||
raise ValueError("Input type not supported") | ||
|
||
img_serializer = data_serializer_factory(value=prompt, data_type="image_path", memory=self._memory) | ||
second_img_bytes = await img_serializer.read_data() | ||
second_img = Image.open(BytesIO(second_img_bytes)) | ||
|
||
# Add overlay to the base image | ||
updated_img = self._add_overlay_image(second_img) | ||
|
||
# Encode the image using base64 and return ConverterResult | ||
# I took the following code from add_image_text_converter.py @author rdheekonda | ||
u7702792 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
image_bytes = BytesIO() | ||
mime_type = img_serializer.get_mime_type(prompt) | ||
image_type = mime_type.split("/")[-1] | ||
updated_img.save(image_bytes, format=image_type) | ||
image_str = base64.b64encode(image_bytes.getvalue()) | ||
# Save image as generated UUID filename | ||
await img_serializer.save_b64_image(data=image_str) | ||
return ConverterResult(output_text=str(img_serializer.value), output_type="image_path") | ||
|
||
def input_supported(self, input_type: PromptDataType) -> bool: | ||
return input_type == "image_path" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT license. | ||
import os | ||
|
||
import pytest | ||
from PIL import Image | ||
|
||
from pyrit.prompt_converter import ImageOverlayConverter | ||
|
||
from io import BytesIO | ||
|
||
|
||
@pytest.fixture | ||
def base_image_path(): | ||
img = Image.new("RGB", (100, 100), color=(255, 255, 255)) | ||
img.save("base_test.png") | ||
u7702792 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return "base_test.png" | ||
|
||
|
||
@pytest.fixture | ||
def overlay_image_byte(): | ||
img = Image.new("RGBA", (20, 20), color=(125, 125, 125, 125)) | ||
img_bytes = BytesIO() | ||
img.save(img_bytes, format="PNG") | ||
img_bytes = img_bytes.getvalue() | ||
return img_bytes | ||
|
||
|
||
def test_image_overlay_converter_initialization(base_image_path): | ||
u7702792 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
converter = ImageOverlayConverter( | ||
base_image_path=base_image_path, x_pos=10, y_pos=10, memory=None | ||
) | ||
assert converter._base_image_path == "base_test.png" | ||
assert converter._x_pos == 10 | ||
assert converter._y_pos == 10 | ||
os.remove("base_test.png") | ||
|
||
|
||
def test_image_overlay_converter_invalid_image(): | ||
with pytest.raises(ValueError): | ||
ImageOverlayConverter(base_image_path="") | ||
|
||
|
||
def test_image_overlay_converter_add_overlay_image(base_image_path, overlay_image_byte): | ||
u7702792 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
converter = ImageOverlayConverter(base_image_path=base_image_path) | ||
base_image = Image.open(base_image_path) | ||
overlay_image = Image.open(overlay_image_byte) | ||
pixels_before = list(base_image.getdata()) | ||
|
||
# Adding overlay image | ||
updated_image = converter._add_overlay_image(overlay_image) | ||
pixels_after = list(updated_image.getdata()) | ||
|
||
assert updated_image is not None | ||
# Check for pixels changes | ||
assert pixels_before != pixels_after | ||
os.remove("base_test.png") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#527 is removing the memory arg so you won't need to have this arg, but when you need to use memory you can simply do
CentralMemory.get_memory_instance()
Let me know if this doesn't work or make sense.