Hi Matthias,
Is it possible to efficiently get all the data of a db.Image? Something like get_data without polling get_pixel?
I would like to create a bitmap as shown in this posts but in a numpy array such that I can feed it directly into the 'meep' electromagnetic simulator.
I also tried it with a 'NumpyTileOutputReceiver' which works but is very slow. If I can get the image data as a raw array then I can easily load it quickly into a numpy array.
Of course I can save the image to disk and load it again.
Just, I did not find out how I can save a db.Image from a script without the GUI.
I admit that an API to access the image data efficiently is missing - actually the image object was intended as a display item, so only input was required. I have created a ticket for this request: https://github.com/KLayout/klayout/issues/473
Right now, one workaround for faster access is "str(image)" aka "to_s". For monochrome images this is the scheme:
import re
data = [ i*0.1 for i in range(0, 10000) ]
img = pya.Image()
img.set_data(100, 100, data)
img_str = str(img)
# NOTE: the data for monochrome images is contained in the string
# like "...;data=[p1;p2;p3;...;]..." where p1 are the pixel values
# from bottom-left to top-right (x first, then y). p* are float
# values.
data_str = re.search(r"data=\[(.*?);?\]", str(img)).group(1)
data = [ float(d) for d in data_str.split(";") ]
print(data)
Comments
There is a "rasterize" method in
dbPolygonTools.h
, which may be a starting point. But I doubt whether it's well tested.Please observe the GPL requirements.
Matthias
Hi Matthias,
Is it possible to efficiently get all the data of a db.Image? Something like
get_data
without pollingget_pixel
?I would like to create a bitmap as shown in this posts but in a numpy array such that I can feed it directly into the 'meep' electromagnetic simulator.
I also tried it with a 'NumpyTileOutputReceiver' which works but is very slow. If I can get the image data as a raw array then I can easily load it quickly into a numpy array.
Of course I can save the image to disk and load it again.
Just, I did not find out how I can save a
db.Image
from a script without the GUI.Thanks a lot!
Stupid, had debugging mode enabled when I tried thing from the GUI.
Without it is much faster
Hi Thomas,
yes, debugging makes execution slow.
I admit that an API to access the image data efficiently is missing - actually the image object was intended as a display item, so only input was required. I have created a ticket for this request: https://github.com/KLayout/klayout/issues/473
Right now, one workaround for faster access is "str(image)" aka "to_s". For monochrome images this is the scheme:
Best regards,
Matthias
Ok! Thanks!