import camera
import network
import time
import gc

# Initializing the Camera
def camera_init():
    # Disable camera initialization
    camera.deinit()
    # Enable camera initialization
    camera.init(0, d0=4, d1=5, d2=18, d3=19, d4=36, d5=39, d6=34, d7=35,
                format=camera.JPEG, framesize=camera.FRAME_VGA, 
                xclk_freq=camera.XCLK_10MHz,
                href=23, vsync=25, reset=-1, pwdn=-1,
                sioc=27, siod=26, xclk=21, pclk=22, fb_location=camera.PSRAM)
        # recommended framesize: framesize=camera.FRAME_VGA
        # recommended clock: xclk_freq=XCLK_20MHz
        # recommended format: format=camera.JPEG  - GRAYSCALE = no compression

#    camera.framesize(camera.FRAME_96X96) # Set the camera resolution
#     camera.framesize(camera.FRAME_VGA) # Set the camera resolution
    # The options are the following:
    # FRAME_96X96 FRAME_QQVGA FRAME_QCIF FRAME_HQVGA FRAME_240X240
    # FRAME_QVGA FRAME_CIF FRAME_HVGA FRAME_VGA FRAME_SVGA
    # FRAME_XGA FRAME_HD FRAME_SXGA FRAME_UXGA
    # Note: The higher the resolution, the more memory is used.
    # Note: And too much memory may cause the program to fail.
    
    camera.flip(1)                       # Flip up and down window: 0-1
    camera.mirror(1)                     # Flip window left and right: 0-1
    camera.saturation(0)                 # saturation: -2,2 (default 0). -2 grayscale 
    camera.brightness(0)                 # brightness: -2,2 (default 0). 2 brightness
    camera.contrast(0)                   # contrast: -2,2 (default 0). 2 highcontrast
    camera.quality(10)                   # quality: # 10-63 lower number means higher quality
    # Note: The smaller the number, the sharper the image. The larger the number, the more blurry the image
    
    camera.speffect(camera.EFFECT_BW)  # special effects:
    # EFFECT_NONE (default) EFFECT_NEG EFFECT_BW EFFECT_RED EFFECT_GREEN EFFECT_BLUE EFFECT_RETRO
    camera.whitebalance(camera.WB_OFFICE)  # white balance
    # WB_NONE (default) WB_SUNNY WB_CLOUDY WB_OFFICE WB_HOME

def capture_image(k):
    gc.collect()  # Run garbage collection to free up memory
    time.sleep(0.05)  # Allow camera to settle after any previous operations
    # Capture the image
    sizesave = 500000
#     sizesave = 0
    for i in range(1):
        gc.collect()  # Run garbage collection to free up memory
        time.sleep(0.01)  # Allow camera to settle after any previous operations
#         print(' capture: ', i)
        img = camera.capture()
        size = len(img)
#         print(f"  captured image size: {size}")
        if size < sizesave:
            sizesave = size
            # Save the image
            filename = "captured_image"+str(k)+".jpg"
            with open(filename, "wb") as file:
                file.write(img)
    return sizesave

k = 0
print('Taking a photo --')
camera_init()
time.sleep(1)
size = capture_image(k)
print('  Size of saved image: ', size)
