How to read '.webp' image file

58 visualizzazioni (ultimi 30 giorni)
Salad Box
Salad Box il 15 Gen 2023
Modificato: DGM il 11 Feb 2025 alle 11:35
Hi,
I have a couple of image files in '.webp' format, how to read them in Matlab?

Risposta accettata

KALYAN ACHARJYA
KALYAN ACHARJYA il 15 Gen 2023
Modificato: KALYAN ACHARJYA il 15 Gen 2023
This option!
Save in the web & you can try using complete web path
Reference : Link
HTH
  3 Commenti
Image Analyst
Image Analyst il 16 Gen 2023
I don't understand. That question seems to only talk about .JPG images on the web. How would any of that work for .webp format images and get them into a variable in MATLAB?
Alec Jacobson
Alec Jacobson il 10 Feb 2025 alle 14:36
This seems answer a different question than what's written in the title or the OP. Does matlab support .webp?

Accedi per commentare.

Più risposte (3)

Alec Jacobson
Alec Jacobson il 10 Feb 2025 alle 14:51
MATLAB doesn't seem to support .webp (a format used by 15% of websites).
One solution is to load it in python (using PIL and numpy) and pass it back to matlab. Here's a quick example to read RGBA:
function imgData = py_read_webp_rgba(image_path)
% imgData = py_read_webp_rgba(image_path)
%
% You may need to run:
% system([char(getfield(pyenv,'Executable')) ' -m pip install pillow numpy'])
% Import necessary Python modules
py.importlib.import_module('PIL.Image');
py.importlib.import_module('numpy');
% Open the image using PIL
img = py.PIL.Image.open(image_path);
% Convert image to RGBA mode if it isn't already
img = img.convert('RGBA');
% Convert the PIL image to a NumPy array
np_array = py.numpy.array(img);
imgData = uint8(np_array);
end
  2 Commenti
Image Analyst
Image Analyst il 10 Feb 2025 alle 20:02
Thanks for figuring this out. 🙂
DGM
DGM il 11 Feb 2025 alle 11:28
Modificato: DGM il 11 Feb 2025 alle 11:35
I don't know anything about python, but I managed to get a python write utility to work too. Supports I/IA/RGB/RGBA inputs, but everything will be converted to uint8 RGB/RGBA. I haven't bothered looking at the format specification, but cwebp appears to enforce the same conversion, so I'm assuming it's canonical.
I don't agree on always expanding everything to RGBA on import, so I made a copy of the reader without that line. The output will either be RGB/RGBA depending on its content. Obviously, users need to be aware that handling monolithic RGBA images in MATLAB takes extra effort and considerations.
I don't know how much speed matters, but these tools are much faster than MIMT wpread() and wpwrite() for obvious reasons. That said, I don't know which is the more conveniently-met dependency -- libwebp or all the python stuff. I guess lots of people already have both.

Accedi per commentare.


Image Analyst
Image Analyst il 16 Gen 2023
Try this. Adapt as needed:
% If you enter this address
% 'https://i0.wp.com/www.bitsandpieces.us/wp-content/uploads/2023/01/unnamed-15.png?resize=600%2C334&ssl=1');
% If, in your browser, you right click, it will ask you to save the image as a .webp format file.
% But what you really want is a variable in MATLAB and from there you can save it as any file format you want.
imageURL = 'https://i0.wp.com/www.bitsandpieces.us/wp-content/uploads/2023/01/unnamed-15.png?resize=600%2C334&ssl=1';
[indexedImage, cmap] = imread(imageURL);
rgbImage = ind2rgb(indexedImage, cmap);
imshow(rgbImage);
% imwrite(rgbImage, 'Mistakes.png')
  2 Commenti
Sven
Sven il 13 Giu 2023
Hey IA, can you describe what's happening under the hood here?
I believe the original image is a .png but the URL suffix specifies a resize, which is done (on the server side?) using the webp format before serving that result?
Anyway, the main thing I'm coming up against is that there doesn't seem to be any native ability for MATLAB to read a .webp file stored locally. For instance, calling imread() with your provided URL works just fine, but if use your browser to save a local .webp version of the image and then provide the location of that file to imread we get the error:
>> imread(myLocalFile)
Error using imread>get_format_info
Unable to determine the file format.
I've tried providing "png" format specifier and also specifying the filename in "file:///C:/path/to/image/image.webp" format - same result.
The only way I've found to read a .webp locally is to download google's .webp processing libraries (webpd.exe for windows) and then call it from MATLAB via a system() call telling it to send the output to a temporary .png file, and then using imread() to read that resultant file. This feels clunky but maybe it's the only solution at the moment?
Anyway, just thought I'd add to the thread in case the same questions were coming up for others.
Image Analyst
Image Analyst il 13 Giu 2023
@Sven I have no idea. I know little about .webp or even why people use that instead of the original format. I didn't even know about Google's webpd.exe until you told me about it. Ask tech support if there is some better way to read .webp files.

Accedi per commentare.


DGM
DGM il 5 Lug 2024
Modificato: DGM il 5 Lug 2024
@Sven's workaround is still about the only thing there is. I just added a simple pair of tools to MIMT to read and write WEBP using cwebp and dwebp. There are a few conveniences added, but they're still basically just using system() calls and temp files.
% at least it's convenient
inpict = wpread('ughnotanother.webp');
Images with transparency (IA/RGBA) will be returned with attached alpha.
MIMT wpwrite() accepts I/IA/RGB/RGBA images, and defaults to lossless encoding options (-lossless and -exact in cwebp versions which support it). Something tells me though that 99% of users' interest in WEBP is trying to figure out how to make it go away, not how to create more.

Categorie

Scopri di più su Large Files and Big Data in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by