Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
FSL
fsleyes
fsleyes-widgets
Commits
ea2c7203
Commit
ea2c7203
authored
Oct 21, 2018
by
Paul McCarthy
🚵
Browse files
ENH: ImagePanel has option to preserve aspect ratio
parent
b6638fa6
Changes
1
Hide whitespace changes
Inline
Side-by-side
fsleyes_widgets/imagepanel.py
View file @
ea2c7203
...
...
@@ -17,15 +17,21 @@ class ImagePanel(wx.Panel):
:class:`wx.Image`. The image is scaled to the size of the panel.
"""
def
__init__
(
self
,
parent
,
image
=
None
):
def
__init__
(
self
,
parent
,
image
=
None
,
preserveAspect
=
False
):
"""Create an ``ImagePanel``.
If the ``image`` is not passed in here, it can be set later with the
:meth:`SetImage` method.
:arg parent: The :mod:`wx` parent object.
:arg parent:
The :mod:`wx` parent object.
:arg image: The :class:`wx.Image` object to display.
:arg image: The :class:`wx.Image` object to display.
:arg preserveAspect: Defaults to ``False``. If ``True``, the image
aspect ratio is preserved.
"""
wx
.
Panel
.
__init__
(
self
,
parent
)
...
...
@@ -35,6 +41,8 @@ class ImagePanel(wx.Panel):
self
.
SetImage
(
image
)
self
.
__preserveAspect
=
preserveAspect
def
SetImage
(
self
,
image
):
"""Set the image that is displayed on this ``ImagePanel``.
...
...
@@ -70,11 +78,26 @@ class ImagePanel(wx.Panel):
if
not
dc
.
IsOk
():
return
width
,
height
=
dc
.
GetSize
().
Get
()
d
width
,
d
height
=
dc
.
GetSize
().
Get
()
if
width
==
0
or
height
==
0
:
if
d
width
==
0
or
d
height
==
0
:
return
bitmap
=
self
.
__image
.
Scale
(
width
,
height
).
ConvertToBitmap
()
if
self
.
__preserveAspect
:
iwidth
,
iheight
=
self
.
__image
.
GetSize
().
Get
()
iratio
=
float
(
iwidth
)
/
iheight
dratio
=
float
(
dwidth
)
/
dheight
# canvas is too wide - reduce
# the display image width
if
dratio
>
iratio
:
dwidth
=
dheight
/
iratio
# canvas is too tall - reduce
# the display image height
elif
dratio
<
iratio
:
dheight
=
dwidth
*
iratio
bitmap
=
self
.
__image
.
Scale
(
dwidth
,
dheight
).
ConvertToBitmap
()
dc
.
DrawBitmap
(
bitmap
,
0
,
0
,
False
)
Write
Preview
Supports
Markdown
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