There is no general code for balancing a dataset. Yet there are methods for this purpose like data augmentation which means that you add artificial samples to your dataset so that each class has the same number of items. imgaug is a python library specifically developed for augmenting image data. You can use it to balance your datatset.