How can I convert an image to JPEG from the active material node with Python? I am adding to an add-on and I am trying to take the image in the active material node, make a backup of the original, and then with two different scripts, resize it, and convert it to JPEG.
Example:
In the active object's material, there is a 4k image attached to the diffuse, and a 4k image in the roughness input.
The user can select the diffuse image, click a button to resize it to 2k, then click on roughness image, click a button and resize it to 1024x1024 and then click another button to convert it to JPEG, automatically saving a backup, loading the new version and refreshing all modified images.
I was able to figure out how to resize, save and refresh all those images, but it overwrites the original, uses ops to save and I still don't know how to convert it to jpeg.
image = bpy.context.active_object.active_material.node_tree.nodes.active.image
image.scale(512,512)

bpy.ops.image.save_all_modified()

for image in bpy.data.images:
    image.reload()
Try
image.file_format = 'JPEG'
image.filepath_raw = your_new_filepath_here
image.save()