How can I encode images to Base64?
Afficher commentaires plus anciens
Hi, I want to encode an image to Base64, but the result is completely different than when I encode the image on this page.
I don't understand what I am doing wrong, is it me or the page I am using?
Here is my code (Google Bard helped me):
function encoded_string = base64encode_image(image)
image_uint8 = im2uint8(image);
encoded_bytes = zeros(1, length(image_uint8) * 4 / 3);
for i = 1:length(image_uint8)
encoded_bytes(4 * (i - 1) + 1:4 * i) = base64_chars(image_uint8(i) / 64 + 1);
encoded_bytes(4 * i + 1:4 * i + 2) = base64_chars((image_uint8(i) % 64) / 8 + 1);
encoded_bytes(4 * i + 2:4 * i + 3) = base64_chars((image_uint8(i) % 8) + 1);
end
encoded_string = char(encoded_bytes);
while length(encoded_string) % 3 ~= 0
encoded_string = [encoded_string "="];
end
end
Thank you!
8 commentaires
Les Beckham
le 5 Déc 2023
I don't see how you got any result at all from this code. It is not valid Matlab code.
The % character is the comment delimiter in Matlab (not the modulo operator like in C), so three of these lines will generate syntax errors.
Dyuman Joshi
le 5 Déc 2023
Modifié(e) : Dyuman Joshi
le 5 Déc 2023
In addition to Les's comment, the code assumes that length(image_uint8) will be divisible by 3, which is not guaranteed.
If you want to try an AI, try the AI Chat Playground by TMW
Laszlo
le 5 Déc 2023
Dyuman Joshi
le 5 Déc 2023
The error is clear, MATLAB does not know what the parameter base64encode is. Thus, it does not know what to do with that command.
It's most likely a function, like the one you posted in your question i.e. base64encode_image.
From where did you get this particular snippet of code?
Les Beckham
le 5 Déc 2023
There is at least one base64 image encoder on the File Exchange.
Laszlo
le 5 Déc 2023
Hmmm. So, I asked the AI the same thing as well. I have attached pictures as to how it went.
which base64encode -all
And it was a bit weird. It references a function that does not exist and mentions the latest version as R2021a.
I tested for the function in my R2021a as well, but it had the same output i.e. not found.
But I did find this - matlab.net.base64encode, but that expects the inputs as either string, character vector or a numeric vector.
Aside from the AI being consistently misleading about how to access it and how to use it, that could be used. At least that would avoid a FEX dependency.
% encode only the raw image data
inpict = imread('peppers.png');
V = matlab.net.base64encode(inpict(:));
outpict = matlab.net.base64decode(V);
outpict = reshape(outpict,size(inpict));
imshow(outpict)
As to whether it matches some other encoder, I suppose that depends on what you're comparing. If you encode a JPG image, you're encoding the entire file as a byte stream -- the header, the metadata, the compressed raster data. If you read the image with imread(), you're just encoding an arbitrarily vectorized bunch of raw uncompressed image data. If you fed the latter to your web browser, it probably wouldn't know what to do with it.
% encode the file itself
fid = fopen('peppers.png');
V = fread(fid,'*uint8');
fclose(fid);
V = matlab.net.base64encode(V); % a web browser could read this
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Convert Image Type dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!