# Trying to write code for random GIF generator

**URL:** <https://discourse.processing.org/t/trying-to-write-code-for-random-gif-generator/41821>\
**Category:** Libraries\
**Created:** [April 25, 2023, 4:03am UTC](https://discourse.processing.org/t/trying-to-write-code-for-random-gif-generator/41821 "2023-04-25T04:03:43Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jbwilke](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jbwilke/32/18300_2.png) [@Jbwilke](https://discourse.processing.org/u/Jbwilke)\
**Post date:** [April 25, 2023, 4:03am UTC](https://discourse.processing.org/t/trying-to-write-code-for-random-gif-generator/41821/1 "2023-04-25T04:03:43Z")

</div>

Hi!

I’m looking for any examples of Processing code (or any leads) that people here might know of for generating random gifs.

I’ve been googling, but haven’t made any progress. It seems there are examples in other languages, but not Processing.

Thank you for your help!

Brad

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [April 25, 2023, 4:50am UTC](https://discourse.processing.org/t/trying-to-write-code-for-random-gif-generator/41821/2 "2023-04-25T04:50:51Z")

</div>

Hi @Jbwilke,

Not really clear from your post what you are trying to achieve, but you can take a look here

(Chapter: EXPORTING A GIF ANIMATION)  
[http://extrapixel.github.io/gif-animation/](http://extrapixel.github.io/gif-animation/)

The library can be installed via Library manager from PDE.

Cheers  
— mnse

---

<div class="post-metadata">

**Author:** ![Jbwilke](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jbwilke/32/18300_2.png) [@Jbwilke](https://discourse.processing.org/u/Jbwilke)\
**Post date:** [April 25, 2023, 5:16am UTC](https://discourse.processing.org/t/trying-to-write-code-for-random-gif-generator/41821/3 "2023-04-25T05:16:44Z")

</div>

Thank you so much, mnse!

I will explore this and see how much headway I can make before coming back to the forum for assistance.

Much obliged!

Brad

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 25, 2023, 11:39am UTC](https://discourse.processing.org/t/trying-to-write-code-for-random-gif-generator/41821/4 "2023-04-25T11:39:44Z")

</div>

> [@Jbwilke](#):
>
> for generating random gifs.

Do you want the animation functionality of a gif or do you just mean image (like jpg)?

For a simple random image you can just place lines  
randomly (or curves):

```auto
strokeWeight(random(0.6, 4.9)); 
stroke( random(256), random(256), random(256));
line(random(width), random (height),
    random(width), random (height));

```

Use a for loop with random upper bound to make many lines

```auto
int upperbound=int(random(3, 444));
for (int i=0; i<upperbound; i++){
   // the lines above
}

```

Then use save or saveFrame(); afterwards

See reference

Chrisir

---

<div class="post-metadata">

**Author:** ![Jbwilke](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jbwilke/32/18300_2.png) [@Jbwilke](https://discourse.processing.org/u/Jbwilke)\
**Post date:** [April 25, 2023, 3:29pm UTC](https://discourse.processing.org/t/trying-to-write-code-for-random-gif-generator/41821/5 "2023-04-25T15:29:58Z")

</div>

Chrisir,

Thank you very much! This is super helpful.

To answer your question, I shouldn’t have used the word “generate” in this context. What I really mean is “pull” pre-existing GIFs from the internet (perhaps from Giphy), display them for a period of time, and then pull another (and another) with a loop function.

I really appreciate your time and thoughtful response, Chrisir!

Brad

---

<div class="post-metadata">

**Author:** ![Jbwilke](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jbwilke/32/18300_2.png) [@Jbwilke](https://discourse.processing.org/u/Jbwilke)\
**Post date:** [May 4, 2023, 7:57pm UTC](https://discourse.processing.org/t/trying-to-write-code-for-random-gif-generator/41821/6 "2023-05-04T19:57:10Z")

</div>

Hello, again!

I made some progress and was hoping someone might be able to answer a question.

Here (attached) is the error message I get when I run the following code.

I’m wondering if you might know how I can get this to display a gif that the API is calling when it needs to recognize it as an image.

 ![Screenshot 2023-05-04 at 12.10.02 PM](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/4/f/4f2d0baa3de6329542cfc40211e4b1a8e8bff1c2.png)  
What I get now is a black screen with that error message. The result I’m hoping for is a gif to be playing in the frame.

Thank you very much!

Brad

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [May 4, 2023, 8:03pm UTC](https://discourse.processing.org/t/trying-to-write-code-for-random-gif-generator/41821/7 "2023-05-04T20:03:17Z")

</div>

Error message says the url string doesn’t end with a supported extension (in your case .gif).

---

<div class="post-metadata">

**Author:** ![Jbwilke](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jbwilke/32/18300_2.png) [@Jbwilke](https://discourse.processing.org/u/Jbwilke)\
**Post date:** [May 4, 2023, 8:11pm UTC](https://discourse.processing.org/t/trying-to-write-code-for-random-gif-generator/41821/8 "2023-05-04T20:11:31Z")

</div>

Yes, I tried adding “.gif” at the end of the URL string and it said it didn’t exist.

I just tried another variation and received the following error:

 ![Screenshot 2023-05-04 at 1.09.57 PM](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/2/a/2a685bfe3b97d20569e652835c80f53fe74588d9.png)

I really appreciate your insight and time here. Thank you!

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [May 4, 2023, 8:20pm UTC](https://discourse.processing.org/t/trying-to-write-code-for-random-gif-generator/41821/9 "2023-05-04T20:20:03Z")

</div>

Hi

```auto
PImage webImg;

void setup() {
  size(400,400);
  //String url = "https://processing.org/img/processing-web.png";
  // Load image from a web server
  webImg = loadImage("https://processing.org/img/processing-web.png");
}

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

```

> **[loadImage() / Reference](https://processing.org/reference/loadImage_.html)**
>
> Loads an image into a variable of type PImage. Four types of images ( .gif, .jpg, .tga, .png) images may be loaded. To load correctly, images must be located in the …

---

<div class="post-metadata">

**Author:** ![Jbwilke](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jbwilke/32/18300_2.png) [@Jbwilke](https://discourse.processing.org/u/Jbwilke)\
**Post date:** [May 4, 2023, 8:33pm UTC](https://discourse.processing.org/t/trying-to-write-code-for-random-gif-generator/41821/10 "2023-05-04T20:33:10Z")

</div>

Thank you, jafal!

I think I am getting closer. End state is that I want to grab a random gif from Giphy when I run this code. Here’s where I’m at now:

 ![Screenshot 2023-05-04 at 1.31.45 PM](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/f/a/faff25780a0b42fa488559d04dfd0309b10efb0e.png)

Any suggestions or ideas? Thank you!

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [May 4, 2023, 8:47pm UTC](https://discourse.processing.org/t/trying-to-write-code-for-random-gif-generator/41821/11 "2023-05-04T20:47:54Z")

</div>

Hi

Looks like you are trying to load gif array not one gif try to load one gif first to make sure that you can load from your site then manage how to load image array

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 4, 2023, 8:59pm UTC](https://discourse.processing.org/t/trying-to-write-code-for-random-gif-generator/41821/12 "2023-05-04T20:59:10Z")

</div>

can you please post your entire code as text here?

---

<div class="post-metadata">

**Author:** ![Jbwilke](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jbwilke/32/18300_2.png) [@Jbwilke](https://discourse.processing.org/u/Jbwilke)\
**Post date:** [May 4, 2023, 9:00pm UTC](https://discourse.processing.org/t/trying-to-write-code-for-random-gif-generator/41821/13 "2023-05-04T21:00:04Z")

</div>

Thank you both. Here is the code:

PImage webImg;

void setup() {  
size(400,400);  
//String url = ““[https://api.giphy.com/v1/gifs/random?api\_key=hSekXK4WNlmjys5ycowpCnQzNlQValQG](https://api.giphy.com/v1/gifs/random?api_key=hSekXK4WNlmjys5ycowpCnQzNlQValQG)””;  
// Load image from a web server  
webImg = loadImage(“[https://api.giphy.com/v1/gifs/random?api\_key=hSekXK4WNlmjys5ycowpCnQzNlQValQG](https://api.giphy.com/v1/gifs/random?api_key=hSekXK4WNlmjys5ycowpCnQzNlQValQG)”);  
}

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 4, 2023, 9:19pm UTC](https://discourse.processing.org/t/trying-to-write-code-for-random-gif-generator/41821/14 "2023-05-04T21:19:37Z")

</div>

as with most API it returns JSON code.

That’s how far I got

It doesn’t work.

I was able to retrieve an url.

When you try to load it, it fails.

But you can paste into your browser and see something.

I didn’t know what to do with

```auto

https://media4.giphy.com/media/J5SaaMHrloUFNVhmFp/giphy.webp?cid=87e71c3e6c3543ddb2ffacd41e906473df24e54d4912ecb7&ep=v1_gifs_random&rid=giphy.webp&ct=g

```

Full Code

( `jsonArray1` ist json3 in fact )

see docs: [Docs | GIPHY Developers](https://developers.giphy.com/docs/api/#quick-start-guide)

```auto

PImage webImg;

JSONObject json;

void setup() {
  size(400, 400);
  //String url = ““https://api.giphy.com/v1/gifs/random?api_key=hSekXK4WNlmjys5ycowpCnQzNlQValQG 1””;

  json = loadJSONObject("https://api.giphy.com/v1/gifs/random?api_key=hSekXK4WNlmjys5ycowpCnQzNlQValQG");

  println(json);

  JSONObject json2 = json.getJSONObject("data");
  println("=========================\n");
  println(json2);

  JSONObject jsonArray1 = json2.getJSONObject("images");

  println("=========================\n");

  println(jsonArray1);

  JSONObject json4 = jsonArray1.getJSONObject("original");
  println("=========================\n");

  println(json4);

  String name1=json4.getString("webp");
  println(name1);

  JSONObject json5 = loadJSONObject(name1);

  println("=========================\n");

  println(json5);

  // Load image from a web server
  webImg = loadImage(name1);
}

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

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 4, 2023, 9:21pm UTC](https://discourse.processing.org/t/trying-to-write-code-for-random-gif-generator/41821/15 "2023-05-04T21:21:15Z")

</div>

or check this out

```auto
// fullscreen image as a screen saver
// from https://discourse.processing.org/t/crop-multiple-images-to-any-quad-shape-and-place-them-side-by-side-filling-the-canvas/41326

int seed = int(random(1000));
PImage img;
int timer;

// -------------------------------------------------------------------------------
// Two core functions

void setup() {
  // size(560, 800);
  fullScreen();

  background(0);
  getRandomImage(width, height);
  timer=millis();
}

void draw() {
  background(0);

  if (millis()-timer>3999) {
    getRandomImage(width, height);
    timer=millis();
  }

  image(img, 0, 0);
}

// -------------------------------------------------------------------------------
// Input functions

void mousePressed() {
  getRandomImage(width, height);
}

// -------------------------------------------------------------------------------
// functions

void getRandomImage(float w, float h) {
  String url = "https://picsum.photos/seed/"
    + seed
    + "/"
    + int(w)
    + "/"
    + int(h)
    + ".jpg";
  // println(url);
  seed += 1;
  img = loadImage(url);
}
//

```

---

<div class="post-metadata">

**Author:** ![Jbwilke](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jbwilke/32/18300_2.png) [@Jbwilke](https://discourse.processing.org/u/Jbwilke)\
**Post date:** [May 4, 2023, 9:37pm UTC](https://discourse.processing.org/t/trying-to-write-code-for-random-gif-generator/41821/16 "2023-05-04T21:37:48Z")

</div>

Chrisir,

This is super helpful. I really appreciate you taking the time to play around with the code and see if you could get it to work. I’m going to keep working on it via trial and error, and I think this code you shared is a huge step in the right direction.

Thank you, again!  
Brad

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [May 4, 2023, 9:42pm UTC](https://discourse.processing.org/t/trying-to-write-code-for-random-gif-generator/41821/17 "2023-05-04T21:42:46Z")

</div>

> [@Jbwilke](#):
>
> This is super helpful. I really appreciate you taking the time to play around with the code and see if you could get it to work. I’m going to keep working on it

Chrisir is our teacher

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [May 5, 2023, 6:59am UTC](https://discourse.processing.org/t/trying-to-write-code-for-random-gif-generator/41821/18 "2023-05-05T06:59:39Z")

</div>

The following seems to work. All I did was add ‘.gif’ to the url.

```auto
PImage webImg;

void setup() {
size(400,400);
webImg = loadImage("https://tenor.com/view/gif-gif-19480288.gif");
}

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

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [May 5, 2023, 8:49am UTC](https://discourse.processing.org/t/trying-to-write-code-for-random-gif-generator/41821/19 "2023-05-05T08:49:39Z")

</div>

Yeah, that’s not an API. Different thing.

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [May 5, 2023, 9:26am UTC](https://discourse.processing.org/t/trying-to-write-code-for-random-gif-generator/41821/20 "2023-05-05T09:26:39Z")

</div>

I’m trying to figure out what he’s trying to do. Why does it have to be an API? What I’ve seen done is download the image(s), create your own animation and then upload that if that’s what you want to do with your work. There is also an animation library for Processing, but have no experience using it.

[Next page](https://discourse.processing.org/t/trying-to-write-code-for-random-gif-generator/41821.md?page=2)
