Get uncompressed picture size

This days I need to get the uncompressed file size info of a thousand of picture files, usually *.jpg files. To do so I’ve to open these images in an image editor and check their uncompressed file size one by one… A real pain.

With uncompressed i refer to the real file size of pictures when they are open in an image editor. This info is displayed, most of the time in the status bar of image editors
I guess such particular info could be available, ideally using a scriptable app, like acorn or Photoshop. But how to do in absence of these apps or, in absence of apples riot commands ? :rolleyes:

Is this what you ask for ?

use AppleScript version "2.5" -- 10.11 or later
use framework "Foundation"
use scripting additions

set aFile to choose file of type {"public.jpeg"}

set theNSURL to current application's NSURL's fileURLWithPath:(POSIX path of aFile)
set nsMetaItem to current application's NSMetadataItem's alloc()'s initWithURL:theNSURL
# Build a list of every available metadatas
set theMetadata to nsMetaItem's valuesForAttributes:(nsMetaItem's attributes())
# Duplicate the dictionary in a mutable one from which we may remove items.
set theKey to "kMDItemPixelCount"
set pixelCount to (nsMetaItem's valueForAttribute:theKey) as string

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 3 mai 2020 13:59:22

Yvan. I tested your script on a JPG image and the number it returns is the pixel width multiplied by the pixel height. Does this number need to be multiplied by 3 for RGB color to get the total image size?

https://www.photoshopessentials.com/basics/how-to-calculate-image-size-in-photoshop/

If so, with Image Events:

set theFile to (choose file) as text

tell application "Image Events"
	launch
	set theImage to open file theFile
	set {imageWidth, imageHeight} to dimensions of theImage
	close theImage
end tell

set imageSize to imageWidth * imageHeight * 3

@Yvan
Nice script,
however I was referring to megabytes, and to illustrate you an example do following :

Pick a picture in Finder, with a file size of approximately 3 MB. Open this image in Gimp / Acorn / Photoshop or whatever image editor you use.

Check the status bar of the open image in your image editor and you’ll see the size in megabytes increased by multifold, to a size of 18 mb.
That is the true file size of a loaded picture file. A file in the Finder is not open so it eats much less mb.

It is the real file size in megabytes which I need to know (this file size is probably catched in ram)

What is returned by my script is what is displayed as picture size in Gimp.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 3 mai 2020 16:31:11

Thanks Yvan. I don’t have an image editor installed which shows image size So, I have modified my script as shown below. On my test image, it returns the exact same figure as your script.


set theFile to (choose file) as text

tell application "Image Events"
   launch
   set theImage to open file theFile
   set {imageWidth, imageHeight} to dimensions of theImage
   close theImage
end tell

set imageSize to imageWidth * imageHeight

For thefile used to test Gimp returned:

Propriétés de l‘image
Taille en pixels : 1280 x 720 pixels
Taille de l‘Impresslon : 451.6 x 254.0 millimètres
Résolution : 72 x 72 ppp
Espace de couleurs : Couleur RVB
Précision : Entier 8 bits gamma
Nom de fichier : fxxxx.jpg
Taille de fichier : 125.2 kB
Type de fichier : Image JPEG
Taille en mémoire : 8.6 MB
Etapes d’annulation : Aucun
Etapes à rétablir : Aucun
Nombre de pixels : 921600
Nombre de calques : 1
Nombre de canaux : 0

As far as I know, the “size in memory” is linked to the application used to open the file.

I opened the same file with Acorn, the unique info I saw is the count of pixels which is what is returned by the script.

Here is a script which may give useful info : the size of the pictured expanded according to the used format :

# borrowed from : http://forum.latenightsw.com/t/how-do-i-copy-image-file-to-clipboard-and-retain-format/590/9

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set posixPath to POSIX path of (choose file of type {"public.image"})
set {theData, theError} to current application's NSData's dataWithContentsOfFile:posixPath options:0 |error|:(reference)
set theClip to current application's NSPasteboard's generalPasteboard()
theClip's clearContents()
theClip's setData:theData forType:"public.png"
set theType to theClip's (pasteboardItems()'s objectAtIndex:0)'s types() as list
log theType (*public.png*)
set theTypes to theClip's types() as list
clipboard info
# The compfactor applied to jpeg is ≃ 0.xx
--> {{«class PNGf», 125187}, {«class 8BPS», 2690903}, {GIF picture, 475892}, {«class jp2 », 187960}, {JPEG picture, 161870}, {TIFF picture, 2768930}, {«class BMP », 2764854}, {«class TPIC», 2519824}}

set theData to theClip's dataForType:"public.tiff" -- get tiff data off pasteboard
if theData = missing value then error "No tiff data found on clipboard"
set newRep to current application's NSBitmapImageRep's imageRepWithData:theData
set compfactor to 1.0
set theData to (newRep's representationUsingType:(current application's NSJPEGFileType) |properties|:{NSImageCompressionFactor:compfactor, NSImageProgressive:false})
theClip's setData:theData forType:"public.jpeg"
set theType to theClip's (pasteboardItems()'s objectAtIndex:0)'s types() as list
log theType (*public.png, public.jpeg*)
set theTypes to theClip's types() as list
log theTypes (*public.png, Apple PNG pasteboard type, public.jpeg, CorePasteboardFlavorType 0x4A504547, public.tiff, NeXT TIFF v4.0 pasteboard type*)
clipboard info

# with compfactor = 1.0
--> {{«class PNGf», 125187}, {JPEG picture, 614536}, {«class 8BPS», 2690903}, {GIF picture, 475892}, {«class jp2 », 187960}, {TIFF picture, 2768930}, {«class BMP », 2764854}, {«class TPIC», 2519824}}

As you may see, none of them is near the space occupied reported by Gimp.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 3 mai 2020 17:08:48

I opened the same file with 4 applications.
With Acorn, the ram used is enlarged by 61.7 Mbytes
With Affinity, the ram used is enlarged by 54.7 Mbytes
With Preview, the ram used is enlarged by 28.3 Mbytes
With Gimp, the ram used is enlarged by 15.3 Mbytes

Nothing resemble to what is reported by Gimp : 8.6 MB

1 pixel takes 3 bytes in the memory. So, multiplying Gimp’s 921600 pixels by 3 we get 2764800 bytes which is near the TIFF picture of the clipboard. I think, additional 4130 bytes in the clipboard should be some control bytes. So, formula of uncompressed image’s size should be:


(pixels x 3) + 4130 -- of TIFF picture, biggest standard

or,


(pixels x 3) + 54 -- of BMP class, next big standard

these are just my considerations, not confirmed by tests… :rolleyes:

@all
Thanks for your generous info!
After several tests, I concluded that the size of pictures in memory is calculated by :

(Height * Width * 3) / 1024 / 1024

@KniazidisR
I didn’t add +45 because I don’t need that precision, as
I need the final sum in mb - thanks very much anyway :wink:

Alas this doesn’t match the RAM really occupied which I already posted :

What’s the real meaning of such value which is not related to the space really occupied ?
When an application opens a file, it doesn’t only expand the file into its graphical representation but it build several sets of datas which take place.

Maybe Joy may explain what is his real goal when he ask for this info.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 3 mai 2020 18:48:12

Only I do not see any status bar in Preview.app? Or do you call the Inspector window a status bar? So there are also only sizes in pixels, and no “real” size in megabytes

I completely quitted the applications and checked that they no longer used memory (thank you Activity Monitor).
Then I opened the applications one by one, asked Activity Monitor for the memory used by the app
Open the file with the app then ask Activity Monitor for the new quantity of ram occupied.
At last completely quit the application.

Acorn, before : 41.8, after : 103.5, delta : 61.7 used for the file
Affinity, before : 263.4, after : 318.1, delta : 54.7 used for the file
Preview, before : 10.3, after : 38.6, delta : 28.3 used for the file
Gimp, before : 81.6, after : 96.9, delta : 15.3 used for the file

I thought that my wording was clear:

[code]I opened the same file with 4 applications.
With Acorn, the ram used is enlarged by 61.7 Mbytes (7.17 * 8.6)
With Affinity, the ram used is enlarged by 54.7 Mbytes (6.36 * 8.6)
With Preview, the ram used is enlarged by 28.3 Mbytes (3.29 * 8.6)
With Gimp, the ram used is enlarged by 15.3 Mbytes (1.78 * 8.6)

Nothing resemble to what is reported by Gimp : 8.6 MB[/code]

I don’t guess what more info was needed.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 3 mai 2020 20:49:55

As others have said, the “file size” will depend on what extra information the application saves. For example, saving as a .tiff in Preview.app results in an RGBA file (that is, RGB plus an alpha channel), and includes a color profile. It’s probably going to keep metadata, too.

I also think you need the usual caveats about relying on metadata, although in this case there’s probably less risk because image size metadata is something the OS will recalculate for files like jpegs. (And it is going to be very fast.)

Here’s a script that gets the size of the RGB data plus color profile – about what you’d get if you saved as RGB without an alpha channel from a typical graphics app:

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set aFile to choose file of type {"public.jpeg"}
set theImage to current application's NSImage's alloc()'s initWithContentsOfURL:aFile
set tiffData to theImage's TIFFRepresentation()
set theBytes to tiffData's |length|()

And to convert it to a size string:

set sizeString to (current application's NSByteCountFormatter's stringFromByteCount:theBytes countStyle:(current application's NSByteCountFormatterCountStyleMemory)) as text