Resizing image files in Maya using Python

I’m trying to figure out a way to resize/save out image files from within Maya using Python… figured I’d use the PIL library but is there a more straightforward way?

if not…what’s the best way to get the right PIL version into Maya?

Maya 2012 is Python 2.6, my installation of Python is 2.7 so I can’t run the correct PIL installation file…figure I’m gonna have to install 2.6 then copy the libraries over but that seems awkward. :open_mouth:

we’re using PIL here for convenience - just copying it to site-packages works. Or if you don’t want to mess with your Maya folder, you can just append it to the pythonpath variable in the maya.env file.

This link might also be helpful if you want to run x64 and don’t like to compile a x64 PIL version yourself: http://www.lfd.uci.edu/~gohlke/pythonlibs/

Awesome! Thanks for the reply. Installing a 2.6 version of python, PIL, and setting the paths worked.

ObviousIdea free image resizer is also a free image converter. It resizes and simply converts images in batch mode with bulk editing. It is directly integrated into the windows shell context. You can download it from here Light Image Resizer, Photo Resizer, Image Converter

We can use MImage class…

# Python code:
import maya.OpenMaya as om

def resizeImage(sourceImage, outputImage, width, height):

    image = om.MImage()
    image.readFromFile(sourceImage)

    image.resize( width, height )
    image.writeToFile( outputImage, 'png')


resizeImage('<sourceImage.png>','<outputImage.png>', 800, 600)

@woopeex

crazy! totally missed that lol… thanks

Anyone have any ideas on how I can use this but keep the alpha channel intact? Seems like this suffers from the Maya wide problem of not properly picking up the alpha channel on a almost every file format but TGAs. Unfortunately I need this to work on BMPs with an alpha. Going to look into PIL, but I wanted to check here first before going the module route. I’d like to avoid that requirement if possible.

Never mind, I got it. MImage, PIL, and Imagemagick all have the same alpha problems. But with a system call nconvert (xnView’s tool) handles it perfectly.

[QUOTE=dekorkh;10613]I’m trying to figure out a way to resize/save out image files from within Maya using Python… figured I’d use the PIL library but is there a more straightforward way?[/QUOTE]

If you wanted to be really dirty about it, you could have the script load the image into maya as a background plate, render it at a lower resolution, then save the render.

It would definitely be dirty method but I’m sure it could be done. The problem though is it will likely have similar alpha channel dropping issues, given the Software Render and/or Mental Ray’s own inconsistencies with them.

I really can’t recommend doing this any other way than through an nConvert system call. There is no particular path it needs to install to, only takes a single line of code in your Maya script editor, supports tons of formats, and most importantly won’t drop the alpha channel.

I don’t know if you’re interested in resolving the problem you had with PIL, but I can’t reproduce it. If I use img.resize( ) on an image with alpha it retains it just fine on the resized result.

Do you have example code illustrating the problem?

Unfortunately no, I don’t have the code. I had a friend look into PIL while I tested MImage, Imagemagick and nConvert. I could be mistaken here given I’m going by his conclusions, but his verdict was BMPs with an alpha channel did not copy properly with PIL, whereas other formats like PNG did. From my tests, the same is true with MImage and Imagemagick. nConvert was the only one I tried that did the job correctly. Given how reliable xnView is, it comes as little surprise.

Yeah, alpha channels in BMP files are notoriously hit-and-miss as far as tools support goes. IIRC, alpha is not part of the official format and was added later in a semi-unofficial way. Or something like that.

Crispy4004:
Have to tried to convert the 32bit BMP to a 32bit TGA, use PIL or MImage to resize, then reconvert the 32bit TGA to a 32bit BMP?

Ugly, but should work…

SamiV.

If you have Photoshop, you could even call that through the COM interface. And then there is also imageMagick

samivRMD - Yeah I tried it. Converting it to TGA keeps the alpha, but converting it back to BMP drops it again.

felixSchl - That was going to be my next approach (thanks for the tutorial btw Adam). I previously got it working with a Photoshop droplet but that was less than ideal. Speaking of which anyone know what steps I would have to take to use the PS COM interface through Maya’s python? There’s certainly potential to do lots of great stuff with both communicating together.

Thanks everyone for your help. We ultimately got it setup at work by re-purposing some existing internal tools. Just more practical for us than picking up the nConvert licenses.

Alright, I also did some research too and from what I understand the 4*8bit BMP format is widely unsupported because it is not part of the Microsoft Bitmap specifications. I think this has been mentioned in this thread too though.

Anyway, not even ImageMagick picks up on the alpha channel. However, this guy seemed to have worked around the issue. Not sure how to translate that to DOS though and what the equivalent of the “dd” command would be (Although I found this)

The Photoshop solution still exists, but loading such a huge app to do such a tiny job seems somewhat counter intuitive.

Keen to see if someone cracks this