Support StabilityAi Image Edit Api #1344
Open
+447
−0
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.
Hello, this PR implements StabilityAi Image Edit - Remove Background API's client.
[The result of the execution]
[Background]
StabilityAI provides various features for editing images via API requests.
Currently, Spring AI supports image generation using text, but lacks functionality for image editing.
This PR adds the StabilitAiEditApi class, which implements the Remove Background API from StabilityAI at the raw level.
The latest version of StabilityAI is V2 Beta, and for the API, please refer to StabilityAi Image Edit - Remove Background API.
[PR contents]
StabilityAiEditApi.RemoveBackgroundRequest record
StabilityAiEditApi.ImageEditHeaders record
StabilityAiEditApi.StructuredResponse record
,byte[]
)removeBackground method
in StabilityAiEditApi ClassStabilityAiEditApiIT Class
image
byte[]
output_format
OutputFormat Enum
Users can submit an image as byte[] in formats such as png or webp.
Raw binary image data can be submitted using multipart form data.
An HTTP request is sent using RestClient.
Using
ImageEditHeaders
, users can add headers. Headers added at the constructor level are included in the RestClient builder, while those added in the HTTP request sending methodremoveBackground
are applied only to that specific request.StructuredResponse
byte[]
StructuredResponse Class
b64Image
String
finishReason
String
- SUCCESS
- CONTENT_FILTERED
seed
String
The response type can be set using the Accept header in the request.
Depending on the requested response type, it returns either a base64-encoded image or raw binary image data.
If a base64-encoded image is requested, the JSON object is mapped to the
StructuredResponse
class. If a raw binary image is requested, it is mapped tobyte[]
. Java Generics are used to dynamically determine the response type, which the user specifies in the responseType parameter when calling theremoveBackground
method.Both tests ensure the functionality of the API in handling different response types (byte[] and StructuredResponse) and confirm that the data is processed and returned as expected.
test.png
) is submitted inbyte[]
format.removeBackground method
byte[]
.null
.test.png
) is submitted with the output formatWEBP
.removeBackground
methodStructuredResponse
class, containing a base64-encoded image."SUCCESS"
.WEBP
file for further validation.[Related Issue]
I’m pleased to submit this PR to Spring AI.
Please let me know if any revisions are needed, and I will address them promptly.
I would be happy to assist with future maintenance as well, if necessary.