Reputation: 35
I was wondering how do I open a file using pgmagick (ImageMagick's Python Wrapper), from a URL?
So far I have tried
i) using BytesIO:
from pgmagick import Image
from io import BytesIO
filepath = 'http://example.com/a.jpeg'
response = requests.get(filepath)
im = Image(BytesIO(response.content))
which produced the error output
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) using BytesIO and 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)))
which produced the error output
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) using StringIO and 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)))
which produced the error output
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) using an in-memory file:
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)
which produced the error output
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) using urllib with in memory file:
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)
which produced the error output
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
So none of these methods work.
I have explored several resources online relating to pgmagick and ImageMagick, but the solutions either don't work, like this:
Opening an Image From a URL with pgmagick
or they are in another programming language that isn't Python, like these:
Imagick - Can't read image files from URL.
I'm a bit stuck on this so appreciate any help on this please.
Upvotes: 0
Views: 186