Hello. I need to transform a folder of PNG gray pictures to hex files
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have a folder that has hundreds of PNG gray images that I need automate their transformation into hex files for classification
4 Commenti
dpb
il 13 Mar 2025
Modificato: dpb
il 13 Mar 2025
"... a text file with each pixel in the PNG picture represented as hexadecimal string"
Let me ask "Why?". What can you do/are you going to do with the string representation you can't do directly from the binary representation already have which is same numerically as will be the result of reading the hex number?
DGM
il 13 Mar 2025
You will have no indication of the geometry or depth of the image. You'll just have a vector of values. Vectorized in what direction? Who knows. That was never specified.
How do you propose to store geometry information? Does it matter which way the data is vectorized? If we don't need to devectorize the data for analysis, maybe we don't need those details, but we don't know that.
If you have a reason to be cramming image data into text files, then surely you're doing so because you're trying to adhere to some existing convention for doing so. It seems you're pursuing this format because you already have something that reads it, but we don't know how to make the format adhere to those expectations unless you say what it is. Otherwise, I don't see why the need exists.
If you want a standardized, simple, easily decodable image format, you can just use PGM/PPM and be done with it. A PGM file consists of the filetype magic number, the geometry, the max grayvalue, followed by the uncompressed pixel data vectorized row-wise. It can be encoded in plain text or binary, and imwrite() can write either.
Risposte (1)
Walter Roberson
il 13 Mar 2025
projectdir = './inputimages'; %directory to look for png files
hexfiledir = './hexdir'; %directory to store hex files
hexext = '.txt';
if ~isdir(hexfiledir); mkdir(hexfiledir); end
dinfo = dir( fullfile(projectdir, '*.png'));
filenames = fullfile( {dinfo.folder}, {dinfo.name});
numfiles = numel(filenames);
for K = 1 : numfiles
thisfile = filenames{K};
[~, basename, ~] = fileparts(thisfile);
outfile = fullfile( hexfiledir, [basename hexext]);
img = fileread(thisfile);
hexdata = reshape( dec2hex(img(:), 2).', 1, []);
[fid, msg] = fopen(outfile, 'w');
if fid < 0; error('failed to open file "%s" for writing because "%s"', outfile, msg); end
fwrite(fid, hexdata);
fclose(fid);
end
This will read .png files that live within projectdir, and convert the image content into row vectors of hex data, following down columns. If the png happen to be color images, the red pixel components will appear, then the green pixel components, then the blue pixel components. The hex data is written to .txt files within the hexfiledir
0 Commenti
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!