New to scripting: manipulate RAW file before writing - Script Writing - CHDK Forum supplierdeeply

New to scripting: manipulate RAW file before writing

  • 3 Replies
  • 1737 Views
New to scripting: manipulate RAW file before writing
« on: 06 / June / 2023, 20:23:39 »
Advertisements
Hi everyone. I am new to CHDK-scripting and this forum ;-)

I tried to to a script for hours now to manipulate an image which is captured before writing.
I tried to convert the image into greyscale picture.

Something like that:


width = rawop.get_raw_width()
height = rawop.get_raw_height()


    for y = 0, height - 1 do
        for x = 0, width - 1 do
            r, g1, b, g2 = rawop.get_pixels_rgbg(x,y)
           
            grayscale = (r + g1 + b) / 3
           
            rawop.set_pixels_rgbg(x, y, grayscale, grayscale, grayscale)
        end
    end


I always got the error there is no current image.

How can I access the captured picture?

Thanks,
Michael


*

Offline reyalp

  • ******
  • 14121
Re: New to scripting: manipulate RAW file before writing
« Reply #1 on: 07 / June / 2023, 02:06:59 »
Hi everyone. I am new to CHDK-scripting and this forum ;-)

I tried to to a script for hours now to manipulate an image which is captured before writing.
I tried to convert the image into greyscale picture.

Something like that:


width = rawop.get_raw_width()
height = rawop.get_raw_height()


    for y = 0, height - 1 do
        for x = 0, width - 1 do
            r, g1, b, g2 = rawop.get_pixels_rgbg(x,y)
           
            grayscale = (r + g1 + b) / 3
           
            rawop.set_pixels_rgbg(x, y, grayscale, grayscale, grayscale)
        end
    end


I always got the error there is no current image.

How can I access the captured picture?
Is your code running in the raw hook?  If so, which camera and shooting mode?

If not, you should generally
1) set the raw hook with hook_raw.set( timeout in milliseconds)
2) shoot using key presses, like
Code: [Select]
press'shoot_half'
repeat sleep(10) until get_shooting()
click'shoot_full'
...
3) After shooting, use hook_raw.wait_ready (needs require'hookutil') before your loop
4) use hook_raw.continue() after the loop.

You can find examples of this approach in CHDK/SCRIPTS/TEST/rawdraw.lua, and additional hook examples in hooktest.lua and drtest.lua.

Note looping over the entire image pixel by pixel will be quite slow, and extremely slow if you don't use set_yield to reduce automatic script yielding. Without testing, I'd guess minutes. The timeout you set when you call hook_raw.set needs to be longer than the time your loop takes. If it times out sooner, you'll probably get the error mentioned.

Don't forget what the H stands for.

Re: New to scripting: manipulate RAW file before writing
« Reply #2 on: 08 / June / 2023, 05:44:47 »
Thanks a lot for your help.

Now I could write a little test script which paints the Ukrainian flag into a photo. It works fine.

I have to fiddle with your advice with "set_yield".

*

Offline reyalp

  • ******
  • 14121
Re: New to scripting: manipulate RAW file before writing
« Reply #3 on: 08 / June / 2023, 16:59:08 »
I have to fiddle with your advice with "set_yield".
You can find some documentation at https://chdk.fandom.com/wiki/Script_commands#set_yield

The default behavior is to yield every 2500 Lua VM instructions or 10 ms, whichever comes first. A tight loop that never yields may cause crashes or other problems. There's no hard and fast rule for what's OK, but I would generally expect a few hundred ms to be OK, so you could use something like set_yield(-1, 100)

Since the task that runs scripting will normally sleep for 10ms per yield, one the max_ms (second argument to set_yield) parameter is over 100ms the gains from larger values are marginal.

You can find some general discussion of Lua performance in https://chdk.setepontos.com/index.php?topic=12451.msg123416#msg123416 and https://chdk.setepontos.com/index.php?topic=6729.msg71456#msg71456
Don't forget what the H stands for.


 

Related Topics


SimplePortal 2.3.6 © 2008-2014, SimplePortal