Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an image to grayscale in react native?

Tags:

react-native

Is there a way to manipulate an image in react native so it appears as grayscale? I want to try something like: filter: grayscale(1)

Thanks.

like image 540
eyal83 Avatar asked Sep 16 '15 10:09

eyal83


People also ask

How do I convert an image to grayscale?

Right-click the picture that you want to change, and then click Format Picture on the shortcut menu. Click the Picture tab. Under Image control, in the Color list, click Grayscale or Black and White.

What is tint color in React Native?

tintColor ​Changes the color of all the non-transparent pixels to the tintColor.


2 Answers

Working on a previously deleted answer, this is possible by having an image, with the other image positioned absolute over the top.

The "back" image is using tintColor, as defined here https://facebook.github.io/react-native/docs/image.html, this will turn all pixels that aren't transparent into that color.

The "front" image gets an opacity added to it, which brings the "back" color forward, leaving you with a "greyscale" image

Before:

<React.Fragment>    <Image source={<something>} /> </React.Fragment> 

Standard image

After (With a backing image):

<React.Fragment>    <Image source={<something>} style={{ tintColor: 'gray' }} />    <Image source={<something>} style={{ position: 'absolute', opacity: 0.3}} /> </React.Fragment> 

enter image description here

'red' as tintColor:

enter image description here

like image 153
FabianCook Avatar answered Oct 14 '22 07:10

FabianCook


You can use my react-native-color-matrix-image-filters library:

import { Image } from 'react-native'; import { Grayscale } from 'react-native-color-matrix-image-filters';  const GrayscaledImage = (imageProps) => (     <Grayscale>         <Image {...imageProps} />     </Grayscale> ); 
like image 41
iyegoroff Avatar answered Oct 14 '22 06:10

iyegoroff