# Exporting Barnsley fern to SVG

**URL:** https://discourse.processing.org/t/exporting-barnsley-fern-to-svg/12857
**Category:** Coding Questions
**Created:** [July 21, 2019, 6:49am UTC](https://discourse.processing.org/t/exporting-barnsley-fern-to-svg/12857 "2019-07-21T06:49:30Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![dr\_vblschrf](https://avatars.discourse-cdn.com/v4/letter/d/8dc957/32.png) [@dr\_vblschrf](https://discourse.processing.org/u/dr_vblschrf)
#### Post date: [July 21, 2019, 6:49am UTC](https://discourse.processing.org/t/exporting-barnsley-fern-to-svg/12857/1 "2019-07-21T06:49:30Z")

</div>

Hello. I’m going to start by saying that I’m very new to coding. Sorry for my inexperience!

I’m trying to export a Barnsley fern code I found from Rosetta Code to an SVG. It needs to be SVG as it is for a vector art project that is not compatible with PDF files. I’ve tried using the methods found [here](https://processing.org/reference/libraries/svg/index.html) but the problem seems to be the set line. Running the code as-is gives a “RuntimeException: No set() for PGraphicsSVG” error message.

I am admittedly out of my depth here, so I’m hoping that someone here will be able to help. I’ve pasted the code as I’ve tried to edit it:

```auto
import processing.svg.*;

void setup() {
  size(325, 725);
  background(219, 226, 233);
  noLoop();
  beginRecord(SVG, "Fern.svg");
}
 
float x = 0;
float y = 0;
 
void draw() {
 
  for (int i = 0; i < 155000000; i++) {
 
    float xt = 0;
    float yt = 0;
 
    float r = random(100);
 
    if (r <= 1) {
      xt = 0;
      yt = 0.16*y;
    } else if (r <= 8) {
      xt = 0.20*x - 0.26*y;
      yt = 0.23*x + 0.22*y + 1.60;
    } else if (r <= 15) {
      xt = -0.15*x + 0.28*y;
      yt = 0.26*x + 0.24*y + 0.44;
    } else {
      xt = 0.85*x + 0.04*y;
      yt = -0.04*x + 0.85*y + 1.60;
    }
 
    x = xt;
    y = yt;
 
    int m = round(width/2 + 60*x);
    int n = height-round(60*y);
    
    set(m, n, #009867);
    
  }
  endRecord();
}

```

And the original code from Rosetta Code, for sake of comparison:

```auto
import processing.svg.*;

void setup() {
  size(325, 725);
  background(219, 226, 233);
}
 
float x = 0;
float y = 0;
 
void draw() {
 
  for (int i = 0; i < 155000000; i++) {
 
    float xt = 0;
    float yt = 0;
 
    float r = random(100);
 
    if (r <= 1) {
      xt = 0;
      yt = 0.16*y;
    } else if (r <= 8) {
      xt = 0.20*x - 0.26*y;
      yt = 0.23*x + 0.22*y + 1.60;
    } else if (r <= 15) {
      xt = -0.15*x + 0.28*y;
      yt = 0.26*x + 0.24*y + 0.44;
    } else {
      xt = 0.85*x + 0.04*y;
      yt = -0.04*x + 0.85*y + 1.60;
    }
 
    x = xt;
    y = yt;
 
    int m = round(width/2 + 60*x);
    int n = height-round(60*y);
    
    set(m, n, #009867);
    
  }
  noLoop();
}

```

Thank you for any help you can provide!

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [July 21, 2019, 7:45am UTC](https://discourse.processing.org/t/exporting-barnsley-fern-to-svg/12857/2 "2019-07-21T07:45:30Z")

</div>

sorry,  
it’s just that i think that

> **[set() / Reference](https://processing.org/reference/PImage_set_.html)**
>
> Changes the color of any pixel or writes an image directly into the display window. The x and y parameters specify the pixel to change and the color parameter specifies…

works on pixels ( of a image or PGraphics )

and that is NOT a vector thing at all, so not works for SVG

> **[SVG Export / Libraries](https://processing.org/reference/libraries/svg/index.html)**
>
> Create SVG files.

> Many methods, particularly pixel-based methods, don’t make sense for SVG renderers. This includes: loadPixels, updatePixels, get, set, mask, filter, copy, blend, save, and image

* * *

BUT

```auto
// https://rosettacode.org/wiki/Barnsley_fern
// https://www.gnu.org/licenses/old-licenses/fdl-1.2.html

import processing.svg.*;

int imax = 15500; //155000000
String outfile = "data/Fern.svg";
float x = 0, y = 0;

void setup() {
  size(325, 725);
  background(219, 226, 233);
  noLoop();
  beginRecord(SVG, outfile);
}

void draw() { 
  for (int i = 0; i < imax; i++) { 
    float xt = 0;
    float yt = 0; 
    float r = random(100); 
    if (r <= 1) {
      xt = 0;
      yt = 0.16*y;
    } else if (r <= 8) {
      xt = 0.20*x - 0.26*y;
      yt = 0.23*x + 0.22*y + 1.60;
    } else if (r <= 15) {
      xt = -0.15*x + 0.28*y;
      yt = 0.26*x + 0.24*y + 0.44;
    } else {
      xt = 0.85*x + 0.04*y;
      yt = -0.04*x + 0.85*y + 1.60;
    }
    x = xt;
    y = yt;
    int m = round(width/2 + 60*x);
    int n = height-round(60*y);
    // set(m, n, #009867);
    stroke(#009867);
    point(m, n);
  }
  endRecord();
  println("saved:"+outfile);
}

```

did give some result.

---

<div class="post-metadata">

### Author: ![dr\_vblschrf](https://avatars.discourse-cdn.com/v4/letter/d/8dc957/32.png) [@dr\_vblschrf](https://discourse.processing.org/u/dr_vblschrf)
#### Post date: [July 21, 2019, 8:18am UTC](https://discourse.processing.org/t/exporting-barnsley-fern-to-svg/12857/3 "2019-07-21T08:18:30Z")

</div>

Thank you for your work. The SVG did get a result…but it was completely unrecognizable. The pixels didn’t render properly, as you said. PDF didn’t work either.

Since the vector didn’t work, how would I export this in a high resolution image, say, a BMP or PNG? Does Processing have the ability to do that? I know I’d need to change the size parameters but other than that I don’t know what I’d need to do.

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [July 21, 2019, 8:25am UTC](https://discourse.processing.org/t/exporting-barnsley-fern-to-svg/12857/4 "2019-07-21T08:25:28Z")

</div>

yes, we know that SVG import and export is limited in several ways in that version.

for a export to a image file pls. experiment with  
[https://processing.org/reference/save\_.html](https://processing.org/reference/save_.html)

 ![Fern](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/1dc9df609e306606800ef2964404b99efdd8ff8d.png)

oh yes, it is good style to have in the code the link, where you get it from!

---

<div class="post-metadata">

### Author: ![monkstone](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/monkstone/32/64_2.png) [@monkstone](https://discourse.processing.org/u/monkstone)
#### Post date: [July 21, 2019, 8:45am UTC](https://discourse.processing.org/t/exporting-barnsley-fern-to-svg/12857/5 "2019-07-21T08:45:01Z")

</div>

If you are prepared to investigate contextfreeart as an alternative to processing, it is great at rendering fractals such as the Barnsley fern in high resolution. It is also possible to render as svg. PS there is a Barnsley fern in the gallery at [https://contextfreeart.org](https://contextfreeart.org)

---

<div class="post-metadata">

### Author: ![dr\_vblschrf](https://avatars.discourse-cdn.com/v4/letter/d/8dc957/32.png) [@dr\_vblschrf](https://discourse.processing.org/u/dr_vblschrf)
#### Post date: [July 21, 2019, 4:49pm UTC](https://discourse.processing.org/t/exporting-barnsley-fern-to-svg/12857/6 "2019-07-21T16:49:24Z")

</div>

Thanks to both of you! Exporting as a BMP, then tracing in Inkscape to convert to a vector solved the problem.
