Q:

Convert File to Image

I take a picture using ImagePicker that is saved like File. What I want is to convert it to png before upload it to FirebaseStorage. The problem is that when I try to convert it using this function:
File('test.png').writeAsBytesSync(encodePng(image));
it gave me error because the photo is saved like File not Image.
I've googled for some type of plugin or function to convert it from File to Image but have found nothing concrete.
Can anyone help me with this, please?
EDIT: Just to be sure I explained it clearly: I have photo 'photo 1.jpeg' that is stored like File, because this is what the ImagePicker does. Then I want to convert this File to Image and then convert it to .png

A:

If you want to re-encode your image as a PNG, you will need to:

Read the bytes from the file via File.readAsBytes.  This will give you a Uint8List (which is compatible with List<int>).
You then will need to decode those bytes (e.g. with decodeJpg) to transform them into package:image's Image type.
Next you will need to use encodePng on the Image.  This will give you more bytes (as a List<int>).
Finally you will need to write those bytes to a file via File.writeAsBytes.

All that said, it's rather pointless to decode a JPEG and to re-encode it as a PNG; you're just making the image take up more space than it needs to.

