Convert tiff image to jpeg or JPEG or Png

62 次查看(过去 30 天)
Hi there,
I am having an issue converting a tiff image file into a jpeg or png.
Pseudo: 1. Read in image with
img = imread();
2. Write the same file with
imwrite(img,'imgNew.jpg');
But it does not work.
Please can someone help me out with converting a tiff image to EITHER jpeg or .Png. Either one will be fine.
Thanks in advance!
  1 个评论
Ashish Uthama
Ashish Uthama 2014-10-8
What does 'it does not work' mean? Does it error or the image saved not what you expected? If its the latter, how so?
Most likely the class of img is not uint8, since jpeg/png mostly need data to be in uint8, you would have to figure out how to convert the non-uint8 in the Tiff to uint8 for JPEG.

请先登录,再进行评论。

回答(1 个)

Mann Baidi
Mann Baidi 2024-1-17
As per my understanding of the question, you are facing issue in reading the 'tiff' file and then converting it into JPEG.
Assuming that you might have multi-frames tiff image, when you read the tiff image using "imread()" function. This by defualt reads the first frame of the tiff image. For reading the other frames, you have to pass the frame number as parameter to the "imread" function as mentioned below:
imread("path/to/image",<frameNumber>);
Here is an example of how to read and civert multi-frame tiff images:
% Specify the path to your multiple frames TIFF file
tiffFilePath = 'multiframe_example.tif';
% Create a directory to save the JPEG images
outputDirectory = 'path/to/your/output/directory';
mkdir(outputDirectory);
% Read the multiple frames TIFF file
tiffInfo = imfinfo(tiffFilePath);
% Loop through each frame and save it as a JPEG image
for frame = 1:numel(tiffInfo)
% Read the current frame
imageData = imread(tiffFilePath, frame);
% Create a filename for the JPEG image (you can adjust the filename as needed)
jpegFileName = sprintf('frame_%03d.jpg', frame);
% Specify the full path for the output JPEG image
jpegFilePath = fullfile(outputDirectory, jpegFileName);
% Write the current frame to a JPEG file
imwrite(imageData, jpegFilePath);
end
Hope this works!

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by