Added option in lasso inpaint to maintain original selection ratio instead of squaring.

pull/369/head
Abdullah Alfaraj 2023-11-21 05:34:27 +03:00
parent 0a6d412ca6
commit f039b381c9
4 changed files with 50 additions and 6 deletions

View File

@ -324,6 +324,24 @@ async function channelToSelectionExe(channel_name = 'mask') {
} }
} }
function keepRatio(selectionInfo, offset) {
// Calculate the current width and height
let width = selectionInfo.right - selectionInfo.left
let height = selectionInfo.bottom - selectionInfo.top
// Calculate the new coordinates with the offset
selectionInfo.right += offset
selectionInfo.left -= offset
selectionInfo.bottom += offset
selectionInfo.top -= offset
// Update width and height
selectionInfo.width = width + 2 * offset
selectionInfo.height = height + 2 * offset
return selectionInfo
}
function makeSquare(selectionInfo, offset) { function makeSquare(selectionInfo, offset) {
// Calculate the current width and height // Calculate the current width and height
let width = selectionInfo.right - selectionInfo.left let width = selectionInfo.right - selectionInfo.left
@ -349,12 +367,20 @@ function makeSquare(selectionInfo, offset) {
return selectionInfo return selectionInfo
} }
async function inpaintLassoInitImageAndMask(channel_name = 'mask', offset = 0) { async function inpaintLassoInitImageAndMask(
channel_name = 'mask',
offset = 0,
make_square = true
) {
const selectionInfo = await psapi.getSelectionInfoExe() const selectionInfo = await psapi.getSelectionInfoExe()
//convert the selection box into square box so that you have best output results //convert the selection box into square box so that you have best output results
const squareSelection = makeSquare(selectionInfo, offset)
const newSelection = make_square
? makeSquare(selectionInfo, offset)
: keepRatio(selectionInfo, offset)
//correct width and height sliders, since this is lasso mode. //correct width and height sliders, since this is lasso mode.
await calcWidthHeightFromSelection(squareSelection) await calcWidthHeightFromSelection(newSelection)
async function getImageFromCanvas() { async function getImageFromCanvas() {
const width = html_manip.getWidth() const width = html_manip.getWidth()
@ -363,7 +389,7 @@ async function inpaintLassoInitImageAndMask(channel_name = 'mask', offset = 0) {
const base64 = await io.IO.getSelectionFromCanvasAsBase64Interface_New( const base64 = await io.IO.getSelectionFromCanvasAsBase64Interface_New(
width, width,
height, height,
squareSelection, newSelection,
true true
) )
return base64 return base64
@ -396,7 +422,7 @@ async function inpaintLassoInitImageAndMask(channel_name = 'mask', offset = 0) {
synchronousExecution: true, synchronousExecution: true,
}) })
// const selection_info = await psapi.getSelectionInfoExe() // const selection_info = await psapi.getSelectionInfoExe()
mask_base64 = await fillSelectionWhiteOutsideBlack(squareSelection) mask_base64 = await fillSelectionWhiteOutsideBlack(newSelection)
}) })
//save laso selection to channel //save laso selection to channel

View File

@ -105,6 +105,22 @@ const Modes = observer(() => {
> >
Lasso Mode Lasso Mode
</SpCheckBox> </SpCheckBox>
<SpCheckBox
style={{
marginLeft: '10px',
display: store.data.is_lasso_mode
? void 0
: 'none',
}}
onChange={() => {
helper_store.data.make_square =
!helper_store.data.make_square
}}
checked={helper_store.data.make_square}
// id={`chEnableControlNet_${this.props.index}`}
>
Make Square
</SpCheckBox>
<SpSlider <SpSlider
show-value="false" show-value="false"

View File

@ -206,6 +206,7 @@ export const helper_store = new AStore({
native_presets: {}, native_presets: {},
base_size: 512 as number, base_size: 512 as number,
lasso_offset: 10 as number, lasso_offset: 10 as number,
make_square: true as boolean,
}) })
export async function refreshModels() { export async function refreshModels() {
let b_result = false let b_result = false

View File

@ -588,7 +588,8 @@ export class LassoInpaintMode extends Img2ImgMode {
} }
const [init_image, mask] = await selection.inpaintLassoInitImageAndMask( const [init_image, mask] = await selection.inpaintLassoInitImageAndMask(
'mask', 'mask',
sd_tab_util.helper_store.data.lasso_offset sd_tab_util.helper_store.data.lasso_offset,
sd_tab_util.helper_store.data.make_square
) )
const selectionInfo = await psapi.getSelectionInfoExe() const selectionInfo = await psapi.getSelectionInfoExe()