create new images by resizing the images in a folder

3 views (last 30 days)
i have a folder of 7 images
i wanted to read each image one by one and create new images of different sizes, till i have 100 total images, including the initial 7 images
when i resize, the new images i create from a single image, should not have the same size, each image i create should have unique size
  1 Comment
Elysi Cochin
Elysi Cochin on 30 Aug 2021
Below is the code i wrote
But its not complete
Checking if number of images has reached 100, checking if the new images generated has same image size
The above two points, when i add its going to infinite loop,
please can someone help me write the code in a simple way
% Generate 25 random numbers
a = 0.5;
b = 1;
rand('seed',0);
r = (b-a).*rand(25,1) + a;
for icnt = 3 : tnum
for i = 1 : rn
filename = cfilename(icnt,1).name;
imfiname = sprintf('Data/%s/%s', subFolders(fcnt).name, filename);
im = imread(imfiname);
if size(im,3) == 3
im = rgb2gray(im);
end
im = imresize(im,r(i));
sz(i,:) = size(im);
cnt = cnt + 1;
outfiname = sprintf('Data/%s/copy_%s_%s', num2str(cnt), filename);
imwrite(im, outfiname);
end
end

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 30 Aug 2021
please can someone help me write the code in a simple way
Oddly enough, yesterday someone had the same need, and I described to them how to write the code in a simple way. However, they then deleted the question, so I cannot refer you to what that other person had already posted and my response to it.
Fortunately, the logs were complete enough that I was able to rescue a partial copy of what I wrote then:
"If you have only 7 images and you need a total of 100, does that mean that you want each image to be copied 14 times, plus another 2 images to make the total 100? If so then use copyfile() instead of imread() / imwrite()"
That is, the proper way to make copies of images is not to imread() / imwrite(): the proper way is to copyfile() the image. You can copyfile() to a different file name if you need to.
  6 Comments
Walter Roberson
Walter Roberson on 30 Aug 2021
numrep = [repmat(13,1,5), 14, 14];
numrep = numrep(randperm(7));
for K = 1 : 7
filename = cfilename(K).name;
this_image = imread(filename);
copy this_image to the destination
numrow = size(this_image,1);
numcol = size(this_image,2);
target_row_range = ceil(numrow/2):numrow-1;
target_rows = target_row_range(randperm(length(target_row_range), numrep(K)));
for R = 1 : numrep(K)
new_cols = ceil(target_rows(R)/numrow * numcol);
resized_image = imresize(this_image, [target_rows(R), new_cols]);
write out resized_image
end
end
No need to keep track of the size previously generated for a particular image because the algorithm generates the sizes in a way that is certain to have a different number of rows. It generates all of the integers from numrow/2 to numrow-1 and randomly selects the necessary number of them without repetition.
Likewise, there is no need to dynamically check as you go through whether this is an image that needs to be generated more often, and no need to test as you go whether you have reached the desired total number of images. Instead, it uses the information about the total number of images to generate a vector with the correct total and then scrambles that; the scrambled order is certain to still have the same total.

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!