Saturday, July 4, 2020

#1 2020-05-03 01:20:50 am

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 642
Website

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 ? roll

Offline

 

#2 2020-05-03 05:59:32 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

Re: Get uncompressed picture size

Is this what you ask for ?

Applescript:

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

Offline

 

#3 2020-05-03 07:38:19 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 530

Re: Get uncompressed picture size

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/bas … photoshop/

If so, with Image Events:

Applescript:

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

Last edited by peavine (2020-05-03 08:15:51 am)


2018 Mac mini - macOS Catalina

Offline

 

#4 2020-05-03 08:24:52 am

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 642
Website

Re: Get uncompressed picture size

@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)

Offline

 

#5 2020-05-03 08:31:58 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

Re: Get uncompressed picture size

peavine wrote:

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/bas … photoshop/

If so, with Image Events:

Applescript:

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



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

Offline

 

#6 2020-05-03 08:49:10 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 530

Re: Get uncompressed picture size

Yvan Koenig wrote:

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



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.

Applescript:


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

Last edited by peavine (2020-05-03 08:50:02 am)


2018 Mac mini - macOS Catalina

Offline

 

#7 2020-05-03 09:08:55 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

Re: Get uncompressed picture size

Joy wrote:

@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)



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 :

Applescript:

# 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

Last edited by Yvan Koenig (2020-05-03 09:30:11 am)

Offline

 

#8 2020-05-03 09:47:56 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: Get uncompressed picture size

Yvan Koenig wrote:


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


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:

Applescript:


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

or,

Applescript:


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

these are just my considerations, not confirmed by tests... roll

Last edited by KniazidisR (2020-05-03 10:29:24 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#9 2020-05-03 10:31:03 am

Joy
Member
From:: South Tirol
Registered: 2008-07-04
Posts: 642
Website

Re: Get uncompressed picture size

@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

Offline

 

#10 2020-05-03 10:51:04 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

Re: Get uncompressed picture size

KniazidisR wrote:
Yvan Koenig wrote:


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


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:

Applescript:


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

or,

Applescript:


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

these are just my considerations, not confirmed by tests... roll



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

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



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

Offline

 

#11 2020-05-03 12:15:38 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: Get uncompressed picture size

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

Last edited by KniazidisR (2020-05-03 12:25:09 pm)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#12 2020-05-03 12:50:02 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

Re: Get uncompressed picture size

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

Offline

 

#13 2020-05-03 05:59:51 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6395

Re: Get uncompressed picture size

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:

Applescript:

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:

Applescript:

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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)