Basically, I have a set of colored lines that I’m rendering into a Graphics[] object that I then render to a 200x200 Image that I later extract pixel data from with ImageData. In my tests, I’m creating around 100 images each with 80 lines - which takes around 6.3 seconds (measured w/ AbsoluteTiming) on my 2018 macbook pro w/ Mathematica 11.3. Given that it works out to drawing 8000 lines and creating only 100 images that are 200x200, I was expecting this to run much faster.
Is there a better way to create 2D Images from Graphics objects built out of graphics primitives that will run faster?
You should provide some sample data otherwise its hard to say without trying things out. One thing that I notice is that imageResize is slow and not needed because you can specify the resolution in Image:
Your code is going to be faster as you wrote it because it isn’t compositing the image from graphics primitives - it’s going from a 2D array of numbers and creating a raster image from it. My code above is creating the Image from a set of Graphics primitives, not a 2D array of numbers. Adding ImageSize->{200,200} to my code doesn’t change the performance in a useful way. In fact, changing render to:
All the time seems to be in the Image[]. The rest is fast. I would contact tech support to see if the developers in that group can explain why. Sorry I could not be more helpful.
Conversion of Graphics to Image usually requires a rasterization and unfortunately it is the case here. Image constructor internally calls Rasterize when the input argument is a Graphics object.
The only exception is Graphics[Raster[..]], where the image data is extracted directly from Raster.
You can use ImageSize option of Graphics in your render function to avoid ImageResize call: