Skip to content

Commit b056e66

Browse files
committed
add readme
1 parent 251a378 commit b056e66

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

hybrid-programming/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ public class Py5Utilities {
4646
}
4747
```
4848

49-
The main idea is that any methods added to this Java `Py5Utilities` class will be made available to you through the `py5.utils` attribute. Use of this feature can greatly enhance the performance and capabilities of your py5 Python code. Refer to the [Hybrid Programming](http://py5coding.org/content/hybrid_programming.html) documentation to learn more.
49+
The main idea is that any methods added to this Java `Py5Utilities` class will be made available to you through the `py5.utils` attribute. Use of this feature can greatly enhance the performance and capabilities of your py5 Python code. Refer to the online [Hybrid Programming](http://py5coding.org/content/hybrid_programming.html) documentation to learn more.

processing-mode/README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Processing Mode
2+
3+
Processing Mode refers to py5’s ability to serve as a bridge from Java to Python, allowing Processing Sketches to call Python code using a new `callPython()` method. This is a solid feature that in time will add a significant amount of value to the Processing community.
4+
5+
[Processing Mode](http://py5coding.org/content/processing_mode.html) is documented on py5's website. The first two examples are code samples from the online documentation.
6+
7+
Here is a quick example of a Processing Sketch that uses py5 in Processing Mode:
8+
9+
```java
10+
package test;
11+
12+
import processing.core.PImage;
13+
import py5.core.SketchBase;
14+
15+
public class Example1Sketch extends SketchBase {
16+
17+
public void settings() {
18+
size(400, 400, P2D);
19+
}
20+
21+
public void setup() {
22+
String msg = "Hello from Java!";
23+
PImage img = createImage(200, 200, RGB);
24+
25+
// call Python function `alter_image(msg, img)` and get back a PImage
26+
PImage imgResponse = (PImage) callPython("test_transfer", msg, img);
27+
image(imgResponse, 100, 100);
28+
29+
// call numpy `random.randint()` function
30+
long randomNumber = (long) callPython("np.random.randint", 0, 100);
31+
py5Println("JAVA: Random number from numpy: " + randomNumber);
32+
}
33+
34+
public void draw() {
35+
rect(mouseX, mouseY, 20, 20);
36+
}
37+
38+
}
39+
```
40+
41+
The `callPython()` method in this Sketch is added to Processing with py5's `py5.core.SketchBase` class. It can pass parameters and return objects back to Java.
42+
43+
The Python code used to execute the above example Sketch is the following:
44+
45+
```python
46+
import numpy as np
47+
48+
import py5_tools
49+
import py5
50+
51+
52+
def alter_image(msg: str, img: py5.Py5Image):
53+
py5.println("PYTHON:", msg)
54+
py5.println("PYTHON:", img)
55+
56+
img.load_np_pixels()
57+
img.np_pixels[::2, ::2] = [255, 255, 0, 0]
58+
img.update_np_pixels()
59+
60+
return img
61+
62+
63+
# register processing mode keys so the Java `callPython()` method can find them
64+
py5_tools.register_processing_mode_key('test_transfer', alter_image)
65+
py5_tools.register_processing_mode_key('np', np)
66+
67+
# run the sketch in processing mode, specifying the Java class to instantiate
68+
py5.run_sketch(jclassname='test.Example1Sketch')
69+
```
70+
71+
The main idea is for py5 to provide Processing users with efficient access to the Python Ecosystem. Calls to `callPython()` in Java are linked to Python through registered "keys" that map to Python functions and modules. Refer to the online [Processing Mode](http://py5coding.org/content/processing_mode.html) documentation to learn more.

0 commit comments

Comments
 (0)