Images sometimes getting flipped

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10): Win11
PsychoPy version (e.g. 2024.2.4 Py 3.8): 2024.2.4
Standard Standalone Installation? (y/n) Y
Do you want it to also run online? (y/n) N
What are you trying to achieve?: Showing images (without getting them mysteriously reversed)

Link to the most relevant existing thread you have found: None from several google attempts

So I have created a very simple memory experiment where the builder retrieves the ImageName and the ImageSize from an excel file. For some reason that I have no idea about, some images randomly gets flipped (like left-to-right). This doesn’t happen for all the images and which images get flipped vary between tets (such that an image once can be flipped one way and then another time the other way). I really don’t understand why this might be happening!

I am very new to this, so I am not exactly sure which piece of code that does what, but I THINK these are the parts of code that retrieves and shows the images:

— Initialize components for Routine “TestTrial” —

PolyBack1 = visual.Rect(
    win=win, name='PolyBack1',
    width=(2, 2)[0], height=(2, 2)[1],
    ori=0.0, pos=(0, 0), draggable=False, anchor='center',
    lineWidth=1.0,
    colorSpace='rgb', lineColor='darkslategray', fillColor='darkslategray',
    opacity=None, depth=0.0, interpolate=True)
image_testphase = visual.ImageStim(
    win=win,
    name='image_testphase', 
    image='default.png', mask=None, anchor='center',
    ori=0.0, pos=(0, 0), draggable=False, size=1.0,
    color=[1,1,1], colorSpace='rgb', opacity=None,
    flipHoriz=False, flipVert=False,
    texRes=128.0, interpolate=True, depth=-1.0)

— Prepare to start Routine “TestTrial” —

    # create an object to store info about Routine TestTrial
    TestTrial = data.Routine(
        name='TestTrial',
        components=[PolyBack1, image_testphase],
    )
    TestTrial.status = NOT_STARTED
    continueRoutine = True
    # update component parameters for each repeat
    image_testphase.setSize(ImageSize)
    image_testphase.setImage(ImageFile)
    # store start times for TestTrial
    TestTrial.tStartRefresh = win.getFutureFlipTime(clock=globalClock)
    TestTrial.tStart = globalClock.getTime(format='float')
    TestTrial.status = STARTED
    thisExp.addData('TestTrial.started', TestTrial.tStart)
    TestTrial.maxDuration = None
    # keep track of which components have finished
    TestTrialComponents = TestTrial.components
    for thisComponent in TestTrial.components:
        thisComponent.tStart = None
        thisComponent.tStop = None
        thisComponent.tStartRefresh = None
        thisComponent.tStopRefresh = None
        if hasattr(thisComponent, 'status'):
            thisComponent.status = NOT_STARTED
    # reset timers
    t = 0
    _timeToFirstFrame = win.getFutureFlipTime(clock="now")
    frameN = -1
    
    # --- Run Routine "TestTrial" ---
    # if trial has changed, end Routine now
    if isinstance(Test_Practice, data.TrialHandler2) and thisTest_Practice.thisN != Test_Practice.thisTrial.thisN:
        continueRoutine = False
    TestTrial.forceEnded = routineForceEnded = not continueRoutine
    while continueRoutine and routineTimer.getTime() < 2.0:
        # get current time
        t = routineTimer.getTime()
        tThisFlip = win.getFutureFlipTime(clock=routineTimer)
        tThisFlipGlobal = win.getFutureFlipTime(clock=None)
        frameN = frameN + 1  # number of completed frames (so 0 is the first frame)
        # update/draw components on each frame
        
        # *PolyBack1* updates
        
        # if PolyBack1 is starting this frame...
        if PolyBack1.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
            # keep track of start time/frame for later
            PolyBack1.frameNStart = frameN  # exact frame index
            PolyBack1.tStart = t  # local t and not account for scr refresh
            PolyBack1.tStartRefresh = tThisFlipGlobal  # on global time
            win.timeOnFlip(PolyBack1, 'tStartRefresh')  # time at next scr refresh
            # add timestamp to datafile
            thisExp.timestampOnFlip(win, 'PolyBack1.started')
            # update status
            PolyBack1.status = STARTED
            PolyBack1.setAutoDraw(True)
        
        # if PolyBack1 is active this frame...
        if PolyBack1.status == STARTED:
            # update params
            pass
        
        # if PolyBack1 is stopping this frame...
        if PolyBack1.status == STARTED:
            # is it time to stop? (based on global clock, using actual start)
            if tThisFlipGlobal > PolyBack1.tStartRefresh + 2-frameTolerance:
                # keep track of stop time/frame for later
                PolyBack1.tStop = t  # not accounting for scr refresh
                PolyBack1.tStopRefresh = tThisFlipGlobal  # on global time
                PolyBack1.frameNStop = frameN  # exact frame index
                # add timestamp to datafile
                thisExp.timestampOnFlip(win, 'PolyBack1.stopped')
                # update status
                PolyBack1.status = FINISHED
                PolyBack1.setAutoDraw(False)
        
        # *image_testphase* updates
        
        # if image_testphase is starting this frame...
        if image_testphase.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
            # keep track of start time/frame for later
            image_testphase.frameNStart = frameN  # exact frame index
            image_testphase.tStart = t  # local t and not account for scr refresh
            image_testphase.tStartRefresh = tThisFlipGlobal  # on global time
            win.timeOnFlip(image_testphase, 'tStartRefresh')  # time at next scr refresh
            # add timestamp to datafile
            thisExp.timestampOnFlip(win, 'image_testphase.started')
            # update status
            image_testphase.status = STARTED
            image_testphase.setAutoDraw(True)
        
        # if image_testphase is active this frame...
        if image_testphase.status == STARTED:
            # update params
            pass
        
        # if image_testphase is stopping this frame...
        if image_testphase.status == STARTED:
            # is it time to stop? (based on global clock, using actual start)
            if tThisFlipGlobal > image_testphase.tStartRefresh + 2-frameTolerance:
                # keep track of stop time/frame for later
                image_testphase.tStop = t  # not accounting for scr refresh
                image_testphase.tStopRefresh = tThisFlipGlobal  # on global time
                image_testphase.frameNStop = frameN  # exact frame index
                # add timestamp to datafile
                thisExp.timestampOnFlip(win, 'image_testphase.stopped')
                # update status
                image_testphase.status = FINISHED
                image_testphase.setAutoDraw(False)
        
        # check for quit (typically the Esc key)
        if defaultKeyboard.getKeys(keyList=["escape"]):
            thisExp.status = FINISHED
        if thisExp.status == FINISHED or endExpNow:
            endExperiment(thisExp, win=win)
            return
        # pause experiment here if requested
        if thisExp.status == PAUSED:
            pauseExperiment(
                thisExp=thisExp, 
                win=win, 
                timers=[routineTimer], 
                playbackComponents=[]
            )
            # skip the frame we paused on
            continue
        
        # check if all components have finished
        if not continueRoutine:  # a component has requested a forced-end of Routine
            TestTrial.forceEnded = routineForceEnded = True
            break
        continueRoutine = False  # will revert to True if at least one component still running
        for thisComponent in TestTrial.components:
            if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
                continueRoutine = True
                break  # at least one component has not yet finished
        
        # refresh the screen
        if continueRoutine:  # don't flip if this routine is over or we'll get a blank screen
            win.flip()
    
    # --- Ending Routine "TestTrial" ---
    for thisComponent in TestTrial.components:
        if hasattr(thisComponent, "setAutoDraw"):
            thisComponent.setAutoDraw(False)
    # store stop times for TestTrial
    TestTrial.tStop = globalClock.getTime(format='float')
    TestTrial.tStopRefresh = tThisFlipGlobal
    thisExp.addData('TestTrial.stopped', TestTrial.tStop)
    # using non-slip timing so subtract the expected duration of this Routine (unless ended on request)
    if TestTrial.maxDurationReached:
        routineTimer.addTime(-TestTrial.maxDuration)
    elif TestTrial.forceEnded:
        routineTimer.reset()
    else:
        routineTimer.addTime(-2.000000)

Any help would be really appreciated!