Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.59 KB | None | 0 0
  1. % Margins
  2.  
  3. % SPECIFY RATIO
  4. RATIO_HEIGHT = 10;
  5. RATIO_WIDTH = 8;
  6.  
  7. % READ FILES
  8. files = dir('*.jpg');
  9.  
  10. % START
  11. clc
  12. for f=1:size(files)
  13.     % READ FILE
  14.     IMG = imread(files(f).name);
  15.     [h w d] = size(IMG);
  16.    
  17.     % CALCULATE RATIO
  18.     ratio = w/h;
  19.    
  20.     % PRINT FILENAME
  21.     fprintf('\n\n-------------------------------------');
  22.     fprintf('\n# PROCESSING FILE: \t%s', files(f).name);
  23.    
  24.     % PRINT ORIENTATION
  25.     if(h > w)
  26.         orientation = 1;
  27.         fprintf('\nPORTRAIT');
  28.     else
  29.         orientation = 2;
  30.         fprintf('\nLANDSCAPE');
  31.     end
  32.    
  33.     % PRINT DIMENSIONS
  34.     fprintf('\t|\t%i x %i', h, w);
  35.    
  36.     % CHECK RATIO?
  37.     if(h/w == RATIO_HEIGHT/RATIO_WIDTH)
  38.         fprintf('\n > Correct aspect ratio.');
  39.         NIMG = IMG;        
  40.     else
  41.         fprintf('\n! BAD aspect ratio : %f', h/w);
  42.        
  43.         % CALCULATE NEW DIMENSIONS
  44.         if(ratio < RATIO_WIDTH/RATIO_HEIGHT)
  45.             ch = h;
  46.             cw = round(RATIO_WIDTH/RATIO_HEIGHT*ch);
  47.         else
  48.             cw = w;
  49.             ch = round(RATIO_HEIGHT/RATIO_WIDTH*cw);
  50.         end
  51.        
  52.         % PRINT NEW DIMENSIONS
  53.         fprintf('\nNEW DIMENSIONS: \t%i x %i | ADDED: %i x %i', ch, cw, ch-h, cw-w);
  54.        
  55.         % CREATE NEW IMAGE
  56.         NIMG = uint8(zeros(ch,cw,d));
  57.        
  58.         % COPY
  59.         for k=1:d
  60.             for i=1:h
  61.                 for j=1:w
  62.                     NIMG(i,j,k) = IMG(i,j,k);
  63.                 end
  64.             end
  65.         end
  66.     end
  67.        
  68.     % WRITE
  69.     imwrite(NIMG, ['modified\' files(f).name '.png'], 'png');
  70. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement