I'm new to Python and need assistance. I am performing segmentation with my own medical data set. The test images in my folder are named like "". I would like to remove the actual image extension and append the image names with "_mask.png". The current code throws an error.

def labelVisualize(num_class,color_dict,img):
    img = img[:,:,0] if len(img.shape) == 3 else img
    img_out = np.zeros(img.shape + (3,))
    for i in range(num_class):
        img_out[img == i,:] = color_dict[i]
    return img_out / 255

def saveResult(save_path,npyfile,flag_multi_class = False,num_class = 2):
    for i,item in enumerate(npyfile):
        img = labelVisualize(num_class,COLOR_DICT,item) if flag_multi_class else item[:,:,0]

2 Answers 2


Most of the code you provided doesn't do much to help your actual problem of renaming the files. I will just focus on tha tpart by showing you how to get the filenames you want.

You can rename the files, as you describe them, like this - assuming you have a list of filenames that match your description.

filenames = ['', '', '']

for file in filenames:

    # Do what you want with the file...
    # Image processing and modelling, etc.

    # Replace the extension with your custom ending
    final_filename = file.replace('.png', '_mask.png')

Another way to do it, breaking down the removal and addition into two steps

for file in filenames:
    file_without_ext = file.replace('.png', '')

    # Add you custom ending
    final_filename = file_without_extension + '_mask.png'

    # Another variant - joining the extension to the file_without_ext using "_"
    final_filename = '_'.join(file_without_ext, 'mask.png')

The three variants of final_filename will be identical.

Now you can save the output of your image processing using the final_filename.


I'm using the code below to take images using OV2311 and RPI Zero w. However, I don't want my previous image to be overwritten.

I therefore request for help from experts here.

import arducam_mipicamera as arducam import v4l2 #sudo pip install v4l2 import time

def set_controls(camera): try: print("Reset the focus...") camera.reset_control(v4l2.V4L2_CID_FOCUS_ABSOLUTE) except Exception as e: print(e) print("The camera may not support this control.")

    print("Enable Auto Exposure...")
    camera.software_auto_exposure(enable = True)
    print("Enable Auto White Balance...")
    camera.software_auto_white_balance(enable = True)
except Exception as e:

if name == "main": try: camera = arducam.mipi_camera() print("Open camera...") camera.init_camera() print("Setting the resolution...") fmt = camera.set_resolution(1920, 1080) print("Current resolution is {}".format(fmt)) # print("Start preview...") # camera.start_preview(fullscreen = False, window = (0, 0, 1280, 720)) set_controls(camera) time.sleep(1) frame = camera.capture(encoding = 'jpeg') frame.as_array.tofile("{}x{}.jpg".format(fmt[0],fmt[1]))

    print("Setting the resolution...")
    fmt = camera.set_resolution(3280, 2464)
    print("Current resolution is {}".format(fmt))
    frame = camera.capture(encoding = 'jpeg')

    # Release memory
    del frame
    # print("Stop preview...")
    # camera.stop_preview()
    print("Close camera...")
except Exception as e:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.