1

In pgmagick, you initialize an image like this:

Image('my_image.png')

I will be operating on files stored remotely on S3 and would rather not temporarily store them on disk. Is there any way to open an image file from a URL instead? When I try to simply replace the file name with the URL, I get an error: Unable to open file.

I'd like to be able to use a URL. If anyone has any suggestions on that or how to extend pgmagick to achieve it, I'd be elated.

user1427661
  • 11,158
  • 28
  • 90
  • 132

1 Answers1

1

The easiest way (in my mind) is to use the awesome requests library. You can fetch each image from the server one at a time, then open it with Image():

from StringIO import StringIO
import requests
from pgmagick import Image, Blob

r = requests.get('https://server.com/path/to/image1.png', auth=('user', 'pass'))
img = Image(Blob(StringIO(r.content)))

And that's all there is to it. Authentication is of course not required, but may be necessary depending on your S3 setup. Have fun!

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • Ah, but this won't work unfortunately. pgmagick's Image is looking for a filename, which it then uses to open the file. In the above example, the Image class will quite unsuccessfully scan the UNIX filesystem for a file that matches the blob contents passed in. – user1427661 Apr 02 '14 at 22:25
  • @user1427661 - so is there no way to open raw data directly? `Pillow` can do that just fine - maybe it would work better for your needs? – MattDMo Apr 02 '14 at 22:27
  • @user1427661 - looking over the docs I found [this example](http://pythonhosted.org/pgmagick/cookbook.html#scaling-a-jpeg-image), which calls the `Blob()` method/class on the results of a Python `open()` command. I just updated my example above, why don't you see if it works now? – MattDMo Apr 02 '14 at 22:34
  • Ahah! That did the trick. Now the only question is how to write the file back to S3. pgmagick apparently doesn't make it super easy to access the blob contents of the edited image. – user1427661 Apr 02 '14 at 22:54