Python: Save image with a path

Python: Save image with a path

Anonymous
Not applicable
2,539 Views
6 Replies
Message 1 of 7

Python: Save image with a path

Anonymous
Not applicable

Hi guys,

 

I want to make an script to store all the texture images in the same folder. This are the variables in my script:

 

ext_Path = String with the path to the folder (C:\Users\me\Desktop\TextuFolder\)

filename_ext = Texture name and format (Texture1.jpg)

full_Path = The complete path wehere I'm storing the image (C:\Users\me\Desktop\TextuFolder\Texture1.jpg)

 

I just need the command to store the images in the selected path. Something like file() or saveImage(). But I'm really newbie in Python programing.

 

Thanks in advance.

0 Likes
Accepted solutions (1)
2,540 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable

No one? 😕

0 Likes
Message 3 of 7

osidedan
Advocate
Advocate
Accepted solution

Hello! what you probably need to do is import the shutil python module, and then use the copyfile function from that module.

 

here's an example:

 

import shutil
shutil.copyfile('c:/source/test_image.png', 'c:/destination/test_image_copy.png')

 

 

of note, the following things have to exist for the copy to work:

1. the source FILE must exist.

2. the destination FOLDER PATH must exist.

 

 

Let me know if you need more info on any of that!

 

Edit: to clarify, shutil is a native python module, meaning it is just part of python, it's not something maya related. Python has a TON of native modules that you can import and use to do all kinds of things. 

Message 4 of 7

Anonymous
Not applicable

Hi!

 

First of all, thanks for your answer. I tried using your solution but I had an unexpected error: 

 

Maya.PNG

And both things exists: destination folder (I get the path with a cmds.fileDialog2(...)) and the source image (cmds.ls(type="file")). I also tried doing it with the shutil.copy(src, dst) operation but the same error appears.

 

Thanks!

0 Likes
Message 5 of 7

osidedan
Advocate
Advocate

hmm, interesting. To narrow things down, I would recommend trying to doing a simple copy first. Not one that is part of your script, just a simple one liner of shutil.copy(), and just use strings for your source and destination. If that works, then we know that we need to make adjustments to your script. if that DOESN'T work, then there may be something corrupt with your Maya preferences, or the python install. 

 

Try that super simple copy, and let me know your results!

Message 6 of 7

Anonymous
Not applicable

Good morning!

 

After reinstalling Python, shutil works perfectly! 

 

Thanks!!

Message 7 of 7

osidedan
Advocate
Advocate

You’re welcome!

0 Likes