Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Gustavo Flores
Gustavo Flores

Posted on

     

Convert an Image to HTML pixel by pixel

Hi!
This is a tutorial to convert an image to HTML pixel by pixel, with this you can analyze your image :)

first, you need some HTML

<!-- canvas need to be hidden, here you will load your image and take information about it --><canvasid="canvas"style="visibility: hidden;"></canvas><!-- here you will print your matrix of span tags, each of them with a single color --><divid="final-image"style="line-height: 7px;letter-spacing: -3px;"></div>
Enter fullscreen modeExit fullscreen mode

now some javascript

constcanvas=document.getElementById("canvas");constctx=canvas.getContext("2d");constimgUrl="https://images.unsplash.com/photo-1549740425-5e9ed4d8cd34?ixid=MXwxMjA3fDB8MHxjb2xsZWN0aW9uLXBhZ2V8MXwzOTU0NTB8fGVufDB8fHw%3D&ixlib=rb-1.2.1&w=1000&q=80";constimg=newImage();constimgWidth=100;lethtml="";img.crossOrigin="Anonymous";img.onload=function(){canvas.width=imgWidth;canvas.height=(this.height*canvas.width)/this.width;ctx.drawImage(img,0,0,canvas.width,canvas.height);for(leti=0;i<canvas.height;i++){for(letj=0;j<canvas.width;j++){add(ctx.getImageData(j,i,1,1).data);}html+="<br />";}document.getElementById("final-image").innerHTML=html;canvas.parentNode.removeChild(canvas);};img.src=imgUrl;functionadd(c){html+=`<span>&#9632;</span>`;}
Enter fullscreen modeExit fullscreen mode

with this code, you can print your image on text

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Some comments may only be visible to logged-in visitors.Sign in to view all comments.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Software Developer, sometimes Frontend, sometimes Backend
  • Location
    Chile
  • Joined

More fromGustavo Flores

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp