def db_get_nonprocessed_images(conn, methodid):
    """Get from table Image the ids of the non-corrupted images that were not
    processed yet by the method id

    Args:
    conn(psycopg2.connection): open connection to the PG database
    methodid(int): method id

    Returns:
    list: ids of non-processed images

    """
    cur = conn.cursor()

    print('Getting non-processed images...')
    query = '''
    SELECT Image.id from Image INNER JOIN
    (
            SELECT Image.id  FROM Image LEFT OUTER JOIN
            (
                    SELECT imageid FROM Imagemethod INNER JOIN method ON
                    Imagemethod.methodid=Method.id AND Method.id={}
            ) AS Q1
            ON Image.id=Q1.imageid WHERE Q1.imageid IS NULL
    )
    NONPR  ON Image.id=NONPR.id AND Image.corrupted=false;
    '''.format(methodid)

    cur.execute(query)
    ids = [x[0] for x in cur.fetchall()]
    print('Got {} non-processed images.'.format(len(ids)))
    return ids