always save result as png

This commit is contained in:
Qing 2023-12-28 11:14:42 +08:00
parent cfb43a4805
commit 92ecd53232
3 changed files with 20 additions and 8 deletions

View File

@ -148,9 +148,10 @@ def save_image():
input = request.files input = request.files
filename = request.form["filename"] filename = request.form["filename"]
origin_image_bytes = input["image"].read() # RGB origin_image_bytes = input["image"].read() # RGB
ext = get_image_ext(origin_image_bytes) # ext = get_image_ext(origin_image_bytes)
ext = "png"
image, alpha_channel, exif_infos = load_img(origin_image_bytes, return_exif=True) image, alpha_channel, exif_infos = load_img(origin_image_bytes, return_exif=True)
save_path = str(global_config.output_dir / filename) save_path = (global_config.output_dir / filename).with_suffix(f".{ext}")
if alpha_channel is not None: if alpha_channel is not None:
if alpha_channel.shape[:2] != image.shape[:2]: if alpha_channel.shape[:2] != image.shape[:2]:
@ -159,7 +160,7 @@ def save_image():
) )
image = np.concatenate((image, alpha_channel[:, :, np.newaxis]), axis=-1) image = np.concatenate((image, alpha_channel[:, :, np.newaxis]), axis=-1)
pil_image = Image.fromarray(image) pil_image = Image.fromarray(image).convert("RGBA")
img_bytes = pil_to_bytes( img_bytes = pil_to_bytes(
pil_image, pil_image,
@ -167,8 +168,11 @@ def save_image():
quality=global_config.image_quality, quality=global_config.image_quality,
exif_infos=exif_infos, exif_infos=exif_infos,
) )
try:
with open(save_path, "wb") as fw: with open(save_path, "wb") as fw:
fw.write(img_bytes) fw.write(img_bytes)
except:
return f"Save image failed: {traceback.format_exc()}", 500
return "ok", 200 return "ok", 200
@ -551,6 +555,9 @@ def start(
if output_dir: if output_dir:
output_dir = output_dir.expanduser().absolute() output_dir = output_dir.expanduser().absolute()
logger.info(f"Image will auto save to output dir: {output_dir}") logger.info(f"Image will auto save to output dir: {output_dir}")
if not output_dir.exists():
logger.info(f"Create output dir: {output_dir}")
output_dir.mkdir(parents=True)
global_config.output_dir = output_dir global_config.output_dir = output_dir
global_config.model_manager = ModelManager( global_config.model_manager = ModelManager(

View File

@ -501,13 +501,17 @@ export default function Editor(props: EditorProps) {
} }
) )
function download() { const download = async () => {
if (file === undefined) { if (file === undefined) {
return return
} }
if (enableAutoSaving && renders.length > 0) { if (enableAutoSaving && renders.length > 0) {
try { try {
downloadToOutput(renders[renders.length - 1], file.name, file.type) await downloadToOutput(
renders[renders.length - 1],
file.name,
file.type
)
toast({ toast({
description: "Save image success", description: "Save image success",
}) })

View File

@ -81,7 +81,7 @@ export default async function inpaint(
"controlnet_conditioning_scale", "controlnet_conditioning_scale",
settings.controlnetConditioningScale.toString() settings.controlnetConditioningScale.toString()
) )
fd.append("controlnet_method", settings.controlnetMethod.toString()) fd.append("controlnet_method", settings.controlnetMethod?.toString())
// PowerPaint // PowerPaint
if (settings.showExtender) { if (settings.showExtender) {
@ -213,6 +213,7 @@ export async function downloadToOutput(
method: "POST", method: "POST",
body: fd, body: fd,
}) })
console.log(res.ok)
if (!res.ok) { if (!res.ok) {
const errMsg = await res.text() const errMsg = await res.text()
throw new Error(errMsg) throw new Error(errMsg)