Skip to content

Commit 594bf6f

Browse files
psychedelicioushipsterusername
authored andcommitted
fix(api,ui): fix canvas saved images have extra transparent regions
- add `crop_visible` param to upload image & set to true only for canvas saves
1 parent 6f2e8d5 commit 594bf6f

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

invokeai/app/api/routers/images.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ async def upload_image(
4646
session_id: Optional[str] = Query(
4747
default=None, description="The session ID associated with this upload, if any"
4848
),
49+
crop_visible: Optional[bool] = Query(
50+
default=False, description="Whether to crop the image"
51+
),
4952
) -> ImageDTO:
5053
"""Uploads an image"""
5154
if not file.content_type.startswith("image"):
@@ -55,6 +58,9 @@ async def upload_image(
5558

5659
try:
5760
pil_image = Image.open(io.BytesIO(contents))
61+
if crop_visible:
62+
bbox = pil_image.getbbox()
63+
pil_image = pil_image.crop(bbox)
5864
except:
5965
# Error opening the image
6066
raise HTTPException(status_code=415, detail="Failed to read image")

invokeai/frontend/web/src/app/store/middleware/listenerMiddleware/listeners/canvasSavedToGallery.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export const addCanvasSavedToGalleryListener = () => {
3535
image_category: 'general',
3636
is_intermediate: false,
3737
board_id: state.gallery.autoAddBoardId,
38+
crop_visible: true,
3839
postUploadAction: {
3940
type: 'TOAST',
4041
toastOptions: { title: 'Canvas Saved to Gallery' },

invokeai/frontend/web/src/services/api/endpoints/images.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ export const imagesApi = api.injectEndpoints({
421421
postUploadAction?: PostUploadAction;
422422
session_id?: string;
423423
board_id?: string;
424+
crop_visible?: boolean;
424425
}
425426
>({
426427
query: ({
@@ -429,6 +430,7 @@ export const imagesApi = api.injectEndpoints({
429430
is_intermediate,
430431
session_id,
431432
board_id,
433+
crop_visible,
432434
}) => {
433435
const formData = new FormData();
434436
formData.append('file', file);
@@ -441,6 +443,7 @@ export const imagesApi = api.injectEndpoints({
441443
is_intermediate,
442444
session_id,
443445
board_id,
446+
crop_visible,
444447
},
445448
};
446449
},

invokeai/frontend/web/src/services/api/schema.d.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5356,17 +5356,17 @@ export type components = {
53565356
image?: components["schemas"]["ImageField"];
53575357
};
53585358
/**
5359-
* StableDiffusionXLModelFormat
5359+
* StableDiffusion2ModelFormat
53605360
* @description An enumeration.
53615361
* @enum {string}
53625362
*/
5363-
StableDiffusionXLModelFormat: "checkpoint" | "diffusers";
5363+
StableDiffusion2ModelFormat: "checkpoint" | "diffusers";
53645364
/**
5365-
* StableDiffusion2ModelFormat
5365+
* StableDiffusionXLModelFormat
53665366
* @description An enumeration.
53675367
* @enum {string}
53685368
*/
5369-
StableDiffusion2ModelFormat: "checkpoint" | "diffusers";
5369+
StableDiffusionXLModelFormat: "checkpoint" | "diffusers";
53705370
/**
53715371
* StableDiffusion1ModelFormat
53725372
* @description An enumeration.
@@ -6050,6 +6050,8 @@ export type operations = {
60506050
board_id?: string;
60516051
/** @description The session ID associated with this upload, if any */
60526052
session_id?: string;
6053+
/** @description Whether to crop the image */
6054+
crop_visible?: boolean;
60536055
};
60546056
};
60556057
requestBody: {

0 commit comments

Comments
 (0)