Request Crop images

Bug #517082 reported by Engel Komies on 2010-02-04
This bug affects 4 people
Affects Status Importance Assigned to Milestone

Bug Description


I've got a request for a new feature in the next Inkcape version. I really miss a crop-tool for imported images. Could this tool be found in a next version please? Would be very nice!


E. Komies

su_v (suv-lp) on 2010-02-04
tags: added: bitmap
Changed in inkscape:
importance: Undecided → Wishlist
su_v (suv-lp) wrote :

RFEs related to cropping bitmap images in Inkscape:

Bug #517082 “Request Crop images” (New)
Bug #479864 “image cutting tool like openoffice Draw” (New)
Bug #248199 “some image editing tools for webdesign” (Invalid)
Bug #172166 “PLEASE INKSCAPE ADD CROP” (Invalid)
Bug #171598 “Crop bitmaps” (Invalid)
Bug #171427 “Crop bitmaps” (Invalid)
Bug #171299 “adjust brightness, contrast and crop images in Inkscape” (Invalid)
Bug #170839 “crop image” (Invalid)
Bug #170514 “Inplace cropping of images” (Invalid)
Bug #170291 “Image Cropping” (Invalid)

I'm not sure if we can mark them all as duplicate (e.g. of 170291) because over the time
- features have been added and improved:
   clipping&masking, editing clippaths&masks, edit bitmap externally
- importing of bitmaps has changed (from <image> to 'as pattern' back to <image> iiuc)

@JazzyNico - what would you suggest?

jazzynico (jazzynico) wrote :

I've marked all the crop reports duplicate of #170291, except the newer ones.
Now that Inkscape has ImageMagick effects and that ImageMagick supports cropping, why not implementing a cropping extension (in Extensions>Bitmap)?.

jazzynico (jazzynico) on 2010-04-02
Changed in inkscape:
assignee: nobody → JazzyNico (jazzynico)
status: New → In Progress
jazzynico (jazzynico) wrote :

Here is a first try, using the bitmap extension system.
It crops a bitmap (or a selection of bitmaps) according to top, bottom, left and right values (in px).

The only drawback is that the bitmap is resized, but not its SVG element. Thus it works well if the height and width SVG attributes are not set. If they are, the image is cropped, but keeps its original size on canvas.

I see no way to resize the SVG element from the extension, except changing things in extension/internal/bitmap/imagemagick.cpp.

jazzynico (jazzynico) wrote :

Jon, any idea or comment?

jazzynico (jazzynico) wrote :

Oops, sorry Jon, I wanted to ask Ted...

Yeah, it wasn't really designed for that. What you could do is overload
the effect() function. Do the SVG tweeking, and then call the super
class's effect() function to do the image manipulation.

jazzynico (jazzynico) wrote :

Thanks for the tip, Ted.

I've tried your suggestion, and I now have a new overloaded effect() function. The problem is that I need a Magick::Image to get the image size and it's Inkscape::XML::Node in order to tweak the height and size (which is not necessarily exactly the bitmap size, e.g. if the svg object is resized with the object handles). These elements are easily available from an ImageMagickDocCache, but I see no way to use or create one from the overloaded function.

An alternative solution consists in adding the node to the applyEffect function (could be interesting if we add other resizing effects), but it implies that we change all the existing bitmap extensions.

The solution may be obvious, but I'm not a C++ expert, and my last course is far in the past (I'm trying to understand Inkscape's code and learn C++ at the same time...).

Ted Gould (ted) wrote :

What I'd recommend is using a member variable on your class. So then
you just save the node until you need it. So something like this:

    -> Sets MyClass::Working Node
    -> Calls ImageMagic::Effect()
    -> Clears MyClass::Working Node
    -> Does IM magic.
    -> Calls MyClass::applyEffect()
    -> Cleans up IM stuff
    -> Uses MyClass::Working Node
    -> Adjusts bit map
    -> Adjusts node

Does that make sense?

jazzynico (jazzynico) wrote :

> Does that make sense?

Yes, thanks. But I wonder if we really need all that. All the bitmap work is done in Crop::applyEffect, and I could also scale the object directly in the ImageMagic::Effect loop. Here's what I've started to do (far from being finished...):

    // Image size before effect
    unsigned int cols_before = effectedImage.columns();
    unsigned int rows_before = effectedImage.rows();


    float cols_ratio = (float)rows_before / effectedImage.columns();
    float rows_ratio = (float)cols_before / effectedImage.rows();

    // The effect changed the image size, let's change the SVG height and width
    if (cols_ratio != 1 and rows_ratio != 1) {
        printf("Image %i: height reduced by %f", i, rows_ratio);
        printf(", width reduced by %f\n", cols_ratio);

   // From transformation.cpp
   SPItem *item = SP_ITEM(l->data);
   Geom::Scale scale (0,0);
   double new_width = scaleX;
   if (fabs(new_width) < 1e-6) new_width = 1e-6;
   double new_height = scaleY;
   if (fabs(new_height) < 1e-6) new_height = 1e-6;
   scale = Geom::Scale(new_width / 100.0, new_height / 100.0);
   sp_item_scale_rel (item, scale);


jazzynico (jazzynico) wrote :

Now I have to find a way to use sp_item_scale_rel inside ImageMagick::Effect. I think it is safer than playing with the node attributes directly, since height and width are chars and we never know what we could find in it (value, value + unit?).
I've started to copy a part of transformation.cpp which is not far from what I'm looking for. But I need to investigate more and understand how sp_items and all the related stuff work first...

Ted Gould (ted) wrote :

On Wed, 2010-04-07 at 18:27 +0000, JazzyNico wrote:
> Yes, thanks. But I wonder if we really need all that. All the bitmap
> work is done in Crop::applyEffect, and I could also scale the object
> directly in the ImageMagic::Effect loop. Here's what I've started to do
> (far from being finished...):

Yeah, that works too. Always lots of choices in programming :)

jazzynico (jazzynico) wrote :

> Yeah, that works too. Always lots of choices in programming :)

Yes, but not always simple and elegant. I'm not in the hurry, I'll try to do both :)

jazzynico (jazzynico) on 2010-05-05
Changed in inkscape:
milestone: none → 0.49
jazzynico (jazzynico) wrote :

Back to this feature with a new patch.

I've added a postEffect method that can be called after the effect is applied. Its goal is to modify the item (an not the image) whenever it is needed (eg if the image element height and width needs to be modified).

Note: the crop dimensions are given in bitmap pixels and thus may differ from the canvas image dimension.

Please test and comment!

jazzynico (jazzynico) wrote :

Available in the trunk as of revision 10554.

Changed in inkscape:
status: In Progress → Fix Committed
jazzynico (jazzynico) on 2015-02-14
Changed in inkscape:
status: Fix Committed → Fix Released
bemyself (chxp-moon) wrote :

Does the solution solve the problem that true crop in Inkscape?

I havenot see something about this in tuturiol.

Anyway, A true crop function is much help to reduce pdf format size when mix with bitmap and vector pictures, as the wishlist shows.

Qantas94Heavy (qantas94heavy) wrote :

@bemyself: this is the Raster -> Crop extension I believe.

To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Related questions