"Could not open file." using imwrite for png file.

global frames;
global fila;
obj=mmreader(fila);
a=read(obj);
frames=get(obj,'numberOfFrames');
for k = 1 : 50
v1(k).cdata = a(:,:,:,k);
v1(k).colormap = [];
end
frames = {v1.cdata};
str2='.png';
for i=1:length(frames)
filename1=strcat(num2str(i),str2 );
imwrite(frames{i},filename1);
data1 = imread(filename1);
data2 = imresize(data1,[144 176]);
iFile = filename1;
imwrite(data2,iFile,'png');
end
frames = {v1.cdata};
??? Error using ==> png
Could not open file.
Error in ==> writepng at 429
png('write', data, map, filename, colortype, bitdepth, ...
Error in ==> imwrite at 477
feval(fmt_s.write, data, map,
filename, paramPairs{:});
Error in ==> main>pushbutton2_Callback at 204
imwrite(data2,iFile,'png');
Sometimes the loop completes giving all the frames of the video of required resolution without any error but sometimes it generates a random number of images and then gives this error. For a few videos i tried, the loop pops up this error even before generating any image at all.
This loop works even without mentioning the 'png' parameter in both imwrite but the result is the same error.
Default toolboxes and block sets present.

 Accepted Answer

What is the current working directory? If it's something under Program Files, and you're using Windows 7, you won't be allowed to do that. Why are you not using fullfile() to create the full file name of your image files?
Why are you calling imwrite() twice: first to write out the full size frame, and then almost instantly after it to write out the resized image to the same filename. Maybe the first call to imwrite is not done yet, but I'm not sure why you're even calling it the first time to begin with. Try getting rid of the first call to imwrite() or adding a pause() of a half second or so.

3 Comments

Image Analyst, I didn't try fullfile() or pause().My files are stored in Documents and i use Windows 8. However thank you for pointing out the two imwrite. I observed that re-sizing of frames was redundant for my requirements. So removed last 4 lines of the loop. So far it has worked out with the test videos and doesn't throw up the error.Thanks again.
This works for me. The current directory must be the same folder where you want to save the .png images. Thx.
The folder where you save your image does not have to be the current directory.
You can specify a full path and name to save your image anywhere you want.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!