Public
Edited
Oct 20, 2023
Insert cell
image = {
const img = new Image();
img.crossOrigin = "*";
FileAttachment("iStock_000045742478_Med.jpg").image().then((i) => {
img.src = i.src;
});
return img;
}
Insert cell
Insert cell
Insert cell
{
var targetCanvas = document.createElement("canvas");
targetCanvas.width = width;
targetCanvas.height = height;
var target = targetCanvas.getContext('2d');

target.drawImage(image, 0, 0, width, height);
var imageData = target.getImageData(0, 0, width, height);

return html`<pre style="line-height: 0.7">${
d3.range(height).map(
y => d3.range(width).map(
x => colorToHTML(pixelFromImageData(imageData, x, y))
).join('')
).join('\n')
}</pre>`;
}
Insert cell
colorToHTML = ([r, g, b]) => {
return `<span style="color: rgb(${r}, ${g}, ${b})">${randomChar()}</span>`
}
Insert cell
randomChar = () => {
var chars = '#W0M8@B';
return chars[Math.floor(Math.random()*chars.length)];
}
Insert cell
pixelFromImageData = (imageData, x, y) => imageData.data.slice(
(y * imageData.width + x) * 4,
(y * imageData.width + x) * 4 + 3
)
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more