How to pull an image from a website

I want to use an image from a website as the basis for an interactive artwork. For example, this page

is a google logo.

This script just gives a ‘loading’ label

let img;

function preload() {
 img = loadImage(url);
function setup() {
  createCanvas(640, 480);
function draw() {
  // put drawing code here
   image(img, 0, 0);

If I have a url to a specific image, how can I copy it to a variable so I can use it with p5.js?

I have successfuly set up a local server btw using sublimetext and browser sync. If I copy the image to my sketch folder I can use it with ‘loadImage(‘logo.png’)’ just fine. I am trying to skip that step so that the user can navigate to the website of their choice, grab an image and draw on it.

I believe the server hosting whatever image you are trying to get has to be configured to allow requests from another domain and they typically don’t.

If you look at your browser console when you try to run that it might give you an error with a link like the following explaining it a bit…

try this

PImage webImg;

void setup() {
  size(800, 600);
  String url = "";
  webImg = loadImage(url, "png");

void draw() {
  image(webImg, 0, 0);

EDIT: sorry, missed the p5.js mention

no but this code works in the sketch

function add_google_logo() {
        var src = "";
        show_image("", 276,110, "Google Logo");

function show_image(src, width, height, alt) {
        var img = document.createElement('img');
        img.src = src;
        img.width = width;
        img.height = height;
        img.alt = alt;

and this is the html

<!DOCTYPE html>
<script src="p5/p5.js"></script>
<script src="p5/addons/p5.dom.js"></script>
<script src="sketch10.js"></script>


<!--var test=''-->
<button type="button" onclick="add_google_logo()">Try it-uses add_google_logo function</button>

<button type="button" onclick="show_image('', 276,110, 'Google Logo')">uses show_image function</button>

This demonstrates two different ways to get the image. First by referencing the url only in the function. Second, by passing the url as a variable in the function. Dont ask me tho if it passes by value or passes by reference-I dont know enough about p5.js to answer that.