我想知道如何从 URL 使用 pgmagick(ImageMagick 的 Python Wrapper)打开文件?
到目前为止我已经尝试过
i) 使用 BytesIO:
from pgmagick import Image
from io import BytesIO
filepath = 'http://example.com/a.jpeg'
response = requests.get(filepath)
im = Image(BytesIO(response.content))
产生错误输出
Traceback (most recent call last):
File "test.py", line 13, in <module>
im = magick_image(BytesIO(response.content))
Boost.Python.ArgumentError: Python argument types in
Image.__init__(Image, _io.BytesIO)
did not match C++ signature:
__init__(struct _object * __ptr64, class Magick::Image)
__init__(class boost::python::api::object, unsigned int, unsigned int, class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >, enum MagickLib::StorageType, class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)
__init__(struct _object * __ptr64, unsigned int, unsigned int, class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >, enum MagickLib::StorageType, char const * __ptr64)
__init__(struct _object * __ptr64, class Magick::Blob, class Magick::Geometry, class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)
__init__(struct _object * __ptr64, class Magick::Blob, class Magick::Geometry, unsigned int, class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)
__init__(struct _object * __ptr64, class Magick::Blob, class Magick::Geometry, unsigned int)
__init__(struct _object * __ptr64, class Magick::Blob, class Magick::Geometry)
__init__(struct _object * __ptr64, class Magick::Blob)
__init__(struct _object * __ptr64, class Magick::Geometry, class Magick::Color)
__init__(struct _object * __ptr64, class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)
__init__(struct _object * __ptr64)
Process finished with exit code 1
ii) 使用 BytesIO 和 Blob:
from pgmagick import Image, Blob
from io import BytesIO
filepath = 'http://example.com/a.jpeg'
response = requests.get(filepath)
im = Image(Blob(BytesIO(response.content)))
产生错误输出
Traceback (most recent call last):
File "test.py", line 14, in <module>
im = magick_image(Blob(BytesIO(response.content)))
File "lib\site-packages\pgmagick\__init__.py", line 16, in __init__
_pgmagick.Blob.__init__(self, *args)
Boost.Python.ArgumentError: Python argument types in
Blob.__init__(Blob, _io.BytesIO)
did not match C++ signature:
__init__(struct _object * __ptr64, class Magick::Blob)
__init__(class Magick::Blob {lvalue}, class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)
__init__(struct _object * __ptr64)
Process finished with exit code 1
iii) 使用 StringIO 和 Blob:
from pgmagick import Image, Blob
from io import StringIO
filepath = 'http://example.com/a.jpeg'
response = requests.get(filepath)
img = Image(Blob(StringIO(response.content)))
产生错误输出
Traceback (most recent call last):
File "test.py", line 14, in <module>
img = magick_image(Blob(StringIO(response.content)))
TypeError: initial_value must be str or None, not bytes
Process finished with exit code 1
iv) 使用内存文件:
from pgmagick import Image
from io import BytesIO
filepath = 'http://example.com/a.jpeg'
response = requests.get(filepath)
in_mem_file = io.BytesIO(response.content)
im = Image(in_mem_file)
产生错误输出
Traceback (most recent call last):
File "test.py", line 15, in <module>
im = magick_image(in_mem_file)
Boost.Python.ArgumentError: Python argument types in
Image.__init__(Image, _io.BytesIO)
did not match C++ signature:
__init__(struct _object * __ptr64, class Magick::Image)
__init__(class boost::python::api::object, unsigned int, unsigned int, class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >, enum MagickLib::StorageType, class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)
__init__(struct _object * __ptr64, unsigned int, unsigned int, class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >, enum MagickLib::StorageType, char const * __ptr64)
__init__(struct _object * __ptr64, class Magick::Blob, class Magick::Geometry, class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)
__init__(struct _object * __ptr64, class Magick::Blob, class Magick::Geometry, unsigned int, class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)
__init__(struct _object * __ptr64, class Magick::Blob, class Magick::Geometry, unsigned int)
__init__(struct _object * __ptr64, class Magick::Blob, class Magick::Geometry)
__init__(struct _object * __ptr64, class Magick::Blob)
__init__(struct _object * __ptr64, class Magick::Geometry, class Magick::Color)
__init__(struct _object * __ptr64, class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)
__init__(struct _object * __ptr64)
Process finished with exit code 1
v) 在内存文件中使用 urllib:
from pgmagick import Image
from io import BytesIO
import urllib.request
filepath = 'http://example.com/a.jpeg'
in_mem_file = io.BytesIO()
urllib.request.urlretrieve(filepath, in_mem_file)
im = Image(in_mem_file)
产生错误输出
Traceback (most recent call last):
File "test.py", line 14, in <module>
urllib.request.urlretrieve(filepath, in_mem_file)
File "C:\Python38\lib\urllib\request.py", line 257, in urlretrieve
tfp = open(filename, 'wb')
TypeError: expected str, bytes or os.PathLike object, not _io.BytesIO
Process finished with exit code 1
所以这些方法都不起作用。
我已经在网上探索了几个与 pgmagick 和 ImageMagick 相关的资源,但是解决方案要么不起作用,就像这样:
或者它们使用另一种不是 Python 的编程语言,例如:
我对此有点坚持,所以请对此提供任何帮助。