Reading an image with fread and displaying this image again issues?
4 views (last 30 days)
Show older comments
hi all
came into a problem with some code. this is being used for a bigger project so ive simplified my issue into a smaller example problem
im basically reading peppers.png, converting this data to JPEG format so i can compress it by quality factor.
reading this compressed image file data.
and trying to reshow this compressed image as a new file with a different name.
but right now i lose the image completely. Below is a copy of my code and images so far
compressedImage.jpg

hmm.jpg

ImageData = imread('peppers.png');
imwrite(ImageData,'compressedImage.jpg','jpg','quality',100); %compress the image by different quality factor
G = size('compressedImage.jpg');
fileID =fopen('compressedImage.jpg');
A = fread(fileID,[384,512]);
fclose(fileID);
imwrite(A,'hmm.jpg','jpg');
0 Comments
Answers (1)
Image Analyst
on 30 Mar 2022
You can't open a JPG image like that - just a raw stream of pixels. It's not like that. There is compression going on so you need to decompress. imread() decompresses but fread() does not. So of course it looks like garbage. Plus A might be floating point which would make it display weird like that.
4 Comments
jacob cunnison
on 30 Mar 2022
@Image Analyst so in order to get anything that resemebels that image you'd need to extract the header data first and then the pixel data and somehow combine the two sets of information? If so which functions would you use?
Thanks
Image Analyst
on 30 Mar 2022
I don't see any need to combine them. If you can figure out where the uncompressed pixels live, just read them into an array and start using it in your MATLAB code. If any header is needed when writing it out again, imwrite() will add it.
See Also
Categories
Find more on Image Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!