I've come up with some improvements (IMO).
The first script below is similar to the last, but:
- gets the original filenames and appends "marked" for the exported
filenames.
- It also closes the files after export which gives some visual feedback
that things are working.
- On my computer it runs through ~50 files in about 5-10 seconds.
- I also coded my way around the problem of not being able to change the
mask color for the grain marking. Then I found the "Default Mask Color"
setting. Whoops. So you could use the routine in the below script, or use
the more straightforward code I used in the first script:
gwy.gwy_process_func_run("grain_mark", c, gwy.RUN_INTERACTIVE)
- the leveling does not work for some reason. but because I can mark the
grains with full white or full black the thresholding algorithm of the
other program I'm counting dots with works great. this should be easy to
fix.
So the big remaining issue is opening all the files and performing these
operations. I was able to come up with a second script that traverses a
directory and opens all the files in it. Unfortunately, it's still manual
because I am working with BMP files, which can't import automatically. Is
there a way to run the import window as NONINTERACTIVE? Or is there an
image format I could convert to that would open automatically in Gwyddion?
This is really the final piece I need to make this all work.
Here are the scripts:
SCRIPT 1 - Mark and export
import gwyutils
#basename = "c:\processed\sample%(sample_num)02d_marked_%(image_num)02d.png"
basename = "c:/processed/%s-marked.png"
i = 0
num = 1
# get list of available containers
cons = gwy.gwy_app_data_browser_get_containers()
# iterate thru containers and datafields
for c in cons:
# get directory of datafields where key is key in container
dfields = gwyutils.get_data_fields_dir(c)
for key in dfields.keys():
# get processed datafield object
datafield = dfields[key]
# create new grainfield by using active datafield
grainfield = datafield.new_alike(True)
# retrieve datafield number in container from key (for example
'/0/data')
datafield_id = key.split('/')[1]
# set palette of datafield
c.set_string_by_name("/%s/base/palette" % datafield_id, "Gray")
#get filename without directory or file extension
filename = c.get_string_by_name('/filename')
filename = filename.rsplit('\\',1)[1]
filename = filename.rsplit('.',1)[0]
# call 'level' process module as non-interactive DOESN'T WORK
gwy.gwy_process_func_run("level", c, gwy.RUN_NONINTERACTIVE)
# mark grains by threshold/slope
#Generate grainfield
datafield.grains_mark_slope(grainfield,15,0)
#Merge two fields
datafield.max_of_fields(datafield, grainfield)
# request redraw before export
datafield.data_changed()
# export datafield to png, for the first time show export dialog
# A generic naming/export routine
# if i == 0:
# gwyutils.save_dfield_to_png(c, key, basename %
{"sample_num":num,"image_num":i+1}, gwy.RUN_INTERACTIVE)
# else :
# gwyutils.save_dfield_to_png(c, key, basename %
{"sample_num":num,"image_num":i+1}, gwy.RUN_NONINTERACTIVE)
# A little more specific naming routine. Preserves original filename
in exported name.
if i == 0:
gwyutils.save_dfield_to_png(c, key, basename % filename,
gwy.RUN_INTERACTIVE)
else :
gwyutils.save_dfield_to_png(c, key, basename % filename,
gwy.RUN_NONINTERACTIVE)
i += 1
# request redraw datawindows with datafields
datafield.data_changed()
#Close container/window after export. Gives some visual feedback when
dealing with large number of windows.
gwy_app_data_browser_remove(c)
SCRIPT 2 - Traverse directory
import os, gwyutils, gwy
directory = "C:\YOUR_DIRECTORY"
print directory
for dirname, dirnames, filenames in os.walk(directory):
# print path to all subdirectories first.
for subdirname in dirnames:
print os.path.join(dirname, subdirname)
# print path to all filenames.
for filename in filenames:
path = os.path.join(dirname, filename)
print path
print path.split(".")[-1].lower()
if path.split(".")[-1].lower() in ['bmp']:
print "BMP!"
gwy_app_file_open(path)
On Wed, Oct 30, 2013 at 2:39 PM, Ramsey Hazbun <
[email protected]> wrote:
> Since I kind of hit a wall with the standalone python script. Here is the
> pygwy code I've written, based on the code posted on the pygwy tutorial.
> I'm pretty close to a reasonable solution, but it won't be ideal, and it's
> still a little too manual to be practical.
>
> Some problems I'm having:
> - the mark grains dialog doesn't seem to run in NONINTERACTIVE mode. In
> interactive mode, while it remembers the slope value I'd like to use, it
> does not remember the color for the mask. This is critical for my work (I
> am trying to count the grains). Perhaps there's a line or two I could use
> to change the color of the grains after they are marked?
> - In general it seems really difficult to pass parameters to these
> functions. I'm guessing there are some more direct functions I should be
> using and so far I've been using GUI/front-end version. I tried to get
> gwy_data_field_grains_mark_slope () to work, but apparently I don't have it
> quite right. Either I'm a bit clueless (likely), or the function is for C
> and hasn't made it into the python implementation.
> - I have no idea how to get grain counts or statistics into a text file of
> some sort. I've been relying on another program I have that is great for
> export dot/grain counts, but lacks the image processing that gwyddion has.
> - I have to load all the images I want to process in the Gwyddion GUI. I
> am planning on doing this about 50 files at a time. Obviously a standalone
> script would be ideal but in lieu of that, this console script is the best
> option I have at the moment.
> - export doesn't work if directory doesn't exist. this is trivial, and
> probably could be fixed by some python code to look for the directory and
> create it if it's missing.
>
> START CODE
>
> import gwyutils
>
> # base name
> basename = "c:\processed\sample%(sample_num)02d_marked_%(image_num)02d.png"
>
> i = 0
> num = 1
>
> # get list of available containers
> cons = gwy.gwy_app_data_browser_get_containers()
>
> # iterate thru containers and datafields
> for c in cons:
>
> # get directory of datafields where key is key in container
> dfields = gwyutils.get_data_fields_dir(c)
>
> for key in dfields.keys():
>
> # get processed datafield object
> datafield = dfields[key]
>
> # retrieve datafield number in container from key (for example
> '/0/data')
> datafield_id = key.split('/')[1]
>
> # set palette of datafield
> c.set_string_by_name("/%s/base/palette" % datafield_id, "Gray")
>
> # call 'level' process module as non-interactive
> gwy.gwy_process_func_run("level", c, gwy.RUN_NONINTERACTIVE)
>
> # mark grains by threshold/slope
> # if i == 0:
> # gwy.gwy_process_func_run("grain_mark", c, gwy.RUN_INTERACTIVE)
> # else :
> # gwy.gwy_process_func_run("grain_mark", c, gwy.RUN_NONINTERACTIVE)
> gwy.gwy_process_func_run("grain_mark", c, gwy.RUN_INTERACTIVE)
>
> # request redraw before export
> datafield.data_changed()
>
> # export datafield to png, for the first time show export dialog
>
> if i == 0:
> gwyutils.save_dfield_to_png(c, key, basename %
> {"sample_num":num,"image_num":i+1}, gwy.RUN_INTERACTIVE)
> else :
> gwyutils.save_dfield_to_png(c, key, basename %
> {"sample_num":num,"image_num":i+1}, gwy.RUN_NONINTERACTIVE)
>
> i += 1
>
> # request redraw datawindows with datafields
> datafield.data_changed()
>
>
> On Wed, Oct 30, 2013 at 1:44 PM, Ramsey Hazbun <
> [email protected]> wrote:
>
>> A follow-up. I realize now that my compiled gwyddion is not working (the
>> apt package did). I get the following error on start-up:
>> Application and library version do not match: 2.33 vs. 2.31
>> I imagine this is because I had the ubuntu package installed previously.
>>
>>
>>
>> On Wed, Oct 30, 2013 at 1:21 PM, Ramsey Hazbun <
>> [email protected]> wrote:
>>
>>> I'm attempting to write a standalone python script to process about a
>>> thousand images. I simply want to level and mark grains based on known
>>> slope. From what I've seen and tried in pygwy this seems doable.
>>>
>>> I'm having a series of problems that have led me to this point.
>>> - I started on windows. So no standalone gwy module for python
>>> available. That much I read on the mailing lists. Not too worried since
>>> I'm fine with using Linux. But less convenient.
>>> - I went over to Ubuntu, but the gwyddion apt package (based on gwyddion
>>> 2.31) doesn't seem to include the python gwy module.
>>> - I installed the gtk devel package and compiled gwyddion 2.33 from
>>> scratch using ./configure --with-python
>>>
>>> As you might of guessed I'm getting an error when I try to import the
>>> gwy module in python:
>>> ImportError: no module named gwy
>>>
>>> So what am I missing here? Let me know what additional info you might
>>> need. I'm on Ubuntu 13.10.
>>>
>>> Also, thanks for all the hard work. I'm already assuming Yeti is going
>>> to respond to me based on all the other messages I've read on these
>>> gwyddion/python issues.
>>>
>>
>>
>
------------------------------------------------------------------------------
Android is increasing in popularity, but the open development platform that
developers love is also attractive to malware creators. Download this white
paper to learn more about secure code signing practices that can help keep
Android apps secure.
http://pubads.g.doubleclick.net/gampad/clk?id=65839951&iu=/4140/ostg.clktrk
_______________________________________________
Gwyddion-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gwyddion-users