简体   繁体   中英

Resize images in Maya with Python

I'm pretty new to coding in general. I want a script that could batch process images inside Maya. The process should be simple: open an image from a given path folder, resize it, overwrite it.

I know PIL should be able to do that, however it appears so it's not supported since Maya 2014 or something. I've tried the code in Maya 2012 and it works but since I want my code to be accessible to all, I want to find a workaround.

I'm aware someone re-released a module so it can be supported on more recent builds (on windows only though).

http://mistermatti.wordpress.com/2014/02/04/maya-2014-with-pythons-pil-module/

But even when I install it in my build, I get this error when attempting to open my file:

open("E:/SoNuchframe000-00000.png")

cannot identify image file

I've tried copying the whole module into my own script and it does the same thing.

On the side I've also tried the OpenMaya module

    image.readFromFile('E:/SoNuchframe000-00000.png')
    image.resize(2048, 2048, False)
    image.writeToFile('E:/SoNuchframe000-00000.png' + 'Resized.png')

and this gives me an empty image of 12mb (so by the way, if I could keep it compressed, that would be awesome).

So now, I've reached the limit of my powers. What can I do from here?

You may use QImage.scaled() from PySide/PyQt http://pyqt.sourceforge.net/Docs/PyQt4/qimage.html#scaled

from PySide.QtCore import *
from PySide.QtGui import *

picture = QImage(path)
pic_rescaled = picture.scaled(2048, 2048, Qt.KeepAspectRatio)
pic_rescaled.save(path, "PNG")

Should work on any Maya 2014+

You're missing the compression argument within image.writeToFile(path, compression) . In this case it should be 'png', but in general this should match the extension of the file you loaded originally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM