Home

Resume

My SAS

Research

My SAS

Family

Feedback

Home

 

My SAS - SAS Code


Back to SAS Code

Image processing (resize and rotation)


SAS Code:
dm 'log;clear; out;clear;';

/*
                  Imaging processing program                                                              
                    Songlin Zhu, Ph. D.                                                                   
                                                                                                         
   scllib       -- libname for link scl program called modify_image.sas7bcat
   mod=size     -- Resize image
   mod=rota     -- Rotate image, Only support 90 degree increments
   in_path      -- directory where input images were saved
   out_path     -- directory where output images will be saved
   image_format -- JFIF=JPG
   scale        -- Rate used to streth image
   keep_rate    -- keep_rate =  Y: Keep original height-to-width ratio
                   keep_rate ne Y: Use the given width and height
   image_width  -- width of output image
   image_height -- height of output image
   current_dir  -- current_dir =  Y : Do not process files in sub-directories
                   current_dir ne Y : Process files in sub-directories

   Examples 1:
            %macro imageprocess;
               %let mod          = size;                  * Resize image ;
               %let in_path          = C:\sasuser\img;
               %let out_path         = C:\sasuser\img\aa\bb;
               %let image_format = JFIF;                   * JPG image ;

              * Strech image with 1/2 rate. ;
              * Scale must have decimal fraction if it is not a integer ;
               %let scale        = 0.5;

               * Files in sub-directories will not be processed if current_dir = Y;
               * let currect_dir = Y;

              * Call scl program to resize image
               dm 'af c=scllib.modify_image.image_process.scl';
            %mend imageprocess;

   Examples 2:
            %macro imageprocess;
               %let mod          = size;
               %let in_path          = C:\sasuser\img;
               %let out_path         = C:\sasuser\img\aa\bb;
               %let image_format = JFIF;

               * scale must be <= 0 when input width and height will take effect;
               %let scale        = 0;

               * Does not keep height-to-width ratio as input image;
               %let keep_rate    = N;

               %let image_width  = 100;                    * Output image width;
               %let image_height = 100;                    * Output image height;

               * Files in sub-directories will not be processed if current_dir = Y;
               * let currect_dir = Y;

               Call scl program to resize image ;
               dm 'af c=scllib.modify_image.image_process.scl';
            %mend imageprocess;

   Examples 3:
            %macro imageprocess;
               %let mod          = size;
               %let in_path          = C:\sasuser\img;
               %let out_path         = C:\sasuser\img\aa\bb;
               %let image_format = JFIF;

               * scale must be <= 0 when input width and height will take effect;
               %let scale        = 0;

               * Keep height-to-width ratio (H/W) as input image;
               %let keep_rate    = Y;

               * Output image width;
               %let image_width  = 100;                                                                                                           

               * Automatic calculate image height based on origianl H/W ratio even it is 0 or other value;
               %let image_height = 0;

               * Files in sub-directories will not be processed if current_dir = Y;
               * let currect_dir = Y;

              * Call scl program to resize image ;
               dm 'af c=scllib.modify_image.image_process.scl';

            %mend imageprocess;

  output files: if original files is aa.jpg, ff.jpg, bs.jpg, df_is.jpg whick are in &in_path,
                the ourput file would be A_aa.jpg, A_ff.jpg, A_bs.jpg, A_df_is.jpg
                which save in the given directory (&out_path). If the directory does not exist,
                create one automatically
*/

libname scllib "C:\sasuser\cat";

%macro imageprocess;
   %let in_path          = C:\sasuser\img\;
   %let out_path         = C:\sasuser\img\temp;
   %let search_string    = .jpg;
   %let mod              = size; 
   %let image_format     = JFIF;
   %let scale            = 0;
   %let keep_rate        = Y;
   %let image_width      = 0;
   %let image_height     = 200;
   %let current_dir      = Y;

   dm 'af c=scllib.modify_image.find_process.scl';
 
%mend imageprocess;

%imageprocess;

This program is used to process all images in a directory including or not including sub-directories.

A SCL program was invoked by above program. Download the SCL program and save it in the directory c:\sasuser\cat. If you want to save it in another directory, change c:\sasuser\cat into the directory you save the SCL program. All input images should be put in the directory that micro variable &in_path points and output images will save in the directory that &out_path points.

If you have any questions, feel free to send me feedback.