Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ Unreleased.
- :meth:`Image.region() <wand.image.BaseImage.region>`
- :meth:`Image.splice() <wand.image.BaseImage.splice>`

- Fixed File I/O handling. [:issue:`675`]

- :meth:`Image.ping() <wand.image.Image.ping>`
- :meth:`Image.read() <wand.image.Image.read>`
- :meth:`Image.save() <wand.image.Image.save>`

- [TEST] Added Python 3.12 regression test. [:issue:`648` by Thijs Triemstra]


Expand Down
36 changes: 23 additions & 13 deletions wand/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import numbers
import weakref
from collections import abc
from io import RawIOBase
from io import IOBase

from . import assertions
from .api import libc, libmagick, library
Expand Down Expand Up @@ -9864,9 +9864,12 @@ def ping(cls, file=None, filename=None, blob=None, **kwargs):
instance = cls()
instance._preamble_read(**kwargs)
if file is not None:
if (isinstance(file, RawIOBase) and
hasattr(libc, 'fdopen') and hasattr(file, 'mode')):
fd = libc.fdopen(file.fileno(), file.mode)
is_fd = (isinstance(file, IOBase),
hasattr(libc, 'fdopen'),
callable(getattr(file, 'fileno', None)),
hasattr(file, 'mode'))
if all(is_fd):
fd = libc.fdopen(file.fileno(), binary(file.mode))
r = library.MagickPingImageFile(instance.wand, fd)
elif not callable(getattr(file, 'read', None)):
raise TypeError('file must be a readable file object'
Expand Down Expand Up @@ -10385,9 +10388,12 @@ def read(self, file=None, filename=None, blob=None, background=None,
width=width
)
if file is not None:
if (isinstance(file, RawIOBase) and
hasattr(libc, 'fdopen') and hasattr(file, 'mode')):
fd = libc.fdopen(file.fileno(), file.mode)
is_fd = (isinstance(file, IOBase),
hasattr(libc, 'fdopen'),
callable(getattr(file, 'fileno', None)),
hasattr(file, 'mode'))
if all(is_fd):
fd = libc.fdopen(file.fileno(), binary(file.mode))
r = library.MagickReadImageFile(self.wand, fd)
elif not callable(getattr(file, 'read', None)):
raise TypeError('file must be a readable file object'
Expand Down Expand Up @@ -10454,24 +10460,28 @@ def save(self, file=None, filename=None, adjoin=True):
elif file is not None and filename is not None:
raise TypeError('expected only one argument; but two passed')
elif file is not None:
is_fd = (isinstance(file, IOBase),
hasattr(libc, 'fdopen'),
callable(getattr(file, 'fileno', None)),
hasattr(file, 'mode'))
if isinstance(file, str):
raise TypeError('file must be a writable file object, '
'but {0!r} is a string; did you want '
'.save(filename={0!r})?'.format(file))
elif isinstance(file, RawIOBase) and hasattr(libc, 'fdopen'):
fd = libc.fdopen(file.fileno(), file.mode)
elif all(is_fd):
fd = libc.fdopen(file.fileno(), binary(file.mode))
if library.MagickGetNumberImages(self.wand) > 1:
r = library.MagickWriteImagesFile(self.wand, fd)
else:
r = library.MagickWriteImageFile(self.wand, fd)
libc.fflush(fd)
if not r:
self.raise_exception()
elif not callable(getattr(file, 'write', None)):
raise TypeError('file must be a writable file object, '
'but it does not have write() method: ' +
repr(file))
else:
if not callable(getattr(file, 'write', None)):
raise TypeError('file must be a writable file object, '
'but it does not have write() method: ' +
repr(file))
file.write(self.make_blob())
else:
if not isinstance(filename, str):
Expand Down