Socket
Book a DemoInstallSign in
Socket

@larscom/image-data

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@larscom/image-data

create ImageData object from URL or ArrayBuffer without creating a canvas (using wasm)

0.2.0
latest
Source
npmnpm
Version published
Weekly downloads
176
-10.2%
Maintainers
1
Weekly downloads
 
Created
Source

@larscom/image-data

npm-version npm license

Easily construct an Image object from a URL or ArrayBuffer. An Image object contains the width and height of the image and an ImageData object as well as the channels (RGBA). It uses WASM to construct such an object.

Installation

npm install @larscom/image-data

Usage

Import the load_from_url function to construct an Image object.

import { load_from_url } from '@larscom/image-data'

const imageUrl = 'https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png'

const image = await load_from_url(imageUrl)

const canvas = document.getElementById('canvas')

canvas.width = image.width
canvas.height = image.height

const ctx = canvas.getContext('2d')

// use the ImageData object
ctx.putImageData(image.data, 0, 0)
interface Image {
  width: number
  height: number
  data: ImageData
  // RGBA
  channels: Uint8Array<ArrayBuffer>
}

Vite

When using a build tool such as vite you need additional configuration as it doesn't serve *.wasm files by default (see examples folder for a vite-react-ts project)

add exclude option to optimizeDeps to vite.config.ts

export default defineConfig({
  plugins: [react()],
  optimizeDeps: {
    exclude: ['@larscom/image-data']
  }
})

Colors (HEX)

Convert each individual pixel to a HEX color code.

import { load_from_url, convert_to_hex_colors } from '@larscom/image-data'

const imageUrl = 'https://example.com/image.png'
const image = await load_from_url(imageUrl)

const colors = convert_to_hex_colors(image.channels)

console.log(colors) // ["#FF5733", "#33C1FF", "#28A745", ...]

Colors (RGBA)

Decode the raw channel data into an array of pixels (RGBA)

import { load_from_url, decode_to_rgba } from '@larscom/image-data'

const imageUrl = 'https://example.com/image.png'
const image = await load_from_url(imageUrl)

const pixels = decode_to_rgba(image.channels)

console.log(pixels)
// [
//  { r: 255, g: 0,   b: 0,   a: 255 },
//  { r: 0,   g: 255, b: 0,   a: 128 },
//  { r: 0,   g: 0,   b: 255, a: 64  },
//  ...
// ]

Supported formats

FormatSupported
JPEG
PNG
BMP
DDS
FARBFELD
GIF
HDR
ICO
EXR
PNM
QOI
TGA
TIFF
WEBP
AVIF
SVG

Keywords

img

FAQs

Package last updated on 30 Aug 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.