Python Forum
def function SyntaxError: invalid syntax
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
def function SyntaxError: invalid syntax
#1
Hello, need help. I am trying to build a light spectrometer on a raspberry PI 3 from "http://blog.durablescope.com/post/BuiltASpectrometer/"

when i implement spectrometer.py file I get this message:
"File "spectrometer.py", line 42
def find_aperture(pic_pixels, pic_width: int, pic_height: int) -> object:
^
SyntaxError: invalid syntax"

here is part of code:

import sys
import math
import time
import picamera
from fractions import Fraction
from collections import OrderedDict
from PIL import Image, ImageDraw, ImageFile, ImageFont


# scan a column to determine top and bottom of area of lightness
def get_spectrum_y_bound(pix, x, middle_y, spectrum_threshold, spectrum_threshold_duration):
    c = 0
    spectrum_top = middle_y
    for y in range(middle_y, 0, -1):
        r, g, b = pix[x, y]
        brightness = r + g + b
        if brightness < spectrum_threshold:
            c = c + 1
            if c > spectrum_threshold_duration:
                break
        else:
            spectrum_top = y
            c = 0

    c = 0
    spectrum_bottom = middle_y
    for y in range(middle_y, middle_y * 2, 1):
        r, g, b = pix[x, y]
        brightness = r + g + b
        if brightness < spectrum_threshold:
            c = c + 1
            if c > spectrum_threshold_duration:
                break
        else:
            spectrum_bottom = y
            c = 0

    return spectrum_top, spectrum_bottom


# find aperture on right hand side of image along middle line
def find_aperture(pic_pixels, pic_width: int, pic_height: int) -> object:
    middle_x = int(pic_width / 2)
    middle_y = int(pic_height / 2)
    aperture_brightest = 0
    aperture_x = 0
    for x in range(middle_x, pic_width, 1):
        r, g, b = pic_pixels[x, middle_y]
        brightness = r + g + b
        if brightness > aperture_brightest:
            aperture_brightest = brightness
            aperture_x = x

    aperture_threshold = aperture_brightest * 0.9
    aperture_x1 = aperture_x
    for x in range(aperture_x, middle_x, -1):
        r, g, b = pic_pixels[x, middle_y]
        brightness = r + g + b
        if brightness < aperture_threshold:
            aperture_x1 = x
            break

    aperture_x2 = aperture_x
    for x in range(aperture_x, pic_width, 1):
        r, g, b = pic_pixels[x, middle_y]
        brightness = r + g + b
        if brightness < aperture_threshold:
            aperture_x2 = x
            break

    aperture_x = (aperture_x1 + aperture_x2) / 2

    spectrum_threshold_duration = 64
    aperture_y_bounds = get_spectrum_y_bound(pic_pixels, aperture_x, middle_y, aperture_threshold, spectrum_threshold_duration)
    aperture_y = (aperture_y_bounds[0] + aperture_y_bounds[1]) / 2
    aperture_height = (aperture_y_bounds[1] - aperture_y_bounds[0]) * 0.9

    return {'x': aperture_x, 'y': aperture_y, 'h': aperture_height, 'b': aperture_brightest}
Thank you
Reply
#2
Solved,
the problem was in the python version
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print(data) is suddenly invalid syntax db042190 6 1,196 Jun-14-2023, 02:55 PM
Last Post: deanhystad
  [Solved] unkown (to me) function def parm "name1:name2" syntax. MvGulik 5 1,068 Nov-11-2022, 11:21 AM
Last Post: MvGulik
  meaning of -> syntax in function definition DrakeSoft 5 1,964 Apr-09-2022, 07:45 AM
Last Post: DrakeSoft
  SyntaxError: invalid syntax ?? korenron 15 5,744 Jan-25-2022, 11:46 AM
Last Post: korenron
  Invalid syntax with an f-string Mark17 7 7,825 Jan-14-2022, 04:44 PM
Last Post: Mark17
  invalid syntax in my class CompleteNewb 2 1,908 Dec-13-2021, 09:39 AM
Last Post: Larz60+
Star I'm getting syntax error while using input function in def. yecktmpmbyrv 1 1,973 Oct-06-2021, 09:39 AM
Last Post: menator01
Exclamation Invalid syntax error(Predict Ethereum Price) lulu43366 2 3,171 Sep-24-2021, 01:24 PM
Last Post: lulu43366
  Tuple generator, and function/class syntax quazirfan 3 3,882 Aug-10-2021, 09:32 AM
Last Post: buran
  Unexplained Invalid syntax Error cybertooth 5 3,257 Aug-02-2021, 10:05 AM
Last Post: cybertooth

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020