Random selection of different images in different categories without repetition across different blocks and counter balancing them to have 2 version of experiment

Hi all, I’m kinda new to coding and Psychopy.
OS (e.g. Win10):
Win10
PsychoPy version (e.g. 1.84.x):
version 2023.2.3
Standard Standalone? (y/n) If not then what?:
What are you trying to achieve?:
The experiment I want to do the programming has 8 blocks of images. each block has 8 images which are in different categories. Half of the objects (32) are animate and the other half are inanimate (other 32 objects). Half of each of these categories are line drawing images and the other half are photographs.
Each block contains equal numbers of animate/inanimate objects with an equal number of hand drawing/photograph images.
I also want to counterbalance the presentation of objects as either photographs or line drawings across participants, so in the end we have two versions of the same experiment.

What specifically went wrong when you tried that?:

I don’t know how to choose images randomly however having the same number of animate/inanimate objects and the same number of photographs/hand drawing images.
I tried to create routines for each category of images, save them in a list, then shuffle them, and make a condition so whenever the number of images of each category reaches the limit it stops the loop, but it seems it’s not the case since each image for me has 2 different categories.

I appreciate your help. Thanks all!