Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
S
Stable Diffusion Webui
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
novelai-storage
Stable Diffusion Webui
Commits
33fbe943
Commit
33fbe943
authored
Mar 04, 2024
by
AUTOMATIC1111
Committed by
GitHub
Mar 04, 2024
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #15062 from astriaai/fix-exif-orientation-api
Fix EXIF orientation in API image loading
parents
92d77e3f
67d8dafe
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
0 deletions
+51
-0
modules/api/api.py
modules/api/api.py
+2
-0
modules/images.py
modules/images.py
+49
-0
No files found.
modules/api/api.py
View file @
33fbe943
...
@@ -86,6 +86,7 @@ def decode_base64_to_image(encoding):
...
@@ -86,6 +86,7 @@ def decode_base64_to_image(encoding):
response
=
requests
.
get
(
encoding
,
timeout
=
30
,
headers
=
headers
)
response
=
requests
.
get
(
encoding
,
timeout
=
30
,
headers
=
headers
)
try
:
try
:
image
=
Image
.
open
(
BytesIO
(
response
.
content
))
image
=
Image
.
open
(
BytesIO
(
response
.
content
))
image
=
images
.
apply_exif_orientation
(
image
)
return
image
return
image
except
Exception
as
e
:
except
Exception
as
e
:
raise
HTTPException
(
status_code
=
500
,
detail
=
"Invalid image url"
)
from
e
raise
HTTPException
(
status_code
=
500
,
detail
=
"Invalid image url"
)
from
e
...
@@ -94,6 +95,7 @@ def decode_base64_to_image(encoding):
...
@@ -94,6 +95,7 @@ def decode_base64_to_image(encoding):
encoding
=
encoding
.
split
(
";"
)[
1
]
.
split
(
","
)[
1
]
encoding
=
encoding
.
split
(
";"
)[
1
]
.
split
(
","
)[
1
]
try
:
try
:
image
=
Image
.
open
(
BytesIO
(
base64
.
b64decode
(
encoding
)))
image
=
Image
.
open
(
BytesIO
(
base64
.
b64decode
(
encoding
)))
image
=
images
.
apply_exif_orientation
(
image
)
return
image
return
image
except
Exception
as
e
:
except
Exception
as
e
:
raise
HTTPException
(
status_code
=
500
,
detail
=
"Invalid encoded image"
)
from
e
raise
HTTPException
(
status_code
=
500
,
detail
=
"Invalid encoded image"
)
from
e
...
...
modules/images.py
View file @
33fbe943
...
@@ -800,3 +800,52 @@ def flatten(img, bgcolor):
...
@@ -800,3 +800,52 @@ def flatten(img, bgcolor):
return
img
.
convert
(
'RGB'
)
return
img
.
convert
(
'RGB'
)
# https://www.exiv2.org/tags.html
_EXIF_ORIENT
=
274
# exif 'Orientation' tag
def
apply_exif_orientation
(
image
):
"""
Applies the exif orientation correctly.
This code exists per the bug:
https://github.com/python-pillow/Pillow/issues/3973
with the function `ImageOps.exif_transpose`. The Pillow source raises errors with
various methods, especially `tobytes`
Function based on:
https://github.com/wkentaro/labelme/blob/v4.5.4/labelme/utils/image.py#L59
https://github.com/python-pillow/Pillow/blob/7.1.2/src/PIL/ImageOps.py#L527
Args:
image (PIL.Image): a PIL image
Returns:
(PIL.Image): the PIL image with exif orientation applied, if applicable
"""
if
not
hasattr
(
image
,
"getexif"
):
return
image
try
:
exif
=
image
.
getexif
()
except
Exception
:
# https://github.com/facebookresearch/detectron2/issues/1885
exif
=
None
if
exif
is
None
:
return
image
orientation
=
exif
.
get
(
_EXIF_ORIENT
)
method
=
{
2
:
Image
.
FLIP_LEFT_RIGHT
,
3
:
Image
.
ROTATE_180
,
4
:
Image
.
FLIP_TOP_BOTTOM
,
5
:
Image
.
TRANSPOSE
,
6
:
Image
.
ROTATE_270
,
7
:
Image
.
TRANSVERSE
,
8
:
Image
.
ROTATE_90
,
}
.
get
(
orientation
)
if
method
is
not
None
:
return
image
.
transpose
(
method
)
return
image
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