From 6eabd226630557bfa573f319355f39bab6cfa369 Mon Sep 17 00:00:00 2001 From: Mario Voigt Date: Tue, 23 Jan 2024 23:52:40 +0100 Subject: [PATCH] clarify DPI stuff --- .../fablabchemnitz/laser_check/laser_check.py | 76 +++++++++++-------- 1 file changed, 46 insertions(+), 30 deletions(-) diff --git a/extensions/fablabchemnitz/laser_check/laser_check.py b/extensions/fablabchemnitz/laser_check/laser_check.py index de7fd34..26308df 100644 --- a/extensions/fablabchemnitz/laser_check/laser_check.py +++ b/extensions/fablabchemnitz/laser_check/laser_check.py @@ -508,9 +508,7 @@ class LaserCheck(inkex.EffectExtension): maxDPIhits = [] minDPIhits = [] - for image in images: - inkex.utils.debug("image id={}".format(image.get('id'))) - + for image in images: self.path = self.checkImagePath(image) # This also ensures the file exists if self.path is None: # check if image is embedded or linked image_string = image.get('{http://www.w3.org/1999/xlink}href') @@ -524,38 +522,56 @@ class LaserCheck(inkex.EffectExtension): else: img = Image.open(self.path) - if img: - img_w = img.getbbox()[2] - img_h = img.getbbox()[3] - if image.get('width') is None: - img_svg_w = self.svg.unittouu(str(img_w) + "px") - else: - img_svg_w = self.svg.unittouu(str(float(image.get('width'))) + "px") - if image.get('height') is None: - img_svg_h = self.svg.unittouu(str(img_h) + "px") - else: - img_svg_h = self.svg.unittouu(str(float(image.get('height'))) + "px") - imgScaleX = img_svg_w / img_w - imgScaleY = img_svg_h / img_h - dpiX = self.svg.unittouu(str(img_w) + "in") / img_svg_w - dpiY = self.svg.unittouu(str(img_h) + "in") / img_svg_h + img_w = img.getbbox()[2] + img_h = img.getbbox()[3] + if image.get('width') is None: + img_svg_w = self.svg.unittouu(str(img_w) + "px") * inkscapeScale + else: + img_svg_w = self.svg.unittouu(str(float(image.get('width'))) + "px") * inkscapeScale + if image.get('height') is None: + img_svg_h = self.svg.unittouu(str(img_h) + "px") * inkscapeScale + else: + img_svg_h = self.svg.unittouu(str(float(image.get('height'))) + "px") * inkscapeScale + imgScaleX = img_svg_w / img_w + imgScaleY = img_svg_h / img_h + dpiX = self.svg.unittouu(str(img_w) + "in") / img_svg_w + dpiY = self.svg.unittouu(str(img_h) + "in") / img_svg_h + + if round(dpiX, 0) < so.min_image_dpi or round(dpiY, 0) < so.min_image_dpi: + minDPIhits.append([image, dpiX, dpiY, img_svg_w, img_svg_h]) + if round(dpiX, 0) > so.max_image_dpi or round(dpiY, 0) > so.max_image_dpi: + maxDPIhits.append([image, dpiX, dpiY, img_svg_w, img_svg_h]) + + uniform = False + if round(imgScaleX, 3) == round(imgScaleY, 3): + uniform = True + else: + malformedScales.append([image, imgScaleX, imgScaleY]) - if round(dpiX, 0) < so.min_image_dpi or round(dpiY, 0) < so.min_image_dpi: - minDPIhits.append([image, dpiY, dpiX]) - if round(dpiX, 0) > so.max_image_dpi or round(dpiY, 0) > so.max_image_dpi: - maxDPIhits.append([image, dpiY, dpiX]) - - uniform = False - if round(imgScaleX, 3) == round(imgScaleY, 3): - uniform = True - else: - malformedScales.append([image, imgScaleX, imgScaleY]) + inkex.utils.debug("image id={} - internal size {}x{}px - scaled to {:0.0f}x{:0.0f}px - DPI X{:0.0f}+Y{:0.0f}".format(image.get('id'), img_w, img_h, img_svg_w, img_svg_h, dpiX, dpiY)) + if len(minDPIhits) > 0: 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}. "\ + "Resize to {:0.0f}x{:0.0f}px to fit".format( + minDPIhit[0].get('id'), + minDPIhit[1], + minDPIhit[2], + so.min_image_dpi, + minDPIhit[3] * (minDPIhit[1] / so.min_image_dpi), + minDPIhit[4] * (minDPIhit[2] / so.min_image_dpi), + )) if len(maxDPIhits) > 0: for maxDPIhit in maxDPIhits: - inkex.utils.debug("Image {} has DPI X{:0.0f}+Y{:0.0f} > max. {:0.0f}".format(maxDPIhit[0].get('id'), maxDPIhit[1], maxDPIhit[2],so.max_image_dpi)) + inkex.utils.debug("Image {} has DPI X{:0.0f}+Y{:0.0f} > max. {:0.0f}. "\ + "Resize to {:0.0f}x{:0.0f}px to fit".format( + maxDPIhit[0].get('id'), + maxDPIhit[1], + maxDPIhit[2], + so.max_image_dpi, + maxDPIhit[3] * (maxDPIhit[1] / so.max_image_dpi), + maxDPIhit[4] * (maxDPIhit[2] / so.max_image_dpi), + )) if len(malformedScales) > 0: for malformedScale in malformedScales: inkex.utils.debug("Image {} has non-uniform scale X = {:0.3f}, Y = {:0.3f}".format(malformedScale[0].get('id'), malformedScale[1], malformedScale[2]))