Problem loading image from URL

I’m trying to display some images from URLs on the internet.

I downloaded ofxJson and built one of the examples, which displayed some images from URLs that were fetched from Flickr’s API.

Then I changed the API url to my URL, which returns an array of URLs as a response.

I compared the URLs from the sample Flickr app (which works correctly) to the URLs from my API and I can’t see why it won’t load my images, as they are valid URLs for jpgs.

This is only the second thing I’ve tried to make with OpenFrameworks, so I thought I’d ask if anyone more experienced can spot what the problem is.

Here is the relevant code:

void ofApp::setup()
{
    ofSetFrameRate(24);

    std::string url = "http://www.thebos.co/events/api/evdyannualparty2014/2189";

    if (!response.open(url))
    {
        ofLogNotice("ofApp::setup") << "Failed to parse JSON";
    }

    unsigned int numImages = MIN(10, response.size());
    
    for(unsigned int i = 0; i < numImages; i++)
    {
        string url = response[i].asString();
        
        // print the URL for each image
        cout << "The URL is:                                               " << url << '\n';

        ofImage img;
        img.loadImage(url);
        images.push_back(img);

    }
    
}

And here is the output from the console:

The URL is:                                               https://s3.amazonaws.com/thebosco/events/evdyannualparty2014/0025-JKDJLQ_400.jpg
[ error ] ofImage: loadImage(): couldn't load image from "https://s3.amazonaws.com/thebosco/events/evdyannualparty2014/0025-JKDJLQ_400.jpg"
The URL is:                                               https://s3.amazonaws.com/thebosco/events/evdyannualparty2014/0024-80LPE5_400.jpg
[ error ] ofImage: loadImage(): couldn't load image from "https://s3.amazonaws.com/thebosco/events/evdyannualparty2014/0024-80LPE5_400.jpg"
The URL is:                                               https://s3.amazonaws.com/thebosco/events/evdyannualparty2014/0023-SZWCBD_400.jpg
[ error ] ofImage: loadImage(): couldn't load image from "https://s3.amazonaws.com/thebosco/events/evdyannualparty2014/0023-SZWCBD_400.jpg"
The URL is:                                               https://s3.amazonaws.com/thebosco/events/evdyannualparty2014/0022-2BVWKR_400.jpg
[ error ] ofImage: loadImage(): couldn't load image from "https://s3.amazonaws.com/thebosco/events/evdyannualparty2014/0022-2BVWKR_400.jpg"
The URL is:                                               https://s3.amazonaws.com/thebosco/events/evdyannualparty2014/0021-YAOJVX_400.jpg
[ error ] ofImage: loadImage(): couldn't load image from "https://s3.amazonaws.com/thebosco/events/evdyannualparty2014/0021-YAOJVX_400.jpg"
The URL is:                                               https://s3.amazonaws.com/thebosco/events/evdyannualparty2014/0020-PZX2IF_400.jpg
[ error ] ofImage: loadImage(): couldn't load image from "https://s3.amazonaws.com/thebosco/events/evdyannualparty2014/0020-PZX2IF_400.jpg"
The URL is:                                               https://s3.amazonaws.com/thebosco/events/evdyannualparty2014/0019-0Z4GTL_400.jpg
[ error ] ofImage: loadImage(): couldn't load image from "https://s3.amazonaws.com/thebosco/events/evdyannualparty2014/0019-0Z4GTL_400.jpg"
The URL is:                                               https://s3.amazonaws.com/thebosco/events/evdyannualparty2014/0018-EP5LHS_400.jpg
[ error ] ofImage: loadImage(): couldn't load image from "https://s3.amazonaws.com/thebosco/events/evdyannualparty2014/0018-EP5LHS_400.jpg"
The URL is:                                               https://s3.amazonaws.com/thebosco/events/evdyannualparty2014/0016-381KY4_400.jpg
[ error ] ofImage: loadImage(): couldn't load image from "https://s3.amazonaws.com/thebosco/events/evdyannualparty2014/0016-381KY4_400.jpg"
The URL is:                                               https://s3.amazonaws.com/thebosco/events/evdyannualparty2014/0016-J192D2_400.jpg
[ error ] ofImage: loadImage(): couldn't load image from "https://s3.amazonaws.com/thebosco/events/evdyannualparty2014/0016-J192D2_400.jpg"

It looks like something funky is happening behind-the-scenes between ofImage and ofURLFileLoader when you try to load from a url. I get a [ error ] ofPixels: format doesn't support channels when I run your code, so it sounds like maybe the image format isn’t being detect.

Here’s one solution. When you go to load your image, try this:

ofImage img;
ofHttpResponse resp = ofLoadURL(url);
img.loadImage(resp.data);
images.push_back(img);

Hopefully someone else can chime in and illuminate what’s going on here.

Oops, nevermind - figured it out

Here’s the issue. ofImage looks at your url and checks to see whether it begins with http://. If it does, then it will do the appropriate calls to load an image from a URL. Your URLs are HTTPS, so ofImage parses them as if they were a file location on your computer.

Your images are accessible via HTTP, so you could modify your array of URLs to be HTTP, or you could use the ofLoadURL() solution from my last post.

That’s a bug in my book :slight_smile: Fix and PR?

Should probably be approached with Poco::URI like this:

1 Like

I think it was probably never updated when we got SSL support a little while back.

Ah, wish I had seen that before submitting a bug.

1 Like

Thanks so much for all of the excellent and quick responses!!!

I have it working using ofHttpResponse.

Really loving ofWorld so far and thanks again!

2 Likes

@andrewlandry +1 to ofWorld :wink:

I am getting the same error still in 0.9.2 on OSX. I think there is a problem when the response code is 301 (permanently moved).

        ofImage img;
        ofHttpResponse resp = ofLoadURL("http://fittoteach.files.wordpress.com/2012/06/sunny-day.jpg");
        cout<<ofToString(resp.status)<<endl;
        img.load(resp.data);

However trying a different file, “http://1.bp.blogspot.com/-8PQ_NKjf5MI/UK_iuF92QUI/AAAAAAAAAYI/GV0NCn74Ly0/s1600/oh-happy-day.jpg” that gives a resp code of 200 works fine. But I can load both links in the browser…Can oF not follow the redirects?

Unfortunately ofLoadURL doesn’t follow redirects … and currently ofHttpResponse doesn’t capture response headers so you can’t easily look at the Location header and follow a redirect.

Luckily, ofxHTTP has clients that follow redirects, so I’d recommend checking those out. e.g.

https://github.com/bakercp/ofxHTTP/blob/master/example_basic_client_loader/src/ofApp.cpp