bnanes / svg-embed-and-crop

Inkscape Extension: Embed and Crop Images
MIT License
4 stars 2 forks source link

bug in path handling #2

Open vmario89 opened 1 year ago

vmario89 commented 1 year ago

Hi, finding this extension useful. But i got some bug with the path handling on Fedora 37. If i make a inkscape file with linked image, regularly the tmp file is generated and svg-embed-and-crop tries to run on the file, but it fails because it wrongly formats the path of the linked image.

Caused by: edu.emory.cellbio.svg.EmbedAndCropException: Plugin error: Can't read file link: home/tomate/Downloads/clipout_error.jpg

but it should be "/home/tomate/Downloads/clipout_error.jpg". I dont know why it is parsing to "home/tomate/Downloads/clipout_error.jpg"

the command is: java -cp "/home/tomate/.config/inkscape/extensions/svg_embed_and_crop/*" "edu.emory.cellbio.svg.EmbedAndCropInkscapeEntry" "/tmp/ink_ext_XXXXXX.svgJSY7V1" -o "/tmp/ink_ext_XXXXXX.svgJSY7V1.cropped"

if i run svg-embed-and-crop not within inkscape, but with regular bash cli in the same directory, where the saved svg is, everything is fine.

bnanes commented 1 year ago

Thank you for the bug report, and sorry for the delayed response. My understanding from your report is that the bug occurs when calling the plugin from Inkscape, but does not occur when calling the program from the shell, is that correct? Which version of Inkscape are you using?

bnanes commented 1 year ago

Also, can you please post a snippet from the SVG file including the relevant image tag? Should look something like this:

    <image
       clip-path="url(#clipPath17749)"
       transform="matrix(0.4984632,0.03917196,-0.03917196,0.4984632,101.50991,40.405525)"
       style="stroke-width:2.2883296"
       width="184.99667"
       height="138.7475"
       preserveAspectRatio="none"
       id="image1242"
       x="-1.5298071"
       y="-5.8591886"
       xlink:href="/path/to/image.png"
       sodipodi:absref="/path/to/image.png" />
vmario89 commented 1 year ago

Hi, sorry for weeks of latency. Made some example screenshots:

some image: grafik

importing into inkscape v.1.2.2: grafik grafik grafik

running the extension (non-saved file yet): the absolute file location is /home/himbeere/Downloads/Auswahl_001.png grafik

now saving as Auswahl_001.svg:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->

<svg
   version="1.1"
   id="svg2"
   width="1043"
   height="592"
   viewBox="0 0 1043 592"
   sodipodi:docname="Auswahl_001.svg"
   inkscape:version="1.2.2 (b0a8486541, 2022-12-01)"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns:xlink="http://www.w3.org/1999/xlink"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:svg="http://www.w3.org/2000/svg">
  <defs
     id="defs6" />
  <sodipodi:namedview
     id="namedview4"
     pagecolor="#ffffff"
     bordercolor="#666666"
     borderopacity="1.0"
     inkscape:showpageshadow="2"
     inkscape:pageopacity="0.0"
     inkscape:pagecheckerboard="0"
     inkscape:deskcolor="#d1d1d1"
     showgrid="false"
     inkscape:zoom="1.2991371"
     inkscape:cx="390.25904"
     inkscape:cy="296.73542"
     inkscape:window-width="1920"
     inkscape:window-height="1024"
     inkscape:window-x="0"
     inkscape:window-y="0"
     inkscape:window-maximized="1"
     inkscape:current-layer="g8" />
  <g
     inkscape:groupmode="layer"
     inkscape:label="Image"
     id="g8">
    <image
       width="1043"
       height="592"
       preserveAspectRatio="none"
       xlink:href="Auswahl_001.png"
       id="image10" />
  </g>
</svg>

modifying xlink:href does not change to fix it: grafik

vmario89 commented 1 year ago

my extension files look alike:


import inkex
import subprocess
import os
from lxml import etree
from inkex import command

class EmbedAndCrop(inkex.EffectExtension):

    '''
    This extension does not work for embedded images, but only for linked ones
    '''

    def effect(self):

        cp = os.path.dirname(os.path.abspath(__file__)) + "/svg_embed_and_crop/*"
        output_file = self.options.input_file + ".cropped"

        cmd = 'java -cp "' + cp + '" "edu.emory.cellbio.svg.EmbedAndCropInkscapeEntry" "' + self.options.input_file + '" -o "' + output_file + '"'
        with subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc:
            proc.wait()
            stdout, stderr = proc.communicate()
            if stderr.decode('utf-8') != "":
                inkex.utils.debug(stderr.decode('utf-8'))
        #cli_output = command.call('java', '-cp', cp, 'edu.emory.cellbio.svg.EmbedAndCropInkscapeEntry', self.options.input_file, "-o", output_file)
        #if len(cli_output) > 0:
        #   self.debug(_("Inkscape extension returned the following output:"))
        #   self.debug(cli_output)

        if not os.path.exists(output_file):
            raise inkex.AbortExtension("Plugin cancelled")
        stream = open(output_file, 'r')
        p = etree.XMLParser(huge_tree=True)
        doc = etree.parse(stream, parser=etree.XMLParser(huge_tree=True))
        stream.close()
        root = self.document.getroot()
        kept = [] #required. if we delete them directly without adding new defs or namedview, inkscape will crash
        for node in self.document.xpath('//*', namespaces=inkex.NSS):
            if node.TAG not in ('svg', 'defs', 'namedview'):
                node.delete()
            elif node.TAG in ('defs', 'namedview'): #except 'svg'
                kept.append(node)

        children = doc.getroot().getchildren()
        for child in children:
           root.append(child)
        for k in kept:
            k.delete()

if __name__ == '__main__':
    EmbedAndCrop().run()
<?xml version="1.0" encoding="UTF-8"?>
<inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
    <name>SVG Embed And Crop Linked Images</name>
    <id>svg_embed_and_crop</id>
    <effect>
        <object-type>all</object-type>
        <effects-menu>
            <submenu name="Extensions">
                <submenu name="Tracing/Images/Edge Detection" />
            </submenu>
        </effects-menu>
    </effect>
    <script>
        <command location="inx" interpreter="python">svg_embed_and_crop.py</command>
    </script>
</inkscape-extension>
bnanes commented 1 year ago

Ok, it looks like there was a bug handling image file paths specified as URIs. Changing behavior running as an extension vs from the command line may reflect a change in the image path format when the SVG file is saved. Here is a tentative fix.

vmario89 commented 1 year ago

Hi. thanks for that commit! now it seems to work, but i have a few other issues:

bnanes commented 1 year ago