Combining two pieces of code



  • edited November 2017

    I think it would be easier to actually combine sketch 1 and sketch 2 into one sketch file.

    Well, I've made those 4 util functions inside the original sketch 1 originally.
    But b/c you were asking for a separate sketch 2, I've posted them separated from sketch 1.
    This is how I actually have them unified w/ sketch 1 here: :(|)

     * Python Command (v2.2.3)
     * GoToLoop (2017/Jul/05)
     * combining-two-pieces-of-code/p2#Item_51
     * executing-python-3-code-from-within-processing/p1#Item_38
     * deadpixel/command/
    import deadpixel.command.Command;
    static final String BASH = platform == WINDOWS? "cmd /C " : "bash -c ";
    static final String CD = "cd ", PY_APP = "python ";
    static final String AMP = " && ", SPC = " ";
    static final String PY_DIR = "scripts/";
    //static final String PY_FILE = PY_DIR + "";
    static final String PY_FILE = PY_DIR + "";
    static final String PICS_DIR = "images/";
    static final String PICS_EXTS = "extensions=" +
    String[][] dirs;
    void setup() {
      final String dp = dataPath(""), py = dataPath(PY_FILE);
      final String prompt = BASH + CD + dp + AMP + PY_APP + py;
      final String pd = dataPath(PICS_DIR);
      final String pics = join(getPathsFromFolder(pd), SPC);
      final Command cmd = new Command(prompt + SPC + pics);
      println(cmd.command, ENTER);
      println("Success:",, ENTER);
      dirs = getFoldersOfImagePaths();
      println("Segment folders found:", dirs.length);
    static final String joinStrArr2d(final String[][] arr2d) {
      final StringBuilder sb = new StringBuilder();
      int outer = 0;
      for (final String[] arr1d : arr2d) {
        int inner = 0;
        for (final String item : arr1d)  sb
          .append("] ").append(item).append(ENTER);
      return sb.toString();
    String[][] getFoldersOfImagePaths() {
      final String[] dirs = getSegFolders();
      final String[][] all = new String[dirs.length][];
      for (int i = 0; i < all.length; all[i] = getPathsFromFolder(dirs[i++]));
      return all;
    String[] getSegFolders() {
      final File dataFolder = dataFile("");
      if (!dataFolder.isDirectory())  return new String[0];
      final String[] folders = listPaths(dataFolder.getPath(), "directories");
      final StringList sl = new StringList(folders);
      final java.util.Iterator<String> it = sl.iterator();
      while (it.hasNext())  if (!"_segments"))  it.remove();
      return sl.array();
    String[] getPathsFromFolder(final String folder) {
      return listPaths(folder, PICS_EXTS);
  • edited July 2017

    Oh, I misunderstood you, I get it now. I was trying to do it from within sketch 2 not sketch 1 and I thought you were doing the same on your end, my bad.
    So now if I move sketch 2 inside the sketch 1 folder, it should pick out the segments from the same data folder if I ask it to using the Pimage, correct?
    Is what I have now.
    I was thinking this
    might help to resolve that error, im still looking into it though.

  • edited July 2017

    I tried installing the multisketch, but I think its missing its own library or its not available to download for some reason. The "Two sketches embedded in a single window" looked good. I've found the archive here:
    Version 3.4 mentions "The multisketch package (embedded sketches and slideshow) has been temporarily removed as it relied on Java-specific implementation of Processing 2 (the AWT)."
    But every version before 3.4 includes it. How would I install it manually or do I need processing 2 instead of 3?

  • @kfrajer gave me an answer on the assigning segments part, so sketch 2 is pretty much complete.

  • I've moved this project to a macbook to see if it would run on a mac, and obviously I knew I'd run into a few errors.
    The error im getting using sketch 1 is "COMMAND ERROR: Cannot run program "opencd": error=2, No such file or directory"
    I installed the numpy, scipy and matplotlib on my mac in order for it to run. Cheers

  • edited October 2017
    • "Python Command" sketch is mostly concatenation operations.
    • You say you've got an "opencd" error. But I believe it should be "open cd" instead.
    • Command cd means Change Directory.
    • Under Windows, constant BASH is "cmd /C ".
    • Thus BASH + CD concatenation operation would result "cmd /C cd ".
    • However, MacOSX is "open" and LINUX is "xdg-open".
    • Notice the Windows' BASH ends w/ a trailing space, while the others don't! :-?
    • So I guess the fix is merely adding a trailing space to both MacOSX & Linux's BASH constant. *-:)
    • Made those tiny fixes as now version v2.0.3. :D


    static final String BASH =
      platform == WINDOWS? "cmd /C " :
      platform == MACOSX? "open" : "xdg-open";


    static final String BASH =
      platform == WINDOWS? "cmd /C " :
      platform == MACOSX? "open " : "xdg-open ";
  • Ah-ha! Ok... So im getting a different error now instead:
    "the files /Users/J/cd, /Users/J/&&, and /Users/J/Python do not exist"
    Is this just a directory mishap on my part?

  • edited October 2017
    • Recall that this sketch was originally customized to use specific subfolder inside subfolder "data/".
    • For example, The Python's script should be here: static final String PY_DIR = "scripts/";
    • And the source images: static final String PICS_DIR = "images/";
    • If you can't mime the same path names as it was in Windows, you're gonna need to modify the sketch's constants in order to match your current environment.
  • edited October 2017

    Yep its all within the same folder as before in "sketch 1" script is in scripts, it because I have the folder on the desktop, instead of in /Users/J? In which case, how would I edit v2.0.3 to have it run from the desktop? (sketch 2 is on the desktop as well)

  • edited October 2017

    Problem is, I dunno anything about how MacOS organizes a user's folders. Can't help ya w/ that! :(

  • Well thanks so much for the help again to get me this far! Ill ask over @ stackexchange and see what they say, I dont quite understand how I should name the paths.

  • edited October 2017

    After searching to find where python is stored I put: usr/bin/python.
    It looks like it recognized it, but im still stuck at cd and &&.... any ideas? :{
    -I tried without cd and &&, and it said success true, but no segments.
    Why would it say that /Users/J/cd doesnt exist?
    What it looks like now:

  • edited October 2017

    After searching to find where python is stored I put: "usr/bin/python".

    AFAIK, if Python is properly installed in the OS, simply invoking "python" is enough to run it. :>
    No need to specify its full path b/c it's already registered in the OS' $PATH system variable. :-B

  • True. Pretty sure I downloaded and set it up right though.. either way, usr/bin/python seemed to make the python error leave.
    Im just lost on this "&&", "cd", I think the program is treating these as directories instead of commands.

  • edited October 2017
    • Possibly your Python script "" relies on CWD (Current Working Directory).
    • It uses the CWD in order to know where to output all the segmented images.
    • That's why when it runs under Windows, it issues the command CD before calling the python interpreter:
    • Before issuing that CD command, 1st it relies on the command CMD together w/ the switch "/C": "cmd /C ":
    • However, the corresponding MacOS's command open doesn't have any such switch:
    • Fortunately, both MacOS & Linux have the command bash, which happens to have the switch "-c":
    • So I believe we can replace Window's "cmd /C " w/ "bash -c " for both Mac & Linux! \m/
    • Already re-edited my "Python Command" post w/ that fix as of version 2.1! O:-)


    static final String BASH =
      platform == WINDOWS? "cmd /C " :
      platform == MACOSX? "open " : "xdg-open ";


    static final String BASH = platform == WINDOWS? "cmd /C " : "bash -c ";

    Given I don't have a Mac, dunno whether the new fix is gonna work... :|

  • edited October 2017

    Wow hard to follow, but I think I get it. Awesome work.
    I get a success true now, even with it just being "python"
    but still no segmentations...would it be because my directory is incorrect within
    Right now its /Users/J/Desktop/Sketch2/Data (line 78)
    All the segments created from sketch 1 go to sketch 2, just like before.
    Maybe its the "r" thats before that line. I forget what that is supposed to represent.
    Apparently "f open" is the equivalent for mac:
    But its not working like this either..

  • edited October 2017

    I'm just gonna post the "" version I've been using all along: L-)

  • edited October 2017

    Thanks. So I just talked to the guy who helped me out with the, and he said it would need to be r'/Users/J/Desktop/sketch2/data'
    So the slashes are the other way for mac.
    But im still getting "segment folders found 0"
    Damn! :(

  • Why don't you publish the whole program at GitHub? Much better to visualize: *-:)

  • edited October 2017

    Yeah I will for sure, right after I figure how to run this sucker on a mac.
    So between the two: (windows) (mac)
    Is it possible that on the mac its not even recognizing the python script at all?
    'segement detected' is part of the python script that should be printed in.

  • edited October 2017

    According to those 2 pictures, neither of them are using the latest Python Command (v2.1.2) I've reposted here! [-(

    And I recommend to use my "" version from the link I've already posted here, which I know that it at least works under Windows .

  • edited October 2017

    Oh whoops, I missed that you had edited it, my bad. Yep, im using the same, and with windows it works, but on mac it does not. Has to be a directory issue, because I dont think or python is even being called at all.
    I just read that macs come with python 2.7 already installed by default.
    Update: It may be an error with pythons "pillow", im working on resolving the errors im getting:
    after trying to run the from the mac terminal.

  • edited October 2017

    Ok after alot of trial and error, new update. By installing anaconda python 2.7 from
    It allows me to run the from the mac terminal like so:
    Js-MacBook:~ J$ python /Users/J/Desktop/sketch1/data/scripts/
    And outputs the segments right into the sketch 2 folder.
    So im not sure why I still cant run sketch1 with the v2.1.2 command, its still not working....but I imagine its some small error in directory, just cant figure it out.
    Maybe this might help, would it be as simple as just executing the terminal or a file to the terminal from within processing?

  • edited October 2017

    By installing Anaconda Python 2.7...

    Under Windows you were using vanilla 64-bit Python 3.6 instead.


    According to the path above, I wonder whether you've directly picked system folder "Desktop/" as your sketchbook path? :-/

    In my Windows environment here, I've created a subfolder called "P3/" under "Documents/Sources/", and chosen that as my PDE's sketchbook location.

    It allows me to run the "" from the mac terminal...

    Have you also tried my "" version from unmodified?

    BtW, I've made some refactoring on it today and committed those changes there on its GitHub repo. L-)

    Particularly, I find filename = os.path.join(os.getcwd(), name) possibly problematic. :-S

    Well, It's not causing any problems under Windows.
    But for the sake of making sure it won't under Mac or Linux, I've changed that code line to this:
    filename = os.path.isfile(name) and name or os.path.join(os.getcwd(), name)

    So I suggest you to re-download my "" and try that out again. O:-)

    ... would it be as simple as just executing the terminal or a file to the terminal from within processing?

    Like I've mentioned already, at least for my "" version, it relies on CWD in order to know where to save the segmented images. :-\"

    That's why "Python Command" issues command CD to temporarily change CWD to the sketch's "data/" subfolder before calling "python". :ar!

  • edited October 2017

    Here's my "Python Command"'s output under Windows, having "test.jpg" & "water_coins.jpg" inside subfolder "data/images/": :(|)

    cmd /C cd C:\Users\User\Documents\Sources\P3\PythonCommand\data && python 
    Success: true 
    [0] "Processing: C:\Users\User\Documents\Sources\P3\PythonCommand\data\images\test.jpg"
    [1] "Segments detected: 99"
    [2] "Processing: C:\Users\User\Documents\Sources\P3\PythonCommand\data\images\water_coins.jpg"
    [3] "Segments detected: 1"
    [0][0] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-0.png
    [0][1] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-1.png
    [0][2] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-10.png
    [0][3] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-11.png
    [0][4] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-12.png
    [0][5] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-13.png
    [0][6] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-14.png
    [0][7] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-15.png
    [0][8] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-16.png
    [0][9] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-17.png
    [0][10] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-18.png
    [0][11] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-19.png
    [0][12] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-2.png
    [0][13] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-20.png
    [0][14] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-21.png
    [0][15] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-22.png
    [0][16] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-23.png
    [0][17] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-24.png
    [0][18] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-25.png
    [0][19] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-26.png
    [0][20] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-27.png
    [0][21] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-28.png
    [0][22] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-29.png
    [0][23] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-3.png
    [0][24] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-30.png
    [0][25] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-31.png
    [0][26] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-32.png
    [0][27] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-33.png
    [0][28] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-34.png
    [0][29] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-35.png
    [0][30] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-36.png
    [0][31] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-37.png
    [0][32] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-38.png
    [0][33] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-39.png
    [0][34] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-4.png
    [0][35] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-40.png
    [0][36] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-41.png
    [0][37] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-42.png
    [0][38] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-43.png
    [0][39] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-44.png
    [0][40] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-45.png
    [0][41] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-46.png
    [0][42] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-47.png
    [0][43] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-48.png
    [0][44] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-49.png
    [0][45] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-5.png
    [0][46] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-50.png
    [0][47] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-51.png
    [0][48] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-52.png
    [0][49] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-53.png
    [0][50] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-54.png
    [0][51] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-55.png
    [0][52] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-56.png
    [0][53] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-57.png
    [0][54] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-58.png
    [0][55] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-59.png
    [0][56] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-6.png
    [0][57] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-60.png
    [0][58] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-61.png
    [0][59] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-62.png
    [0][60] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-63.png
    [0][61] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-64.png
    [0][62] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-65.png
    [0][63] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-66.png
    [0][64] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-67.png
    [0][65] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-68.png
    [0][66] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-69.png
    [0][67] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-7.png
    [0][68] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-70.png
    [0][69] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-71.png
    [0][70] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-72.png
    [0][71] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-73.png
    [0][72] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-74.png
    [0][73] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-75.png
    [0][74] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-76.png
    [0][75] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-77.png
    [0][76] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-78.png
    [0][77] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-79.png
    [0][78] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-8.png
    [0][79] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-80.png
    [0][80] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-81.png
    [0][81] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-82.png
    [0][82] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-83.png
    [0][83] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-84.png
    [0][84] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-85.png
    [0][85] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-86.png
    [0][86] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-87.png
    [0][87] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-88.png
    [0][88] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-89.png
    [0][89] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-9.png
    [0][90] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-90.png
    [0][91] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-91.png
    [0][92] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-92.png
    [0][93] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-93.png
    [0][94] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-94.png
    [0][95] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-95.png
    [0][96] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-96.png
    [0][97] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-97.png
    [0][98] C:\Users\User\Documents\Sources\P3\PythonCommand\data\test_segments\test-98.png
    [1][0] C:\Users\User\Documents\Sources\P3\PythonCommand\data\water_coins_segments\water_coins-0.png
    Segment folders found: 2
  • Yeah I needed to download anaconda python 2.7 in order for all the numpy/scipy libraries to be updated and in the right place on the mac in order for python to run correctly.
    Wait, should I pick /desktop as my sketchbook path? It was on /documents like how you have yours. And yeah, totally works on my windows computer like how you have shown.
    Yep, tried out the latest unmodified from your github, still nothing.
    Im not sure if this is a python problem or a processing problem at this point...very confusing indeed

  • edited October 2017

    Yeah I needed to download Anaconda Python 2.7...

    Still why have you picked 2.7 over 3.6? 8-X
    Also, I thought that Anaconda's Python isn't named "python". Have you checked that out? :-SS

    Wait, should I pick "Desktop/" as my sketchbook path?

    IMO nope! But it seems like it's your actual current sketchbook path according to the output you've posted here. ~:>

  • Like just anaconda...when I ask "which python" in the mac terminal its "/Users/J/anaconda2/bin/python"
    I picked 2.7 over 3.6 because I figured it would work with the python 2.7 thats already installed by default on the mac?
    Ah, so I literally just have both sketch1+2 on the desktop, so my sketchbook path should be documents, or should be desktop? Im a little confused. It was documents originally

  • edited October 2017

    Ah, so I literally just have both sketch1+2 on the desktop, ...

    Sketches should ideally go into sketchbook's folder path. 8-|

    I figured it would work with the python 2.7 that's already installed...

    Anaconda doesn't use any existing installed Python. #-o
    AFAIK, Anaconda hacks the system's path to point to its own Python instead. :ar!

    Regardless, that's a good thing that when issuing "python" in the terminal, it runs Anaconda's now. >-)

  • Ok...I got it!
    Basically on a mac if you create an executable file to the terminal that includes the direct path that opens python and the image you are using, it works.
    using launch("/Users/J/Desktop/image");
    which contains: /Users/J/Desktop/sketch1/data/scripts/ /Users/J/Desktop/sketch1/data/images/image.jpg
    (just means you need to always rename your image that you want segmented "image"

    Since this renders sketch 1 obsolete (at least on a mac) I can move everything into one folder, and what I'd like to do is add the image executable into sketch 2 so that I just have the one sketch file which we shall call "sketch" (if you follow).

    So how can I add launch("/Users/J/sketch/image");
    to the current version of sketch 2:
    So that it launches the exectuable image file before the segments are assigned to hand.jpg?

    (ill clean all this up after and make it one tidy file)

  • edited October 2017

    All I need to know is how can I add launch("/Users/J/sketch/image");
    to so that it comes prior to everything else opening.
    Ill compile the rest.

    • 1st your PasteBin sketch is painfully mal-indented, very hard to study.
    • Before pasting any sketch, always use CTRL+T inside the PDE for auto-indentation.
    • 2nd, the way the sketch was coded is that it is the 1 which auto-finds the paths for all source images.
    • Then those paths are concatenated in 1 String and are sent to the remote ".py" script.
    • After the ".py" script finishes, the sketch auto-searches inside the output subfolder "data/images/" for those generated segment images.
    • However, for that to work correctly, the ".py" script needs to be programmed to output the seg images inside the folder the Processing sketch expects it, inside its "data/images/" subfolder.
  • edited October 2017

    Sorry about that. Ok to make this alot easier, here it is:
    It works on my mac if I double click the executable "image" it does its thing and the segments are put in data.
    Also included in input1 is a palette image file which can colour the segments at random !
    So im thinking use launch("/Users/J/Desktop/sketch/image"); for MAC
    and launch("C:\Users\J\Desktop\sketch\image); for WINDOWS
    So somehow add both these into the current sketch file.

    for windows would be a text file as well that would include:
    python C:\Users\J\Desktop\sketch\scripts\ C:\Users\J\Desktop\sketch\input1\image.jpg

    Would that work?

    Im looking at the file here on my windows computer now, you can just open the image file in notepad to check it out.

  • edited October 2017
    • The ".pde" sketch there is exactly the same mal-indented you've posted in PasteBin.
    • The reason why I've recommended "" to you is b/c it already handles the whole Process of calling the external file, saving its output in an array, print in red any error messages, and then waitFor() it to end.
    • If you rather prefer launch() or exec(), you're gonna have to write the code to deal w/ the Process by yourself!
    • If you choose so, I recommend you to study the "" anyways, so you can have an idea how to handle the Process object returned by launch() and exec():
  • edited October 2017

    It works on my end though even with the mal-indents?
    Right, so im wondering how I can put launch() into the sketch?
    It just needs to come beforehand. Ill look into it.
    It might be easier this way, then itd just be one entire sketch file which would look really nice.
    Basically you'd have two versions of the python segmenting code, one for windows and one for mac, and two executable files, one for windows and one for mac.
    I figured launch() could outline
    but im not sure how the pde could pick which launch or exec to use.
    I've updated the dropbox link above to include the windows version of the python code, I just tested it in the command prompt using
    C:\Users\J\Desktop\sketch\scripts\ C:\Users\J\Desktop\sketch\input1\image.jpg
    and it works.

    I mean I have both working on windows and mac, but for anyone else interested I think having it as one zip file for both mac and win would be neat, otherwise im all set and appreciate all the help.

  • It works on my end though even with the mal-indents?

    Java language doesn't care about indentations, humans do!

    I've updated the dropbox link above...

    Not every1 likes to download files in order to see some1 else's code.
    Post your code folder on GitHub instead.
    Folks can study the code and offer changes to it there.

  • edited December 2017
     Python Command (v2.2.3.1)
     GoToLoop (2017/Jul/06)




    # from deadpixel.command import Command
    from Command import Command
    BASH = PApplet.platform == WINDOWS and 'cmd /C ' or 'bash -c '
    CD, PY_APP = 'cd ', 'python '
    AMP, SPC = ' && ', ' '
    PY_DIR = 'scripts/'
    # PY_FILE = PY_DIR + ''
    PY_FILE = PY_DIR + ''
    PICS_DIR = 'images/'
    PICS_EXTS = 'extensions=,png,jpg,jpeg,gif,tif,tiff,tga,bmp,wbmp'
    def setup():
        dp, py = this.dataPath(''), this.dataPath(PY_FILE)
        prompt = BASH + CD + dp + AMP + PY_APP + py
        pd = this.dataPath(PICS_DIR)
        pics = SPC.join(getPathsFromFolder(pd))
        cmd = Command(prompt + SPC + pics)
        print cmd.command, ENTER
        print 'Success: ' + `` + ENTER
        print joinList1d(cmd.getOutput())
        global dirs
        dirs = getFoldersOfImagePaths()
        print joinList2d(dirs)
        print 'Segment folders found:', len(dirs)
    def joinList2d(l2d, FORMAT='[%d][%d] %s\n', s=''):
        for outer, l1d in enumerate(l2d):
            s += ENTER
            for inner, item in enumerate(l1d):
                s += FORMAT % (outer, inner, item)
        return s
    def joinList1d(l1d, FORMAT='[%d] %s\n', s=''):
        for kv in enumerate(l1d): s += FORMAT % kv
        return s
    def getFoldersOfImagePaths():
        return tuple(getPathsFromFolder(dir) for dir in getSegFolders())
    def getSegFolders(NAME='_segments', OPT='directories', ROOT='', NONE=()):
        dataFolder = this.dataFile(ROOT)
        if not dataFolder.isDirectory(): return NONE
        dirs = this.listPaths(dataFolder.path, OPT)
        return tuple(dir for dir in dirs if dir.endswith(NAME))
    def getPathsFromFolder(folder, EXTS=PICS_EXTS):
        return this.listPaths(folder, EXTS);
  • Hey! Just noticed this...what did you change?
    I still have both working on windows and mac, my mac is using the terminal version.
    Ill try and run this today.
    I came up with another idea for this project....adding this:
    As the pattern creator..instead of loading in patterns, theres already a processing version of it too!
    ...we will see..

  • edited October 2017

    That's merely the corresponding Python Mode version. :-\"
    Posted it for completeness' sake, heheh. :P

  • Ah, very nice!

Sign In or Register to comment.