new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Electron · API · all subjects

nativeimage/methods

25 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

nativeImage.createEmpty()

Creates an empty NativeImage instance. Returns NativeImage.

nativeImage.createThumbnailFromPath(path, size)

Creates a thumbnail from a file. Available on macOS and Windows. Parameters: path (string) - path to file, size (Size) - desired width and height (positive numbers). Returns Promise<NativeImage>. Note: Windows implementation ignores size.height and scales height according to size.width.

nativeImage.createFromPath(path)

Creates a new NativeImage instance from an image file (PNG or JPEG) at the specified path. Parameter: path (string). Returns NativeImage. Returns an empty image if the path does not exist, cannot be read, or is not a valid image.

nativeImage.createFromBuffer(buffer, options)

Creates a new NativeImage instance from a buffer, attempting to decode as PNG or JPEG first. Parameters: buffer (Buffer), options (Object, optional) with width (Integer, optional, required for bitmap buffers), height (Integer, optional, required for bitmap buffers), scaleFactor (Number, optional, defaults to 1.0). Returns NativeImage.

nativeImage.createFromDataURL(dataURL)

Creates a new NativeImage instance from a base 64 encoded Data URL string. Parameter: dataURL (string). Returns NativeImage.

nativeImage.createFromNamedImage(imageName, options)

Available on macOS. Creates a new NativeImage instance from an NSImage that maps to the given image name. Parameter: imageName (string). Options (optional): can be a number array (deprecated, interpreted as hslShift) or an object with hslShift (number[], optional), pointSize (Number, optional, defaults to 30.0), weight ('ultralight'|'thin'|'light'|'regular'|'medium'|'semibold'|'bold'|'heavy'|'black', optional, defaults to 'regular'), scale ('small'|'medium'|'large', optional, defaults to 'medium'). Returns NativeImage.

HSL shift values for createFromNamedImage

For nativeImage.createFromNamedImage hslShift array: hsl_shift[0] (hue) is the absolute hue value, 0 and 1 map to 0 and 360 on the hue color wheel (red). hsl_shift[1] (saturation): 0 = remove all color, 0.5 = leave unchanged, 1 = fully saturate. hsl_shift[2] (lightness): 0 = remove all lightness (black), 0.5 = leave unchanged, 1 = full lightness (white). Example: [-1, 0, 1] makes image completely white; [-1, 1, 0] makes image completely black.

nativeImage.createMenuSymbol(imageName)

Available on macOS. Creates a new NativeImage instance from an SF Symbol for use in a native Menu. Parameter: imageName (string). Returns NativeImage.

nativeImage.createMenuSymbol example

const { nativeImage, MenuItem } = require('electron') const item = new MenuItem({ icon: nativeImage.createMenuSymbol('folder.badge.plus'), label: 'Create Folder' })

image.toPNG(options)

Encodes the image to PNG format. Parameters: options (Object, optional) with scaleFactor (Number, optional, defaults to 1.0). Returns Buffer containing PNG encoded data.

image.toJPEG(quality)

Encodes the image to JPEG format. Parameter: quality (Integer, between 0-100). Returns Buffer containing JPEG encoded data.

image.toBitmap(options)

Returns a Buffer containing a copy of the image's raw bitmap pixel data. Parameters: options (Object, optional) with scaleFactor (Number, optional, defaults to 1.0), colorSpace (ColorSpace, optional, defaults to sRGB). Pass the image's original color space to preserve previous behavior, or another color space to get pixel values in that space. Pixel data is normalized to sRGB by default.

image.toDataURL(options)

Returns the Data URL of the image. Parameters: options (Object, optional) with scaleFactor (Number, optional, defaults to 1.0). Returns string. PNG colorspace is preserved.

image.getBitmap(options) deprecated

Deprecated method. Legacy alias for image.toBitmap(). Parameters: options (Object, optional) with scaleFactor (Number, optional, defaults to 1.0), colorSpace (ColorSpace, optional, defaults to sRGB).

image.getNativeHandle()

Available on macOS. Returns a Buffer storing a C pointer to the underlying native handle of the image. On macOS, returns a pointer to an NSImage instance. The returned pointer is a weak pointer to the underlying native image, not a copy, so the associated nativeImage instance must be kept around.

image.isEmpty()

Returns a boolean indicating whether the image is empty.

image.getSize(scaleFactor)

Returns the size of the image as a Size object. Parameter: scaleFactor (Number, optional, defaults to 1.0). If scaleFactor is passed, returns the size corresponding to the image representation most closely matching the passed value.

image.setTemplateImage(option)

Marks the image as a macOS template image. Parameter: option (boolean). This affects how the image is rendered on macOS.

image.isTemplateImage()

Returns a boolean indicating whether the image is a macOS template image.

image.crop(rect)

Crops the image to the specified area. Parameter: rect (Rectangle) - the area to crop. Returns NativeImage - the cropped image.

image.resize(options)

Resizes the image. Parameters: options Object with width (Integer, optional, defaults to image's width), height (Integer, optional, defaults to image's height), quality (string, optional, possible values: 'good', 'better', 'best', default is 'best'). Returns NativeImage. If only height or width is specified, the current aspect ratio is preserved.

image.getAspectRatio(scaleFactor)

Returns the image's aspect ratio as a Number (width divided by height). Parameter: scaleFactor (Number, optional, defaults to 1.0). If scaleFactor is passed, returns the aspect ratio corresponding to the image representation most closely matching the passed value.

image.getScaleFactors()

Returns an array of all scale factors (Number[]) corresponding to representations for a given NativeImage.

image.addRepresentation(options)

Adds an image representation for a specific scale factor. Parameters: options Object with scaleFactor (Number, optional), width (Integer, optional, defaults to 0, required if buffer is specified), height (Integer, optional, defaults to 0, required if buffer is specified), buffer (Buffer, optional, containing raw image data), dataURL (string, optional, containing base 64 encoded PNG or JPEG). This can be called on empty images to programmatically add different scale factor representations.

nativeImage with Tray and BrowserWindow example

const { BrowserWindow, nativeImage, Tray } = require('electron') const trayIcon = nativeImage.createFromPath('/Users/somebody/images/icon.png') const appIcon = nativeImage.createFromPath('/Users/somebody/images/window.png') const tray = new Tray(trayIcon) const win = new BrowserWindow({ icon: appIcon })

Give your agent this brain