2
$\begingroup$

I want to write a simple script that scans all the textures files in a folder containing .dds format files and then save them as .tga. I want to know how I can:

  • open the texture file
  • get a reference to the opened texture file
  • save_as (not save) .tga files (Targa)
  • close the opened texture file

Manually I can do the following to convert the files:

  • Open Blender, change to UV Editing mode from default
  • Click on Open image and load the desired texture image (which is .dds)
  • Click on Image -> save as Image and select Targa format while saving

Essentially I just want the automation of the above mentioned process!

Thank you for help!

$\endgroup$
5
  • $\begingroup$ Why blender? Did you tried Imagemagick or PythonMagick? $\endgroup$
    – brockmann
    Sep 1, 2017 at 10:30
  • $\begingroup$ @brockmann Because I am using blender to import a custom 3D file format of a game and then export these models as .fbx to be used in unreal engine 4. Since the unreal engine 4 doesn't support the .dds texture files used on these models, I need to convert them as well. And I felt it would be better if i converted textures alongside models in blender. $\endgroup$
    – Ryū Jin
    Sep 1, 2017 at 10:34
  • $\begingroup$ Ok, can you please share any sample file? $\endgroup$
    – brockmann
    Sep 1, 2017 at 11:12
  • $\begingroup$ @brockmann I am not sure why you need sample, all I wanted was some help with python code to automate loading an image in uv editor of blender, and then save_as with different file format. (I mean, I tested it manually in blender but i wanted to automate it) Anyway, here is the sample: drive.google.com/file/d/0B0WMRZkjfBoCeTJQRmx6OEZRQXM/… $\endgroup$
    – Ryū Jin
    Sep 1, 2017 at 11:24
  • $\begingroup$ @brockmann I figured it out and posted in answer. Thank you! $\endgroup$
    – Ryū Jin
    Sep 1, 2017 at 12:01

1 Answer 1

3
$\begingroup$

Okay, I figured it out.

image = bpy.data.images.load("filepath_to_source")  
image.file_format = "TARGA"  
image.save_render(<filepath_to_destination>)

I tried using image.save() function before I posted this question, which raised a Runtime Error:

Image does not have any image data

and I thought I was doing something wrong. Using save_render() function seem to have fixed it. I still don't know why save() didn't/doesn't work.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .