2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# The Python Imaging Library.
|
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
# a Windows DIB display interface
|
|
|
|
#
|
|
|
|
# History:
|
|
|
|
# 1996-05-20 fl Created
|
|
|
|
# 1996-09-20 fl Fixed subregion exposure
|
|
|
|
# 1997-09-21 fl Added draw primitive (for tzPrint)
|
|
|
|
# 2003-05-21 fl Added experimental Window/ImageWindow classes
|
|
|
|
# 2003-09-05 fl Added fromstring/tostring methods
|
|
|
|
#
|
|
|
|
# Copyright (c) Secret Labs AB 1997-2003.
|
|
|
|
# Copyright (c) Fredrik Lundh 1996-2003.
|
|
|
|
#
|
|
|
|
# See the README file for information on usage and redistribution.
|
|
|
|
#
|
|
|
|
|
2013-03-07 20:20:28 +04:00
|
|
|
from PIL import Image
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
|
2015-05-26 17:07:21 +03:00
|
|
|
class HDC(object):
|
2013-10-14 09:19:12 +04:00
|
|
|
"""
|
2014-07-06 01:43:22 +04:00
|
|
|
Wraps an HDC integer. The resulting object can be passed to the
|
2013-10-14 09:19:12 +04:00
|
|
|
:py:meth:`~PIL.ImageWin.Dib.draw` and :py:meth:`~PIL.ImageWin.Dib.expose`
|
|
|
|
methods.
|
|
|
|
"""
|
2010-07-31 06:52:47 +04:00
|
|
|
def __init__(self, dc):
|
|
|
|
self.dc = dc
|
2014-07-06 01:43:22 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def __int__(self):
|
|
|
|
return self.dc
|
|
|
|
|
2014-07-06 01:43:22 +04:00
|
|
|
|
2015-05-26 17:07:21 +03:00
|
|
|
class HWND(object):
|
2013-10-14 09:19:12 +04:00
|
|
|
"""
|
2014-07-06 01:43:22 +04:00
|
|
|
Wraps an HWND integer. The resulting object can be passed to the
|
2013-10-14 09:19:12 +04:00
|
|
|
:py:meth:`~PIL.ImageWin.Dib.draw` and :py:meth:`~PIL.ImageWin.Dib.expose`
|
|
|
|
methods, instead of a DC.
|
|
|
|
"""
|
2010-07-31 06:52:47 +04:00
|
|
|
def __init__(self, wnd):
|
|
|
|
self.wnd = wnd
|
2014-07-06 01:43:22 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def __int__(self):
|
|
|
|
return self.wnd
|
|
|
|
|
|
|
|
|
2015-05-26 17:07:21 +03:00
|
|
|
class Dib(object):
|
2013-10-14 09:19:12 +04:00
|
|
|
"""
|
|
|
|
A Windows bitmap with the given mode and size. The mode can be one of "1",
|
|
|
|
"L", "P", or "RGB".
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2013-10-14 09:19:12 +04:00
|
|
|
If the display requires a palette, this constructor creates a suitable
|
|
|
|
palette and associates it with the image. For an "L" image, 128 greylevels
|
|
|
|
are allocated. For an "RGB" image, a 6x6x6 colour cube is used, together
|
|
|
|
with 20 greylevels.
|
|
|
|
|
|
|
|
To make sure that palettes work properly under Windows, you must call the
|
|
|
|
**palette** method upon certain events from Windows.
|
|
|
|
|
|
|
|
:param image: Either a PIL image, or a mode string. If a mode string is
|
|
|
|
used, a size must also be given. The mode can be one of "1",
|
|
|
|
"L", "P", or "RGB".
|
|
|
|
:param size: If the first argument is a mode string, this
|
|
|
|
defines the size of the image.
|
|
|
|
"""
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def __init__(self, image, size=None):
|
|
|
|
if hasattr(image, "mode") and hasattr(image, "size"):
|
|
|
|
mode = image.mode
|
|
|
|
size = image.size
|
|
|
|
else:
|
|
|
|
mode = image
|
|
|
|
image = None
|
|
|
|
if mode not in ["1", "L", "P", "RGB"]:
|
|
|
|
mode = Image.getmodebase(mode)
|
|
|
|
self.image = Image.core.display(mode, size)
|
|
|
|
self.mode = mode
|
|
|
|
self.size = size
|
|
|
|
if image:
|
|
|
|
self.paste(image)
|
|
|
|
|
|
|
|
def expose(self, handle):
|
2013-10-14 09:19:12 +04:00
|
|
|
"""
|
|
|
|
Copy the bitmap contents to a device context.
|
|
|
|
|
2014-07-06 01:43:22 +04:00
|
|
|
:param handle: Device context (HDC), cast to a Python integer, or an
|
|
|
|
HDC or HWND instance. In PythonWin, you can use the
|
2013-10-14 09:19:12 +04:00
|
|
|
:py:meth:`CDC.GetHandleAttrib` to get a suitable handle.
|
|
|
|
"""
|
2010-07-31 06:52:47 +04:00
|
|
|
if isinstance(handle, HWND):
|
|
|
|
dc = self.image.getdc(handle)
|
|
|
|
try:
|
|
|
|
result = self.image.expose(dc)
|
|
|
|
finally:
|
|
|
|
self.image.releasedc(handle, dc)
|
|
|
|
else:
|
|
|
|
result = self.image.expose(handle)
|
|
|
|
return result
|
|
|
|
|
|
|
|
def draw(self, handle, dst, src=None):
|
2013-10-14 09:19:12 +04:00
|
|
|
"""
|
|
|
|
Same as expose, but allows you to specify where to draw the image, and
|
|
|
|
what part of it to draw.
|
|
|
|
|
|
|
|
The destination and source areas are given as 4-tuple rectangles. If
|
|
|
|
the source is omitted, the entire image is copied. If the source and
|
|
|
|
the destination have different sizes, the image is resized as
|
|
|
|
necessary.
|
|
|
|
"""
|
2010-07-31 06:52:47 +04:00
|
|
|
if not src:
|
2014-07-06 01:43:22 +04:00
|
|
|
src = (0, 0) + self.size
|
2010-07-31 06:52:47 +04:00
|
|
|
if isinstance(handle, HWND):
|
|
|
|
dc = self.image.getdc(handle)
|
|
|
|
try:
|
|
|
|
result = self.image.draw(dc, dst, src)
|
|
|
|
finally:
|
|
|
|
self.image.releasedc(handle, dc)
|
|
|
|
else:
|
|
|
|
result = self.image.draw(handle, dst, src)
|
|
|
|
return result
|
|
|
|
|
|
|
|
def query_palette(self, handle):
|
2013-10-14 09:19:12 +04:00
|
|
|
"""
|
|
|
|
Installs the palette associated with the image in the given device
|
|
|
|
context.
|
|
|
|
|
|
|
|
This method should be called upon **QUERYNEWPALETTE** and
|
|
|
|
**PALETTECHANGED** events from Windows. If this method returns a
|
|
|
|
non-zero value, one or more display palette entries were changed, and
|
|
|
|
the image should be redrawn.
|
|
|
|
|
|
|
|
:param handle: Device context (HDC), cast to a Python integer, or an
|
|
|
|
HDC or HWND instance.
|
|
|
|
:return: A true value if one or more entries were changed (this
|
|
|
|
indicates that the image should be redrawn).
|
|
|
|
"""
|
2010-07-31 06:52:47 +04:00
|
|
|
if isinstance(handle, HWND):
|
|
|
|
handle = self.image.getdc(handle)
|
|
|
|
try:
|
|
|
|
result = self.image.query_palette(handle)
|
|
|
|
finally:
|
|
|
|
self.image.releasedc(handle, handle)
|
|
|
|
else:
|
|
|
|
result = self.image.query_palette(handle)
|
|
|
|
return result
|
|
|
|
|
|
|
|
def paste(self, im, box=None):
|
2013-10-14 09:19:12 +04:00
|
|
|
"""
|
|
|
|
Paste a PIL image into the bitmap image.
|
|
|
|
|
|
|
|
:param im: A PIL image. The size must match the target region.
|
|
|
|
If the mode does not match, the image is converted to the
|
|
|
|
mode of the bitmap image.
|
|
|
|
:param box: A 4-tuple defining the left, upper, right, and
|
|
|
|
lower pixel coordinate. If None is given instead of a
|
|
|
|
tuple, all of the image is assumed.
|
|
|
|
"""
|
2010-07-31 06:52:47 +04:00
|
|
|
im.load()
|
|
|
|
if self.mode != im.mode:
|
|
|
|
im = im.convert(self.mode)
|
|
|
|
if box:
|
|
|
|
self.image.paste(im.im, box)
|
|
|
|
else:
|
|
|
|
self.image.paste(im.im)
|
|
|
|
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
def frombytes(self, buffer):
|
2013-10-14 09:19:12 +04:00
|
|
|
"""
|
|
|
|
Load display memory contents from byte data.
|
|
|
|
|
|
|
|
:param buffer: A buffer containing display data (usually
|
|
|
|
data returned from <b>tobytes</b>)
|
|
|
|
"""
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
return self.image.frombytes(buffer)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
def tobytes(self):
|
2013-10-14 09:19:12 +04:00
|
|
|
"""
|
|
|
|
Copy display memory contents to bytes object.
|
|
|
|
|
|
|
|
:return: A bytes object containing display data.
|
|
|
|
"""
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
return self.image.tobytes()
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2015-09-19 14:36:19 +03:00
|
|
|
def fromstring(self, *args, **kw):
|
2016-05-06 13:11:03 +03:00
|
|
|
raise NotImplementedError("fromstring() has been removed. " +
|
|
|
|
"Please use frombytes() instead.")
|
2015-09-19 14:36:19 +03:00
|
|
|
|
|
|
|
def tostring(self, *args, **kw):
|
2016-05-06 13:11:03 +03:00
|
|
|
raise NotImplementedError("tostring() has been removed. " +
|
|
|
|
"Please use tobytes() instead.")
|
2015-09-19 14:36:19 +03:00
|
|
|
|
2014-07-06 01:43:22 +04:00
|
|
|
|
2015-05-26 17:07:21 +03:00
|
|
|
class Window(object):
|
2016-05-24 10:36:14 +03:00
|
|
|
"""Create a Window with the given title size."""
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def __init__(self, title="PIL", width=None, height=None):
|
|
|
|
self.hwnd = Image.core.createwindow(
|
|
|
|
title, self.__dispatcher, width or 0, height or 0
|
|
|
|
)
|
|
|
|
|
|
|
|
def __dispatcher(self, action, *args):
|
2012-10-21 01:05:13 +04:00
|
|
|
return getattr(self, "ui_handle_" + action)(*args)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def ui_handle_clear(self, dc, x0, y0, x1, y1):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def ui_handle_damage(self, x0, y0, x1, y1):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def ui_handle_destroy(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def ui_handle_repair(self, dc, x0, y0, x1, y1):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def ui_handle_resize(self, width, height):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def mainloop(self):
|
|
|
|
Image.core.eventloop()
|
|
|
|
|
2014-07-06 01:43:22 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
class ImageWindow(Window):
|
2016-05-24 10:36:14 +03:00
|
|
|
"""Create an image window which displays the given image."""
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
def __init__(self, image, title="PIL"):
|
|
|
|
if not isinstance(image, Dib):
|
|
|
|
image = Dib(image)
|
|
|
|
self.image = image
|
|
|
|
width, height = image.size
|
|
|
|
Window.__init__(self, title, width=width, height=height)
|
|
|
|
|
|
|
|
def ui_handle_repair(self, dc, x0, y0, x1, y1):
|
|
|
|
self.image.draw(dc, (x0, y0, x1, y1))
|
2016-05-24 10:36:14 +03:00
|
|
|
|
|
|
|
# End of file
|