Document.scaleImage() causes the canvas to become invisible

I looked at the previous threads tagged scripting help and didn’t see anything similar. I’m on 5.1.5, but I don’t see anything in the 5.2 release notes that looks relevant to my issue.

I’ve been learning the script api recently, and one thing I would like to do is to scale an image after pasting it into a new document. The pasting part works until I try using Document.scaleImage()

from krita import *

doc_cur = Krita.instance().activeDocument()
a_node = doc_cur.activeNode()
img_w = doc_cur.width()
img_h = doc_cur.height()

doc_new = Application.createDocument(img_w,img_h, "Temp Tile Preview", "RGBA", "U8", "", 120.0)

pixels = a_node.pixelData(0,0,img_w,img_h)
root = doc_new.rootNode()
a_node = doc_new.createNode("layertopasteto","paintLayer")
a_node.setPixelData(pixels,0,0,img_w,img_h)
root.addChildNode(a_node,None)
view = Application.activeWindow().addView(doc_new)

new_w = int(img_w/8)
new_h = int(img_h/8)

doc_new.scaleImage(new_w,new_h,new_w,new_h,'Lanczos3')

When doc_new.scaleImage() is present the new document opens in krita and I can see the image for a frame (or similar low amount of time) before it disappears. Maybe I’m seeing it at the larger res before the scaling happens? If I remove the scaleImage() call it is visible, but obviously not scaled.

If I toggle the layers it’s still not visible.

If I save the image and reload it, the image will be visible and scaled to the correct size, but I would like to not have to do that.

I’ve tried a few things, Document.refreshProjection() looks relevant, but doesn’t fix this. I’ve tried resizing the image with Document.resizeImage() after the scale, and I’ve tried resizing with Document.setWidth() and Document.setHeight(), but none of them help.

Is there something I’m missing? I know I can simply use the GUI resize menu or use something like imagemagick, but the whole reason I want to do this is to automate my existing workflow of copying to a new document and resizing with image->scale image to new size

Make sure the arguments you pass to scaleImage are larger than 0.

They are. On the image I’ve been testing with they come out to 32 and 32, both ints and I printed them a line before the scaleImage() call to make sure

What document dimensions are shown in the status bar and what zoom level, after the script ran?

Your script works for me but what happened is that since the new image is much smaller and the origin of the view does not get reset it was simply outside of the view and I had to recenter it by pressing 2 on the keyboard.

The image resizes to the expected int(input_dimensions/8), and in the OP I mentioned that if I save the result image and reopen it it is perfectly intact and at the expected size. 2 isn’t bound to recenter for me because I’ve customized my install somewhat heavily. I’m not sure that’s my issue because the rulers disappear as well. I tried rebinding some things in ‘configure krita’ that looked relevant to recentering but they didn’t do anything.

(the things I tried were krita->painting->move to center X & it’s Y counterpart, and I also tried menu->view->snap image center)

What document dimensions are shown in the status bar and what zoom level, after the script ran?

The dimensions are 32x32 (with 256x256 input) after the script runs

The zoom level goes to 359%, seemingly no matter what it was before I ran the script.

I also had a hunch to look at the overview docker and it shows the image intact without saving and reloading, as do the layer thumbnails.

Edit: the fact that it works for you makes me think it might be a build specific bug because I’m on windows.

Here’s me running your script, it works as expected. On Windows.

Perhaps it’s something with the input image, since yours is much smaller. For example setting the resolution to be the height and width is a bit odd. Especially when you look at how it is implemented.

void Document::scaleImage(int w, int h, int xres, int yres, QString strategy)
{
    if (!d->document) return;
    KisImageSP image = d->document->image();
    if (!image) return;
    QRect rc = image->bounds();
    rc.setWidth(w);
    rc.setHeight(h);
 
    KisFilterStrategy *actualStrategy = KisFilterStrategyRegistry::instance()->get(strategy);
    if (!actualStrategy) actualStrategy = KisFilterStrategyRegistry::instance()->get("Bicubic");
 
    image->scaleImage(rc.size(), xres / 72.0, yres / 72.0, actualStrategy);
    image->waitForDone();
}

I thought resolution was just a printing thing so I figured any non zero value would work, if that’s not the case maybe I should try 300 because that’s the default that I never change from when creating an image

I guess that was the case.

Would be my assumption too, to be honest.

1 Like

I still don’t get why I could run my script, save the result, close krita, open krita, and open the result and it would be scaled correctly when that was the issue though.

You can get the original resolution from the other file, if you feel uncomfortable hard coding the resolution, just in case.

Maybe it’s that Krita uses a sane default when it detects an unusual resolution or perhaps it’s something else. I’m not deep enough into the rabit hole that is Krita code.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.