摆脱Lt; Canvas>用.tif扩展在图像上显示多边形

发布于 2025-02-11 04:30:19 字数 1392 浏览 3 评论 0 原文

我正在使用D3 SVG在图像上显示多边形,如下所示,在JavaScript代码中,我正在创建IMG标记元素,并将其附加到“模板”并将SVG附加到'Template'中,以便多边形出现在图像上。

        var template = document.createElement('div');
         var img = document.createElement('img');
             img.setAttribute("src", imagesource);
             template.appendChild(img);
       var bb = d3.select(template).append("svg")
                        .attr("width",width)
                        .attr("height",height)
                        .attr("id", "polygons");

...

    <img src="TSU/images/21021.jpg">
        <svg width="1920" height="1080" id="polygons">
          <g><rect x="1296 " y="229 " height="231" width="250" stroke="red"></rect>
      </g>
       </svg>

...

但是我有一组带有.tif扩展名的图像,我正在使用seikichi/tiff库,但是如下所示,图像附加到帆布元素上,这就是显示图像的方式,

但上面提到的我需要“ img”标签而不是canvas',因为d3不使用帆布元素,它需要“ img”才能在图像之上附加svg,

这就是为什么显示图像但显示了多边形的原因。图的顶部。我还试图操纵.tocanvas()与SVG一起工作,但仍未在图像顶部显示多边形。

            var xhr = new XMLHttpRequest();
            xhr.responseType = 'arraybuffer';
            xhr.open('GET', imagesource);
            xhr.onload = function (e) {
           var tiff = new Tiff({buffer: xhr.response});
           var canvas = tiff.toCanvas();
           template.append(canvas)

           };
          xhr.send();

I am working with displaying polygons on an image using d3 svg and as shown below in the javascript code i am creating img tag element and appending it to 'template'and then appending svg to 'template' so that polygons appear on the image.

        var template = document.createElement('div');
         var img = document.createElement('img');
             img.setAttribute("src", imagesource);
             template.appendChild(img);
       var bb = d3.select(template).append("svg")
                        .attr("width",width)
                        .attr("height",height)
                        .attr("id", "polygons");

...

    <img src="TSU/images/21021.jpg">
        <svg width="1920" height="1080" id="polygons">
          <g><rect x="1296 " y="229 " height="231" width="250" stroke="red"></rect>
      </g>
       </svg>

...

But I have a set of images with .tif extension and I am using seikichi/tiff Library but as shown below in the code the image is appended to an canvas element and that's how the image is displayed,

but as mentioned above I need 'img' tag not 'canvas' cause D3 doesnt use the canvas element,it needs the 'img' to be able to append svg on top of the image,

and thats why the images are displayed but polygons arent displayed on top of the image.I also tried to manipulate the.toCanvas() to work with svg but it is still not displaying the polygons on top of the image.

            var xhr = new XMLHttpRequest();
            xhr.responseType = 'arraybuffer';
            xhr.open('GET', imagesource);
            xhr.onload = function (e) {
           var tiff = new Tiff({buffer: xhr.response});
           var canvas = tiff.toCanvas();
           template.append(canvas)

           };
          xhr.send();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独﹏钓一江月 2025-02-18 04:30:19

解析 tiff 后,您需要运行D3功能。
实际上,您是否使用&lt; img&gt; &lt; canvas&gt; ,因为您的脚本只是附加了SVG。

换句话说:D3不会更改/重新绘制实际图像。

但是,您可以轻松地将生成的tiff-canvas转换为并将其用作&lt; img&gt; 元素中的 src 属性。

let imgSrc = 'test.tif';
var xhr = new XMLHttpRequest();
xhr.responseType = 'arraybuffer';
xhr.open('GET', imgSrc);
xhr.onload = function (e) {
    var tiff = new Tiff({
        buffer: xhr.response
    });
    var canvas = tiff.toCanvas();
    var dataURL = canvas.toDataURL();
    var template = document.createElement('div');
    document.body.append(template);

    var img = document.createElement('img');
    img.setAttribute("src", dataURL);
    template.appendChild(img);

    var width = canvas.width;
    var height = canvas.height;

    var bb = d3.select(template).append("svg")
        .attr("width", width)
        .attr("height", height)
        .attr("id", "polygons")
        .append('rect')
        .attr("x", "50%")
        .attr("y", "50%")
        .attr("height", "25%")
        .attr("width", "25%")
        .attr("fill", "red");

};
xhr.send();

桌面批处理
由于tiff.js是一个相当复杂的库(1.5 MB),而浏览器并未对TIFF文件进行本机的支持,因此您应该使用台式机应用程序考虑将tiffs的批量转换

不乏免费转换器,例如(例如 gimp )。

这样,您将获得明显较小的文件以及更好的页面/应用程序性能。

You need to run your d3 function after the tiff was parsed.
Actually it shouldn't really matter if you're using an <img> or a <canvas> since your script is just appending an svg.
In other words: d3 doesn't change/redraw the actual image.

However you could easily convert the generated tiff-canvas to a data-URL and use it as src property in your <img> element.

let imgSrc = 'test.tif';
var xhr = new XMLHttpRequest();
xhr.responseType = 'arraybuffer';
xhr.open('GET', imgSrc);
xhr.onload = function (e) {
    var tiff = new Tiff({
        buffer: xhr.response
    });
    var canvas = tiff.toCanvas();
    var dataURL = canvas.toDataURL();
    var template = document.createElement('div');
    document.body.append(template);

    var img = document.createElement('img');
    img.setAttribute("src", dataURL);
    template.appendChild(img);

    var width = canvas.width;
    var height = canvas.height;

    var bb = d3.select(template).append("svg")
        .attr("width", width)
        .attr("height", height)
        .attr("id", "polygons")
        .append('rect')
        .attr("x", "50%")
        .attr("y", "50%")
        .attr("height", "25%")
        .attr("width", "25%")
        .attr("fill", "red");

};
xhr.send();

Desktop batch onversion
As tiff.js is a rather complex library (1.5 MB) and tiff files are not natively supported by browsers, you should consider a batch conversion of your tiffs to jpg or png using a desktop application.

There's no shortage of free converters like (e.g gimp).

This way you get significantly smaller files as well as a better page/app performance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文