Hello. I need to transform a folder of PNG gray pictures to hex files

조회 수: 1 (최근 30일)
Ziad Osman
Ziad Osman 2025년 3월 13일
댓글: DGM 2025년 3월 13일
I have a folder that has hundreds of PNG gray images that I need automate their transformation into hex files for classification
  댓글 수: 4
dpb
dpb 2025년 3월 13일
편집: dpb 2025년 3월 13일
"... 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
DGM 2025년 3월 13일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2025년 3월 13일
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

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by