Inkscape.org
Creating New Extensions brand new to inkscape extensions - how to get selected bitmap
  1. #1
    raphael75 raphael75 @raphael75
    *

    Hello, I am trying to write my 1st Inkscape extension. I have an Inkscape file with nothing in it but a bitmap that I pasted in, and all I want the extension to do is get the selected bitmap image and resize it to a specific size in inches.

    I looked at the tutorial, and I downloaded some other extensions that deal with bitmap images, but nothing I tried worked. Things I tried:

    img_elt, objs = self.get_selection()
    img_elt.style['width'] = '2.463'
    img_elt.style['height'] = '3.438'

     

    for id, node in self.selected.iteritems():
        node.set('width', '2.463')
        node.set('height', '3.438')
    for elem in self.selection:
        elem.style['width'] = '2.463'
        elem.style['height'] = '3.438'

    And none of these worked. I can't find the documentation for the "self" object and its properties. Any help is appreciated.

  2. #2
    inklinea inklinea @inklinea⛰️

    Style refers to css style properties.

    To set the height and width you would either need to set the height and width attributes or apply a transform.

    When adjusting the size of objects in an svg, normally you have to take account of preexising transforms on the object, or any of its parent groups.

    You also have to be aware of the units ( mm, inches, pixels ) of the document.

    For the simple example you asked for.

    ****

    If we assume that the document is in inches and there are no transforms on the object or its parent layer (or if the object is in the document root).

    selection_list = self.svg.selected
            if len(selection_list) < 1:
                inkex.errormsg('Please select at least one image')
                return
                
    for selection in selection_list:
        selection.set('width', 2)
        selection.set('height', 4)

        
    or better:

    Filter for images in the selection first:

    selection_list = self.svg.selected.filter(inkex.elements._image.Image)

    or in the processing loop:

    if selection.TAG == 'image':
        etc

  3. #3
    inklinea inklinea @inklinea⛰️

    I should add that resizing a bitmap in this way does not work very well.

    It very much depends upon the viewing software on how good it looks. 

    I may look fine in a web browser, but in other viewers maybe not.

    It will probably have anomalies / artifacts in Inkscape. 

    If you want to truly scale a bitmap in Inkscape, the extension system comes with PIL ( Pillow ) the python image processing library. 

    It's not hard to open the image in PIL, resize then return an image object. However it will change the filesize.

    If the original image was jpeg and you resample to png ( which Inkscape does anyway once you edit in the gui ) the file could get quite large.

     

  4. #4
    raphael75 raphael75 @raphael75

    Thanks for your response! I tried the code you posted and it almost worked. The wierd thing I found was my document is in inches, but when I ran the extension it changed the document to use mm and resized the bitmap to millimeters. So it was 2.463mm x 3.438mm. I don't understand what in that code could possibly be causing the page to change units.

Inkscape Inkscape.org Inkscape Forum Creating New Extensions brand new to inkscape extensions - how to get selected bitmap