Image files corrupted

Hi

I have run the example codes including gif_1.py and snapshot_1.py successfully from the IDE. However, after saving the scripts to SD card as main.py and switching to connect with just USB power, I’m finding the new saved images are corrupted. Based on LED response the code appears to rerun when I reconnect to the computer. I’m not sure whether the corruption is occurring when on USB power or the program is overwriting them when reconnecting to computer.

I have done follow up tests with deleting the main.py between resetting the camera and found the file created outside of IDE was corrupted.

Any suggestions for what I should be doing to avoid image file corruption?

Thanks
David.

Hi, when saving to the SD card, you need to make sure the power isn’t removed before the script finishes saving images. Since a GIF is made up of multiple frames you may not have closed the file before the power is removed and etc. Additionally, if your script is always writing to the same filename then it’s possible the file can be corrupted easily as it’s overwritten each time the system boots.

If you post your code, I can provide some guidance.

The code is based on examples gif_1.py and snapshot_1.py

# This work is licensed under the MIT license.
# Copyright (c) 2013-2023 OpenMV LLC. All rights reserved.
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# Snapshot Example
#
# Note: You will need an SD card to run this example.
# You can use your OpenMV Cam to save image files.

import sensor
import time
import machine

sensor.reset()  # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565)  # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA)  # Set frame size to QVGA (320x240)
sensor.skip_frames(time=2000)  # Wait for settings take effect.

led = machine.LED("LED_BLUE")

start = time.ticks_ms()
while time.ticks_diff(time.ticks_ms(), start) < 3000:
    sensor.snapshot()
    led.toggle()

led.off()

img = sensor.snapshot()
img.save("example7.jpg")  # or "example.bmp" (or others)

raise (Exception("Please reset the camera to see the new file."))

and

# This work is licensed under the MIT license.
# Copyright (c) 2013-2023 OpenMV LLC. All rights reserved.
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# GIF Video Recording Example
#
# Note: You will need an SD card to run this example.
#
# You can use your OpenMV Cam to record gif files. You can either feed the
# recorder object RGB565 frames or Grayscale frames. Use photo editing software
# like GIMP to compress and optimize the Gif before uploading it to the web.

import sensor
import time
import gif
import machine

sensor.reset()  # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565)  # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QQVGA)  # Set frame size to QQVGA (160x120)
sensor.skip_frames(time=2000)  # Wait for settings take effect.

led = machine.LED("LED_RED")

led.on()
g = gif.Gif("example_gif2er.gif", loop=True)

clock = time.clock()  # Create a clock object to track the FPS.
for i in range(100):
    clock.tick()
    # clock.avg() returns the milliseconds between frames - gif delay is in
    g.add_frame(sensor.snapshot(), delay=int(clock.avg() / 10))  # centiseconds.
    print(clock.fps())
    #print("test")

g.close()
led.off()

raise (Exception("Please reset the camera to see the new file."))

I have been using the led.off() to inform completion of the script

Okay, so, yeah, the corruption is because it’s running again when attached to the PC… you should make the scripts create a file with a random name when they run using urandom. It’s going to run the script when it’s main.py on startup.