Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
S
stable-diffusion-webui
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
stable-diffusion-webui
Commits
185ab3cb
Commit
185ab3cb
authored
Nov 26, 2022
by
cat
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Atomically rename saved image to avoid race condition with other processes.
parent
488f831d
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
22 deletions
+29
-22
images.py
modules/images.py
+29
-22
No files found.
modules/images.py
View file @
185ab3cb
...
@@ -500,14 +500,11 @@ def save_image(image, path, basename, seed=None, prompt=None, extension='png', i
...
@@ -500,14 +500,11 @@ def save_image(image, path, basename, seed=None, prompt=None, extension='png', i
image
=
params
.
image
image
=
params
.
image
fullfn
=
params
.
filename
fullfn
=
params
.
filename
info
=
params
.
pnginfo
.
get
(
pnginfo_section_name
,
None
)
info
=
params
.
pnginfo
.
get
(
pnginfo_section_name
,
None
)
fullfn_without_extension
,
extension
=
os
.
path
.
splitext
(
params
.
filename
)
def
exif_bytes
():
def
_atomically_save_image
(
image_to_save
,
filename_without_extension
,
extension
):
return
piexif
.
dump
({
# save image with .tmp extension to avoid race condition when another process detects new image in the directory
"Exif"
:
{
temp_file_path
=
filename_without_extension
+
".tmp"
piexif
.
ExifIFD
.
UserComment
:
piexif
.
helper
.
UserComment
.
dump
(
info
or
""
,
encoding
=
"unicode"
)
image_format
=
Image
.
registered_extensions
()[
extension
]
},
})
if
extension
.
lower
()
==
'.png'
:
if
extension
.
lower
()
==
'.png'
:
pnginfo_data
=
PngImagePlugin
.
PngInfo
()
pnginfo_data
=
PngImagePlugin
.
PngInfo
()
...
@@ -515,15 +512,27 @@ def save_image(image, path, basename, seed=None, prompt=None, extension='png', i
...
@@ -515,15 +512,27 @@ def save_image(image, path, basename, seed=None, prompt=None, extension='png', i
for
k
,
v
in
params
.
pnginfo
.
items
():
for
k
,
v
in
params
.
pnginfo
.
items
():
pnginfo_data
.
add_text
(
k
,
str
(
v
))
pnginfo_data
.
add_text
(
k
,
str
(
v
))
image
.
save
(
fullfn
,
quality
=
opts
.
jpeg_quality
,
pnginfo
=
pnginfo_data
)
image_to_save
.
save
(
temp_file_path
,
format
=
image_format
,
quality
=
opts
.
jpeg_quality
,
pnginfo
=
pnginfo_data
)
elif
extension
.
lower
()
in
(
".jpg"
,
".jpeg"
,
".webp"
):
elif
extension
.
lower
()
in
(
".jpg"
,
".jpeg"
,
".webp"
):
image
.
save
(
fullfn
,
quality
=
opts
.
jpeg_quality
)
image_to_save
.
save
(
temp_file_path
,
format
=
image_format
,
quality
=
opts
.
jpeg_quality
)
if
opts
.
enable_pnginfo
and
info
is
not
None
:
if
opts
.
enable_pnginfo
and
info
is
not
None
:
piexif
.
insert
(
exif_bytes
(),
fullfn
)
exif_bytes
=
piexif
.
dump
({
"Exif"
:
{
piexif
.
ExifIFD
.
UserComment
:
piexif
.
helper
.
UserComment
.
dump
(
info
or
""
,
encoding
=
"unicode"
)
},
})
piexif
.
insert
(
exif_bytes
,
temp_file_path
)
else
:
else
:
image
.
save
(
fullfn
,
quality
=
opts
.
jpeg_quality
)
image_to_save
.
save
(
temp_file_path
,
format
=
image_format
,
quality
=
opts
.
jpeg_quality
)
# atomically rename the file with correct extension
os
.
replace
(
temp_file_path
,
filename_without_extension
+
extension
)
fullfn_without_extension
,
extension
=
os
.
path
.
splitext
(
params
.
filename
)
_atomically_save_image
(
image
,
fullfn_without_extension
,
extension
)
image
.
already_saved_as
=
fullfn
image
.
already_saved_as
=
fullfn
...
@@ -537,9 +546,7 @@ def save_image(image, path, basename, seed=None, prompt=None, extension='png', i
...
@@ -537,9 +546,7 @@ def save_image(image, path, basename, seed=None, prompt=None, extension='png', i
elif
oversize
:
elif
oversize
:
image
=
image
.
resize
((
image
.
width
*
target_side_length
//
image
.
height
,
target_side_length
),
LANCZOS
)
image
=
image
.
resize
((
image
.
width
*
target_side_length
//
image
.
height
,
target_side_length
),
LANCZOS
)
image
.
save
(
fullfn_without_extension
+
".jpg"
,
quality
=
opts
.
jpeg_quality
)
_atomically_save_image
(
image
,
fullfn_without_extension
,
".jpg"
)
if
opts
.
enable_pnginfo
and
info
is
not
None
:
piexif
.
insert
(
exif_bytes
(),
fullfn_without_extension
+
".jpg"
)
if
opts
.
save_txt
and
info
is
not
None
:
if
opts
.
save_txt
and
info
is
not
None
:
txt_fullfn
=
f
"{fullfn_without_extension}.txt"
txt_fullfn
=
f
"{fullfn_without_extension}.txt"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment