Extract pixels from files and calculate it into cm

Javascript renaming examples. Javascript renaming is supported in version 3 or newer.

Extract pixels from files and calculate it into cm

Postby ktvmd » Sat Feb 08, 2025 2:11 pm

Hello guys. I'm not into js and I have one question. Is possible to extract properties from the file?

I want to rename:
example_file.tif
something else.tif

into

01_newName_10x10cm
02_newName_42x4cm
etc.

.tif files contain needed properties. Height and width in pixels and DPI. So it's possible to calculate it into centimeters by this formula
Centimeters = (Pixels/DPI)×2.54
ktvmd
 
Posts: 2
Joined: Sat Feb 08, 2025 1:46 pm

Add _WidthxHeight in centimeters

Postby Luuk » Sun Feb 09, 2025 11:41 am

To suffix _WidthxHeightcm, Special(14) "Javascript" could use something like...

// Add rounded WidthxHeight in centimeters to image-files having a "Width" attribute
if (fileProperty('Width')) {
wide=Math.round(filePropertyNum('Width')/filePropertyNum('System.Image.HorizontalResolution')*2.54)
high=Math.round(filePropertyNum('Height')/filePropertyNum('System.Image.VerticalResolution')*2.54)

// Test image for bad _WidthxHeight at end, to replace with good _WidthxHeight
test = new RegExp("_" + wide + "x" + high + "cm" + "$" | "_" + wide + "x" + high + "$")
if (!name.match(test)) {
newName = name.replace(/^(.+)_\d+x\d+(cm)?$/, '$1') + "_" + wide + "x" + high + "cm"
}
}

If names already end with the correct _WidthxHeightcm (rounded), they wont get renamed.
If names end with the correct _WidthxHeight but without "cm", then only "cm" gets appended.
Any bad _WidthxHeightcm will get replaced with good-ones (in case someone edits the graphics)?


--------------------------------------------------------------------------------------------------------------------------------
If whole filenames can always be destroyed like the samples, use Numbering(10) with Mode==None, Pad==2
Then javascript could use something like...

if (fileProperty('Width')) {
wide=Math.round(filePropertyNum('Width')/filePropertyNum('System.Image.HorizontalResolution')*2.54)
high=Math.round(filePropertyNum('Height')/filePropertyNum('System.Image.VerticalResolution')*2.54)
newName = object("autonumber") + "_newName_" + wide + "x" + high + "cm"
}

If you have any non-image files, the Filters(12) "Condition" should have something like... fileProperty('Width')
Then to click the blue refresh-arrows, so that your numbering will only advance with true image files.
Luuk
 
Posts: 797
Joined: Fri Feb 21, 2020 10:58 pm

Re: Extract pixels from files and calculate it into cm

Postby ktvmd » Mon Feb 10, 2025 12:02 am

Thank you!

Does these functions are built in javascript language or Bulk Rename Utility only? I mean these parts: filePropertyNum and System.Image.VerticalResolution

Where I can find more about properties that can be read from file?
ktvmd
 
Posts: 2
Joined: Sat Feb 08, 2025 1:46 pm

Add _WidthxHeight in centimeters

Postby Luuk » Mon Feb 10, 2025 4:56 am

The fileProperty(), filePropertyNum(), and filePropertyDate() are all BRU functions.
They just tell BRU how to format some 'windows-file-property' inside of its parentheses.
The Math object grants methods like Math.round() and these are parts of regular javascript.

To see how many 'windows-file-properties' a file might have...
Can right-click the file, then choose "Show List of File Properties".


ALSO: I made a big mistake on the 1st-script to verify if names already have the correct dimensions in cm!!!
There should be a + symbol on each side of the "|", so there's no way it would conduct all of the images properly.
But I did think of something much simpler anyway...

// Add rounded WidthxHeight in centimeters to image-files having a "Width" attribute
if (fileProperty('Width')) {
wide=Math.round(filePropertyNum('Width')/filePropertyNum('System.Image.HorizontalResolution')*2.54)
high=Math.round(filePropertyNum('Height')/filePropertyNum('System.Image.VerticalResolution')*2.54)
newName = name.replace(/^(.+)_\d+x\d+(cm)?$/, '$1') + "_" + wide + "x" + high + "cm"
}

This one just removes all _NumbersxNumberscm at the very end, but with the "cm" part also being optional.
Then it suffixes the correct dimensions, so if any files had the correct dimensions, they wont get double-appended.
But if someone had _10x10 at the end (for inches) and wanted it that way, this replaces their _10x10 ---> _25x25cm
Luuk
 
Posts: 797
Joined: Fri Feb 21, 2020 10:58 pm


Return to Javascript Renaming