how to get the current file name / directory - almost solved - LUA Scripting - CHDK Forum supplierdeeply

how to get the current file name / directory - almost solved

  • 8 Replies
  • 12181 Views
how to get the current file name / directory - almost solved
« on: 21 / October / 2012, 07:16:21 »
Advertisements
I want to write a script that writes a text file in addition to each jpg file (to store metadata like successful/unsuccessful focus, focus distance etc. for each shot). Naturally, I want to use the same file name and simply add .txt instead of .jpg. How do I get the file name of the last shot?

Problem almost solved... I use get_exp_count to read the image number and then create the file name from this.

An open question is how to read the current image directory (100___10, 100___11 etc.).


Anyway, this script now saves focus state and focus distance to .txt files for each image:
 
--[[
@title Metadata
]]
shoot()
s=get_focus_state()
g=get_focus()
c=get_exp_count()
filename="A/DCIM/IMG_" .. string.format('%04d',c) .. ".txt"
fout=io.open(filename, "w")
fout:write(tostring(s) .. " " .. tostring(g))
io.close(fout)
« Last Edit: 21 / October / 2012, 08:16:06 by ralfhesse »

*

Offline lapser

  • *****
  • 1093
Re: how to get the current file name / directory - almost solved
« Reply #1 on: 21 / October / 2012, 20:11:22 »
c=get_exp_count()
filename="A/DCIM/IMG_" .. string.format('%04d',c) .. ".txt"
I didn't realize that get_exp_count() was in the file name. That should be useful.

EOS-M3_120f / SX50_100b / SX260_101a / G1X_100g / D20_100b
https://www.youtube.com/user/DrLapser/videos

*

Offline reyalp

  • ******
  • 14082
Re: how to get the current file name / directory - almost solved
« Reply #2 on: 21 / October / 2012, 22:25:26 »
Unfortunately, the directory isn't easily available from script. now in the trunk, see below.

You can look at http://trac.assembla.com/chdk/browser/trunk/core/raw.c#L120 to see how the raw code makes a raw filename matching the most recent jpeg name. The get_target_file_num() doesn't actually use quite the same code as get_exp_count() but it looks like it should be equivalent (might vary on some cameras though)

For cameras that use numbered directories, you can emulate get_target_dir_num() using get_parameter_data with the appropriate param. For cameras that use date naming, there isn't any way to get it that I know of. You could list the DCIM directory and try to pick the right one.

This should really be made into a generic function that returns a string to lua, since all the necessary code already exists. I'll look into doing this, it's been on my todo list for a while.

edit:
trunk changeset 2225 adds get_image_dir(). The following lua should give you the full path of the most recently shot jpeg
Code: [Select]
string.format('%s/IMG_%04d.JPG',get_image_dir(),get_exp_count())
This will be available on http://mighty-hoernsche.de/trunk/ shortly.

I haven't checked this on cameras with date folder naming, but if "raw in dir with jpeg" worked, so should this.
I don't know exactly when the counters change, so I'd be cautious using it in the middle of the shooting process.
« Last Edit: 21 / October / 2012, 23:06:04 by reyalp »
Don't forget what the H stands for.

Re: how to get the current file name / directory - almost solved
« Reply #3 on: 22 / October / 2012, 00:09:47 »
now in the trunk, see below.
Any thoughts on where to document this so that I can add it to the scripting xref ?
Ported :   A1200    SD940   G10    Powershot N    G16


*

Offline reyalp

  • ******
  • 14082
Re: how to get the current file name / directory - almost solved
« Reply #4 on: 22 / October / 2012, 00:21:05 »
now in the trunk, see below.
Any thoughts on where to document this so that I can add it to the scripting xref ?
It's Lua specific (doesn't make sense in ubasic since there's no strings or file io) so I guess it could go here http://chdk.wikia.com/wiki/Lua/Lua_Reference
Don't forget what the H stands for.

Re: how to get the current file name / directory - almost solved
« Reply #5 on: 22 / October / 2012, 00:25:16 »
It's Lua specific (doesn't make sense in ubasic since there's no strings or file io) so I guess it could go here http://chdk.wikia.com/wiki/Lua/Lua_Reference
That would be good.  Just let me know.
Ported :   A1200    SD940   G10    Powershot N    G16

*

Offline msl

  • *****
  • 1280
  • A720 IS, SX220 HS 1.01a
    • CHDK-DE links
Re: how to get the current file name / directory - almost solved
« Reply #6 on: 22 / October / 2012, 17:14:54 »
I haven't checked this on cameras with date folder naming, but if "raw in dir with jpeg" worked, so should this.

It works fine, tested with the SX220 (date folder naming) and the A720 (number folder naming).

The new function is a good idea. Finally there's an easy possibility to determine the path of the last picture. Thanks for that.

msl
CHDK-DE:  CHDK-DE links

*

Offline lapser

  • *****
  • 1093
Re: how to get the current file name / directory - almost solved
« Reply #7 on: 22 / October / 2012, 17:43:24 »
Thanks for adding that. I just tried get_image_dir() on the G1X and it works (date folder).
« Last Edit: 22 / October / 2012, 17:45:41 by lapser »
EOS-M3_120f / SX50_100b / SX260_101a / G1X_100g / D20_100b
https://www.youtube.com/user/DrLapser/videos


*

Offline reyalp

  • ******
  • 14082
Re: how to get the current file name / directory - almost solved
« Reply #8 on: 22 / October / 2012, 22:48:09 »
One thing I'm not certain about is what will happen when the folder rolls over. For example, if you are using date based names, and the date changes between when you shoot and when you check the function, you will probably get the wrong path (the Canon folder name function takes a date parameter).

I guess number should probably be OK, since they use the same counter as files, but obviously I haven't tested.

I will probably backport this to 1.1, since it seems unlikely to break anything.
Don't forget what the H stands for.

 

Related Topics