add xlink fix in laser_check

This commit is contained in:
Mario Voigt 2024-01-23 21:45:54 +01:00
parent f9c2be0270
commit 66ed980951

View File

@ -13,6 +13,7 @@ from collections import Counter
from PIL import Image from PIL import Image
from io import BytesIO from io import BytesIO
import base64 import base64
import urllib.request as urllib
class LaserCheck(inkex.EffectExtension): class LaserCheck(inkex.EffectExtension):
@ -35,6 +36,30 @@ class LaserCheck(inkex.EffectExtension):
- this code is horrible ugly stuff - this code is horrible ugly stuff
- output time/cost estimations per stroke color - output time/cost estimations per stroke color
''' '''
def checkImagePath(self, node):
"""Embed the data of the selected Image Tag element"""
xlink = node.get('xlink:href')
if xlink and xlink[:5] == 'data:':
# No need, data alread embedded
return
url = urllib.urlparse(xlink)
href = urllib.url2pathname(url.path)
# Primary location always the filename itself.
path = self.absolute_href(href or '')
# Backup directory where we can find the image
if not os.path.isfile(path):
path = node.get('sodipodi:absref', path)
if not os.path.isfile(path):
inkex.errormsg('File not found "{}". Unable to embed image.').format(path)
return
if (os.path.isfile(path)):
return path
def add_arguments(self, pars): def add_arguments(self, pars):
pars.add_argument('--tab') pars.add_argument('--tab')
@ -482,42 +507,49 @@ class LaserCheck(inkex.EffectExtension):
malformedScales = [] malformedScales = []
maxDPIhits = [] maxDPIhits = []
minDPIhits = [] minDPIhits = []
for image in images: for image in images:
inkex.utils.debug("image id={}".format(image.get('id'))) inkex.utils.debug("image id={}".format(image.get('id')))
image_string = image.get('{http://www.w3.org/1999/xlink}href') self.path = self.checkImagePath(image) # This also ensures the file exists
# find comma position if self.path is None: # check if image is embedded or linked
i = 0 image_string = node.get('{http://www.w3.org/1999/xlink}href')
while i < 40: # find comma position
if image_string[i] == ',': i = 0
break while i < 40:
i = i + 1 if image_string[i] == ',':
img = Image.open(BytesIO(base64.b64decode(image_string[i + 1:len(image_string)]))) break
img_w = img.getbbox()[2] i = i + 1
img_h = img.getbbox()[3] img = Image.open(BytesIO(base64.b64decode(image_string[i + 1:len(image_string)])))
if image.get('width') is None:
img_svg_w = self.svg.unittouu(str(img_w) + "px")
else: else:
img_svg_w = float(image.get('width')) * inkscapeScale img = Image.open(self.path)
if image.get('height') is None:
img_svg_h = self.svg.unittouu(str(img_h) + "px") if img:
else: img_w = img.getbbox()[2]
img_svg_h = float(image.get('height')) * inkscapeScale img_h = img.getbbox()[3]
imgScaleX = img_svg_w / img_w if image.get('width') is None:
imgScaleY = img_svg_h / img_h img_svg_w = self.svg.unittouu(str(img_w) + "px")
dpiX = self.svg.unittouu(str(img_w) + "in") / img_svg_w else:
dpiY = self.svg.unittouu(str(img_h) + "in") / img_svg_h img_svg_w = float(image.get('width')) * inkscapeScale
if image.get('height') is None:
if round(dpiX, 0) < so.min_image_dpi or round(dpiY, 0) < so.min_image_dpi: img_svg_h = self.svg.unittouu(str(img_h) + "px")
minDPIhits.append([element, dpiY, dpiX]) else:
if round(dpiX, 0) > so.max_image_dpi or round(dpiY, 0) > so.max_image_dpi: img_svg_h = float(image.get('height')) * inkscapeScale
maxDPIhits.append([element, dpiY, dpiX]) imgScaleX = img_svg_w / img_w
imgScaleY = img_svg_h / img_h
uniform = False dpiX = self.svg.unittouu(str(img_w) + "in") / img_svg_w
if round(imgScaleX, 3) == round(imgScaleY, 3): dpiY = self.svg.unittouu(str(img_h) + "in") / img_svg_h
uniform = True
else: if round(dpiX, 0) < so.min_image_dpi or round(dpiY, 0) < so.min_image_dpi:
malformedScales.append([element, imgScaleX, imgScaleY]) minDPIhits.append([element, dpiY, dpiX])
if round(dpiX, 0) > so.max_image_dpi or round(dpiY, 0) > so.max_image_dpi:
maxDPIhits.append([element, dpiY, dpiX])
uniform = False
if round(imgScaleX, 3) == round(imgScaleY, 3):
uniform = True
else:
malformedScales.append([element, imgScaleX, imgScaleY])
if len(minDPIhits) > 0: if len(minDPIhits) > 0:
for minDPIhit in minDPIhits: for minDPIhit in minDPIhits:
inkex.utils.debug("Image {} has DPI X{:0.0f}+Y{:0.0f} < min. {:0.0f}".format(minDPIhit[0].get('id'), minDPIhit[1], minDPIhit[2], so.min_image_dpi)) inkex.utils.debug("Image {} has DPI X{:0.0f}+Y{:0.0f} < min. {:0.0f}".format(minDPIhit[0].get('id'), minDPIhit[1], minDPIhit[2], so.min_image_dpi))