How to convert Unit8List to Files in flutter - opencv

I have used opencv to enhance the image in flutter which returns a Unit8List as a dynamic result i have used to display the image.
Image newImage = Image.memory(res);
How to convert that image it to file, or how to write Unit8List as a file. I have tried this but its printing ���� as the file path
File imageNeww = File.fromRawPath(res);
print(imageNeww.path);

Use the file class
File f = File('desired/path/to/file.png');
await f.writeAsBytes(bytes);
here bytes are your Uint8List

Related

jpg images without extension aren't displayed

from kivy.uix.image import Image
self.img = Image(source="image") # This works when image is an PNG image
self.img = Image(source="image.jpg") # This works when image.jpg is a JPG image
self.img = Image(source="image") # This doesn't work when image is a JPG image
I need to specify images without extention for the app to be generic (working with more image types). Can I achieve it somehow?
Kivy is using "imghdr" to determine the image type here, and as a fallback it uses the file extension here.
That explains why the image loads fine when it has a file extension, even though "imghdr" can't find the file type in the file's content.
I tested on a list of JPEG files, and each time "imghdr" was able to detect the file type each time. That is done here im imghdr. Notably, "imghdr" does not consider the file extension.
$ python
>>> import os, imghdr
... for f in os.listdir('.'):
... print('%s -- %s' % (f, imghdr.what(f)))
Maybe the JPEG file is missing the "JFIF" or "Exif" string that imghdr is looking for? You could use hexedit to see if one of those string is present at Byte 6 of the image file.

Reading Image file in Opencv without ext

I am trying to download an image from URL, but it's difficult to get the complete file name and extension.
Currently, I save the downloaded image with any name and without an extension.
with open(path, "wb") as f:
f.write(request.content)
I then use opencv to read this image which has no extension and it works. How is this possible?
opencv tries magic numbers / signatures for known image formats:
https://en.wikipedia.org/wiki/Magic_number_(programming)
Implementation details:
https://github.com/opencv/opencv/blob/666be238d84c339993bf18c0798d27afd420d6be/modules/imgcodecs/src/loadsave.cpp#L209

how to call multiple image with opencv

I am using this line of code to call my image in python
(img = cv2.imread("frame12160.jpg")
but I can just call one image once a time how can I call multiple images every time and thanks in advance
You can only read one image at a time using cv2.imread(). If you want to read in multiple images, try using the os package and save the images into a list:
import cv2
import os
my_images = []
os.chdir('/home/stephen/Desktop/images/')
for path in os.listdir(os.getcwd()):
img = cv2.imread(path)
my_images.append(img)

What does the command String imageName( "../data/HappyFish.jpg" ); do?

I am a total beginner in OpenCV. I was learning a program to load and display an image where the below statement is used.
String imageName( "../data/HappyFish.jpg" ); //by default
what does this statement do?
This statement is the folder directory of the image file HappyFish.jpg in the OpenCV example Load and Display an Image. Simply it is used with imread to indicate the location where our image file is located.
image = imread( imageName, IMREAD_COLOR ); // Read the file

Convert Binary Data to Torch Tensor in Lua

I have Lua code that downloads an image from a url using a luasocket:
local http = require('socket.http')
local image = require('image')
image_url = 'https://www.somedomain.com/someimage.jpg'
local body, code = http.request(image_url) -- body has jpg binary data
if not body then error(code) end -- check for errors
In order to read this image into a Torch tensor, I save it in a jpg file and read it using image.load:
-- open a file in binary mode to store the image
local f = assert(io.open('./temp.jpg', 'wb'))
f:write(body)
f:close()
tensor = image.load('temp.jpg')
Is there a way to convert the binary jpg data to a torch tensor directly without doing a write-to-and-read-from the hard-drive? Something like:
tensor = CovertBinaryDataToTorchTensor(body)
Thank you!
See image.decompressJPG.
You just have to pack your body string inside a ByteTensor first. This can be done by constructing this tensor with a storage which can set his contents with string(str).
One potential solution is to use graphicsmagick.
local gm = require 'graphicsmagick'
local img = gm.Image()
local ok = pcall(img.fromString, img, body)
img = img:toTensor('float', 'RGB', 'DHW')
I found this example in https://github.com/clementfarabet/graphicsmagick/blob/master/test/corrupt.lua and I know that
local body, code = http.request(image_url)
will return body as a string. And, obviously if pcall returns false, the image was corrupt.

Resources