#1 2011-03-03 20:43

mjbiggs
Member
Registered: 2011-03-03
Posts: 3

Add aspect ratio as suffix to image files

Hello

I have just discovered ReNamer and believe that, via a script, it may be able to do what I want.

I wish to be able to scan a folder of image files then automatically rename each one by adding a suffix comprising it's decimal image ratio.

For example:

original file name (with cropped dimensions width = 500, height = 300):

mjb_20110101_0001.jpg


new file name with image ratio suffix added:

mjb_20110101_0001_1.67.jpg


The suffix 1.67 is calculated as width / height = 500 / 300 = 1.67.

Here I have chosen to limit the output to 2 decimal places and rounded up to 1.67.

Does anyone know how to do this?

Any help greatly appreciated.

Thanks.

Mike

Offline

#2 2011-03-03 23:33

Stefan
Moderator
From: Germany, EU
Registered: 2007-10-23
Posts: 1,161

Re: Add aspect ratio as suffix to image files

Hi Mike, welcome.

mjb_20110101_0001.jpg
mjb_20110101_0001_1.67.jpg

I have take an look into script docu how to extract exif info from file but found no function.



So i thought we can make an work around and use Insert-Rule first to add
the exif info :Image_WidthAndHeight: to the file name temporary (only for the preview, no real rename here)

1) Insert: Insert "_:Image_WidthAndHeight:" as Suffix (skip extension)
mjb_20110101_0001_500x300.jpg




Then i would add an PascalScript-Rule as second rule to take the dimension from the file name and do the math:
2) PascalScript: var BaseName : WideString; W,H : WideString; R : Double; begin BaseName :=



But i had no luck to use type Double and convert this to string (DoubleToStr) to compose the new file name:

Pseudo code:

var
 BaseName : WideString;
 W,H          : WideString;
 R             : Double;
 
begin
  BaseName := WideExtractBaseName(FileName)

  W := ReplaceRegEx(BaseName, '(.+)_(\d+)x(\d+)', '$2', False, True);
  H := ReplaceRegEx(BaseName, '(.+)_(\d+)x(\d+)', '$3', False, True);
  //ShowMessage( W + 'X' + H );


  R := StrToInt(W) / StrToInt(H);
  
  //round or shorten R here >

  FileName :=  BaseName + '_' + DoubleToStr( R )  + WideExtractFileExt(FileName);
end.

Here is an script with Int/Integer used which works like i had in mind, but aspect ratio is every time 1, of course due to Integer.

var
 BaseName : WideString;
 W,H          : WideString;
 R              : Integer;
 
begin
  BaseName := WideExtractBaseName(FileName)

  W := ReplaceRegEx(BaseName, '(.+)_(\d+)x(\d+)', '$2', False, True);
  H := ReplaceRegEx(BaseName, '(.+)_(\d+)x(\d+)', '$3', False, True);

  R := StrToInt(W) / StrToInt(H);
  
  //round or shorten here >

  FileName :=  BaseName + '_' + IntToStr( R ) + WideExtractFileExt(FileName);
end.

If this would work we could deselect the first Rule ( 1: Insert ) and do the renaming as wanted.



Perhaps you will get an faster result if you search this forum and the wiki for exif command line tool "exiv2"
which you can use with ReNamer.... there are a few scripts showing how to.
There is at least one another such tool. Maybe this tools provide already the wanted ratio as an string to use to rename?


Read the  *WIKI* for HELP + MANUAL + Tips&Tricks.
If ReNamer had helped you, please *DONATE* to Denis or buy a PRO license. (Read *Lite vs Pro*)

Offline

#3 2011-03-04 08:42

mjbiggs
Member
Registered: 2011-03-03
Posts: 3

Re: Add aspect ratio as suffix to image files

Hello Stefan

Thanks very much for your help. I am very interested in your approach - it is very helpful for me as a novice. I will have to spend sometime learning before I understand fully.

However, I have meanwhile been hacking away, based on info found in the forum, and come up with a solution that appears to work OK:

var
  Height, Width: Integer;
  name: WideString;
  extension: WideString;
  ratio: Extended;
  ratiostring: WideString;

begin
  Width  := StrToIntDef(CalculateMetaTag(FilePath, 'Image_Width'), 0);
  Height := StrToIntDef(CalculateMetaTag(FilePath, 'Image_Height'), 0);

  begin
    name := WideExtractBaseName(FileName);
    extension := WideExtractFileExt(FileName);

    if Height <> 0 then
      begin
        ratio := Width / Height;
        ratiostring := FormatFloat('00.00', ratio);
      end
  end

  FileName := name + '_c' + ratiostring + extension;
end.

There is probably a more elegant solution and I'm sure the coding could be improved, but this seems to do the job. Only remaining bit for me to do is to replace the decimal point in the ratio by a dash -more hacking to follow.

Thanks again for your help.

Regards

Mike

Offline

#4 2011-03-04 10:17

SafetyCar
Senior Member
Registered: 2008-04-28
Posts: 446
Website

Re: Add aspect ratio as suffix to image files

mjiggs, there's a dangerous thing on that code, on renamer keeps the values of the previous files during all the preview, so look at the condition, if the condition is false (skipped), the ratio value will keep the same as the previous file, what is not correct, so you should think on the "else" possibility.

So, you might be interested in doing it on other ways. Here 2 examples

1. (Wich I like the most) Only rename when a valid value was found:

var
  Height, Width: Integer;
  name: WideString;
  extension: WideString;
  ratio: Extended;
  ratiostring: WideString;

begin
  Width  := StrToIntDef(CalculateMetaTag(FilePath, 'Image_Width'), 0);
  Height := StrToIntDef(CalculateMetaTag(FilePath, 'Image_Height'), 0);

  if Height > 0 then
  begin
    name := WideExtractBaseName(FileName);
    extension := WideExtractFileExt(FileName);

    ratio := Width / Height;
    ratiostring := FormatFloat('00.00', ratio);

    FileName := name + '_c' + ratiostring + extension;
  end;
end.

or

2. With the "else" part, giving 0 when there's a not valid height:

var
  Height, Width: Integer;
  name: WideString;
  extension: WideString;
  ratio: Extended;
  ratiostring: WideString;

begin
  Width  := StrToIntDef(CalculateMetaTag(FilePath, 'Image_Width'), 0);
  Height := StrToIntDef(CalculateMetaTag(FilePath, 'Image_Height'), 0);

  name := WideExtractBaseName(FileName);
  extension := WideExtractFileExt(FileName);

  if Height > 0 then
    ratio := Width / Height
  else
    ratio := 0;

  ratiostring := FormatFloat('00.00', ratio);

  FileName := name + '_c' + ratiostring + extension;
end.

Last edited by SafetyCar (2011-03-04 10:20)


If this software has helped you, consider getting your pro version. :)

Offline

#5 2011-03-04 12:28

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,380

Re: Add aspect ratio as suffix to image files

I can add a meta tag say :Image_AspectRatio: which would be easy to implement since it is a simple combination of already available image width and height, if that helps?

Offline

#6 2011-03-04 12:40

den4b
Administrator
From: den4b.com
Registered: 2006-04-06
Posts: 3,380

Re: Add aspect ratio as suffix to image files

Image_AspectRatio is available in the latest development version: ReNamer Beta.

Offline

#7 2011-03-04 14:30

mjbiggs
Member
Registered: 2011-03-03
Posts: 3

Re: Add aspect ratio as suffix to image files

Wow! Thank you everyone for your help.

Thanks for the script advice and for the additional metatag - all really appreciated.

Regards, Mike

Offline

Board footer

Powered by FluxBB