3

How to scale a picture to fit a window/layout?

enter image description here

With the code below the original image is not actually enlarged to 300px, it is displayed with the original image size instead.

import maya.cmds as cmds

if (cmds.window(window1, exists=True)):
    cmds.deleteUI(window1)

window1 = cmds.window(w=300, h=300)
layout = cmds.columnLayout(w=300, h=300)
cmds.picture( image='image.png', w=300, h=300)

cmds.showWindow( window1 )
Jim
  • 89
  • 8

2 Answers2

1

Try this

import maya.OpenMaya as om

def resize_image(source_image, output_image, width, height):

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

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


resizeImage('<source_image.png>','<output_image.png>', 300, 300)
Amir Afianian
  • 2,679
  • 4
  • 22
  • 46
  • How does this work if I have a dynamic interface? Then this function is writing files for every size that will be used... – Jim Nov 01 '20 at 13:31
  • When you resize a picture, you are in fact, producing a new image. That new image either has to be written as a new image or, it must be written over the previous image. Alternatively, you can write the new image and remove the older one. – Amir Afianian Nov 01 '20 at 13:33
  • Just added a gif to the main post, hope this makes it more clear what I want to do. – Jim Nov 01 '20 at 13:38
  • @user2625615 you need to trigger an event in your dynamic ui so when you are using certain drawing, release event, it will update the image. You might want to use QT because maya ui cmds is not enough flexible – DrWeeny Nov 03 '20 at 08:49
  • The width/height attribute in the cmds.picture is not working for me now. If we can get that to work I can bind dynamic ui events to the image size. I try to avoid Qt and use the maya defaults for simplicity (long as it is possible) – Jim Nov 03 '20 at 09:15
0

I have used iconTextButton to set an image with different width and height. Use adjustableColumn=False to set width/height of your image. You can also try iconTextStaticLabel cmd. Hope this will work for you..

import maya.cmds as cmds

if cmds.window('Test',exists=True):
   cmds.deleteUI('Test')
  
cmds.window('Test',title='Test Image', bgc=[0.266, 0.266, 0.266], widthHeight=[600,400], sizeable = True)
parentLayout = cmds.columnLayout(adjustableColumn=False)
cmds.iconTextButton(style="iconOnly",image1='Capture.png',p=parentLayout,w=150,h=150)
cmds.setParent('..')
cmds.showWindow('Test')
Epsi Vennila
  • 190
  • 3
  • 9