Bug 388961 - Fit canvas to the layers size
Summary: Fit canvas to the layers size
Status: RESOLVED FIXED
Alias: None
Product: krita
Classification: Applications
Component: Scripting (show other bugs)
Version: unspecified
Platform: Debian testing Linux
: NOR normal
Target Milestone: ---
Assignee: Krita Bugs
URL:
Keywords: triaged
Depends on:
Blocks:
 
Reported: 2018-01-14 13:41 UTC by rkfg
Modified: 2018-09-28 10:51 UTC (History)
3 users (show)

See Also:
Latest Commit:
Version Fixed In:


Attachments
A plugin that adds the script to the menu as well. (3.23 KB, application/zip)
2018-09-28 10:24 UTC, Halla Rempt
Details

Note You need to log in before you can comment on or make changes to this bug.
Description rkfg 2018-01-14 13:41:33 UTC
This is a great feature I miss from GIMP where it's called "Fit Canvas to Layers". It must be very easy to implement and it's good when creating collages or strips from multiple images. This function should set the canvas size so that all layers fit into it without margins. The algorithm is simple, iterate over the layers and find the "most left", "most top" etc. coordinate among them, then set it as the respective canvas coordinate. The excessive border would be cropped and any offscreen parts would be revealed. For now after putting the images next to each other I have to resize the canvas to some "big enough" size so that they all fit and then crop it down manually which is time consuming and boring.
Comment 1 Halla Rempt 2018-01-14 14:12:42 UTC
Please mark feature requests as "wish". We already have resize canvas to layer size, but that only checks the current layer. You could easily implement this in Python in Krita 4.
Comment 2 Isaac Zuniga 2018-08-19 07:49:18 UTC
(In reply to rkfg from comment #0)
> This is a great feature I miss from GIMP where it's called "Fit Canvas to
> Layers". It must be very easy to implement and it's good when creating
> collages or strips from multiple images. This function should set the canvas
> size so that all layers fit into it without margins. The algorithm is
> simple, iterate over the layers and find the "most left", "most top" etc.
> coordinate among them, then set it as the respective canvas coordinate. The
> excessive border would be cropped and any offscreen parts would be revealed.
> For now after putting the images next to each other I have to resize the
> canvas to some "big enough" size so that they all fit and then crop it down
> manually which is time consuming and boring.

rkfg, I found a feature in Krita that may be what you are looking for. Select the layer(s) that you want to have the cropping performed around, and go to:

Image --> Trim to current layer.

You can also perform the same command for your current selection. Alternatively, it seems you can also apply the command to the entire image, which will remove any wasted empty space.

Are any of these what you are looking for?
Comment 3 Halla Rempt 2018-08-24 18:31:05 UTC
Setting to needinfo
Comment 4 rkfg 2018-08-24 18:52:04 UTC
Sorry for not answering sooner. No, this is not what I want because it just trims the image to the curent layer as the name suggests. The main problem is to make the image bigger when a layer is offscreen, not to cut it down. An example: take two images, put them on two distinct layers and then shift the top layer down so that half of it beyond the image's lower border. Now, how do we make the image big enough to see both images? To be precise, to see the top layer in full and half of the bottom one. Except, of course, doing it manually by resizing the canvas to some deliberately bigger size and then cropping it so that all of it fits.
Comment 5 Andrew Crouthamel 2018-09-28 03:15:37 UTC
Dear Bug Submitter,

This bug has been in NEEDSINFO status with no change for at least 15 days. Please provide the requested information as soon as possible and set the bug status as REPORTED. Due to regular bug tracker maintenance, if the bug is still in NEEDSINFO status with no change in 30 days, the bug will be closed as RESOLVED > WORKSFORME due to lack of needed information.

For more information about our bug triaging procedures please read the wiki located here: https://community.kde.org/Guidelines_and_HOWTOs/Bug_triaging

If you have already provided the requested information, please set the bug status as REPORTED so that the KDE team knows that the bug is ready to be confirmed.

Thank you for helping us make KDE software even better for everyone!
Comment 6 rkfg 2018-09-28 05:49:23 UTC
Information provided.
Comment 7 Halla Rempt 2018-09-28 09:25:35 UTC
You could also use the Grow mode of the crop tool for this. In theory, a simple python script like 

from krita import *
d = Krita.instance().activeDocument()
w = d.width()
h = d.height()
x = d.xOffset()
y = d.yOffset()

print(x, y, w, h)

for n in d.topLevelNodes():
    print (n, n.bounds())
    b = n.bounds()
    if b.x() < x:
        x = b.x()
    if b.y() < y:
        y = b.y()
    if b.x() + b.width() > w:
        w = b.x() + b.width()
    if b.y() + b.height() > h:
        h = b.y() + b.height()
        
print (x, y, w, h)       

d.setXOffset(x)
d.setYOffset(y)
d.setWidth(w)
d.setHeight(h)

Should do the trick, but setting the image dimensions seems to be broken. Which makes this a bug...
Comment 8 Halla Rempt 2018-09-28 09:35:08 UTC
Ah, I don't know my own API. This script will do what you need (unless you have nested layers, then it needs to be rewritten a little):

from krita import *
d = Krita.instance().activeDocument()
w = d.width()
h = d.height()
x = d.xOffset()
y = d.yOffset()

print(x, y, w, h)
r = QRect(x, y, w, h)
print(r)
for n in d.topLevelNodes():
    print (n, n.bounds())
    b = n.bounds()
    r = r.united(b)
        
print (r) 

d.resizeImage(r.x(), r.y(), r.width(), r.height())
Comment 9 rkfg 2018-09-28 09:40:25 UTC
Thank you, it works. But I'd much prefer it to be available as a menu item, as I'm not so familiar with scripting. I used "Scripter" to test this one but it's quite inconvenient to load the script each time.
Comment 10 Halla Rempt 2018-09-28 10:24:00 UTC
Created attachment 115291 [details]
A plugin that adds the script to the menu as well.

Unpack this in .local/share (Linux) or in %APPDATA% and the script will be in the menu :-).
Comment 11 rkfg 2018-09-28 10:46:38 UTC
Hmm, this looks really great but I don't see this menu item in the "Image" menu. I'm on version 4.1.3, could it be that this python plugin support is not implemented in it?
Comment 12 rkfg 2018-09-28 10:49:38 UTC
To make it clear: I enabled the plugin in the settings, I can see its description but I don't see it in the menu.
Comment 13 Halla Rempt 2018-09-28 10:49:57 UTC
It's in tools/scripts. Placing menu entries somewhere else is something I'm working on.
Comment 14 rkfg 2018-09-28 10:51:19 UTC
Oh, I see. <Menu name="Image"> confused me. Now it's all green, thanks for your work!