필터 지우기
필터 지우기

Read in a general binary file (for example, but not exclusively a jpeg file), make a modification and write a new file out verbatim--no altered bytes.

조회 수: 3 (최근 30일)
Someone has written a Python program that I would like to translate into MATLAB:
There are stereoscopic (3D) files that are just concatenated jpegs. There are two FF D8 byte pairs signifying the beginning of a jpeg. The software produces separate jpegs for each of the two concatenated jpegs.
My program reads in the file with
bytes=fileread(infilename);
It then finds the two occurrences of FF D8 (as 255 and 216):
beginBytes=strfind(bytes,char(255));
for i=1:length(beginBytes)
if bytes(beginBytes(i)+1)==char(216)
jpegCtr=jpegCtr+1;
offsets(jpegCtr)=beginBytes(i)
end
end
and tries to write out the first included jped:
fid=fopen('c:\stereophotomaker\childrens library_l.jpg','w');
% fprintf(fid,'%s',bytes(offsets(1):offsets(2))); % first tried way
%fwrite(fid,bytes(offsets(1):offsets(2)),'*char'); % second
fprintf(fid,'%s',bytes); %third
fclose('all');
but even when attempting a fraction of the original file, the newly created file is larger than the half file attempted, and the last trial produces a file larger than the whole original file, even though it's the same bytes.
Regardless of which method used, the resulting file is not recognized by Windows Media Player, though called a .jpg.

채택된 답변

Les Beckham
Les Beckham 2023년 7월 28일
fileread reads the file as text. You need to open the file and read it as binary bytes (uint8) and then process it that way.
Replace
bytes=fileread(infilename);
with
fp = fopen(infilename, 'r');
bytes = fread(fp, 'uint8')
and also replace your
beginBytes=strfind(bytes,char(255));
with
beginBytes = find(bytes == uint8(255));
and similarly for finding the 216 marker(s).
  댓글 수: 3
Les Beckham
Les Beckham 2023년 7월 31일
I'm glad that you got things figured out. Thanks for accepting my answer.
Alexander
Alexander 2023년 7월 31일
You can do it also in one step:
beginBytes=strfind(bytes,[255 216]);

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

추가 답변 (1개)

Image Analyst
Image Analyst 2023년 7월 29일
See the FAQ for how to read in a series of files.
After you read them in, create an output file name that has PNG as the extension and call imwrite. Then it will write out the new image with exactly the same values it has in MATLAB. At this point, you'll have the same pixel values regardless if you read in the JPG version of the file or the PNG version of the file. Whatever you're going to do with the image matrices after that does not care what format the original disk files were in, so you might as well just stick with the JPG files (unless you plan on modifying the image matrix out and resaving it).
  댓글 수: 1
Charles Kluepfel
Charles Kluepfel 2023년 7월 29일
My question was not about a series of files, but rather one file that was a concatenation of two jpegs in the same file (two occurrences of the FF D8 signifier, the second being about halfway into the file). The goal was to split the one file into the two separate jpegs. Les's answer solved the problem: use fread instead of fileread, (and on output use fwrite, both using 'uint8').

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

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by