Exporting and Deleting Images in Python Blender?

Hi,

I’m using Blender unconventionally for this one.

  1. Import image
  2. Export the image in .PNG or .JPG format
  3. Delete image in the blender file

Redo the whole process until all images are exported.

For number 1, I have bpy.ops.image.open.

My problem is #2 and #3.
For number #2, bpy.ops.image.save_as doesn’t specify what specific image to be exported. Its filepath parameter only says what is the output but not what the image (input) it will be using.

For number #3, I’m lost on this one. the console doesn’t return any code when I delete an image.

Is there a way around this?

Thank you

Are you just doing this to batch convert images? Why not use imagemagick?

Anyway.

image = bpy.data.images.load(image_path) # Load
# Saving I'm not 100% sure about, likely
image.file_format = 'PNG' # or 'JPEG'
image.save(output_filepath)

bpy.data.images.remove(image) # Delete
1 Like

@init_pixel

Thanks! The functions directed me to search proper keywords.
Here is the working code so far (excluding the loading command and removing command)

import bpy
import os

for image in bpy.data.images:
    if image == bpy.data.images['Render Result']:
        continue
    
    image.file_format = 'PNG'
    output_filepath = r"C:\Users\Luke\Desktop\test_blender_export"
    image.filepath_raw =  os.path.join(output_filepath, image.name + ".PNG")
    image.save()

Thank you again!