Wednesday, July 17, 2019

#1 2019-06-17 04:26:23 am

CWilcke
Member
Registered: 2012-06-14
Posts: 7

Change resolution of images not the size

Hi,

is it possible to just change the resolution of an image - not to change the artboardor the image size itself by applescript?

Thank you!
Christoph


Filed under: applescript, size, resolution

Offline

 

#2 2019-06-17 10:31:29 pm

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

Re: Change resolution of images not the size

The most lightweight solution:

1) Download and install on your machine command-line tool imagemagick
2) Put  it at applications folder
3) Then try script similar to following:

Applescript:


do shell script "/Applications/imagemagick convert -units PixelsPerInch -density 600 inputFile.png outputFile.png"

I have not tried this utility. You may have to play around with the settings. As for image processing by AppleScript itself, the answer is: it is not intended for this. The whole idea of AppleScript is scripting other applications. And AppleScript itself has only... 5 commands, all other commands are add-ons and extensions (which you can write yourself)!

Last edited by KniazidisR (2019-06-17 10:44:54 pm)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#3 2019-06-18 01:21:18 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4907

Re: Change resolution of images not the size

There's also Image Events, a faceless AppleScriptable application included with macOS which lives in /System/Library/CoreServices. Its dictionary can easily be opened in Script Editor (File -> Open Dictionary…).

Edit: Example script removed after peavine's observation below that 'resolution' is read-only in Image Events!

Last edited by Nigel Garvey (2019-06-19 01:24:39 am)


NG

Online

 

#4 2019-06-18 04:00:07 am

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

Re: Change resolution of images not the size

Nigel Garvey wrote:

There's also Image Events, a faceless AppleScriptable application included with macOS which lives in /System/Library/CoreServices. Its dictionary can easily be opened in Script Editor (File -> Open Dictionary…). Illustrative (rather than working) code as follows:



Very well. I did not know about it, may be useful in the future.


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#5 2019-06-18 02:20:51 pm

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

Re: Change resolution of images not the size

Just a small addition to Nigel's post. In Image Events, resolution is read only and cannot be used to change the resolution of an image.

Last edited by peavine (2019-06-22 08:05:04 am)

Offline

 

#6 2019-06-18 04:40:43 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4907

Re: Change resolution of images not the size

peavine wrote:

Just a small addition to Nigel's post. In Image Events, resolution is read only and cannot be used to change the resolution of an image.


Thanks for finding that, peavine. I missed the all-important little "r/o" this morning! Sorry if I've wasted anyone's time.  sad


NG

Online

 

#7 2019-06-19 06:53:34 pm

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

Re: Change resolution of images not the size

Sips is a macOS command-line utility that can be used in an AppleScript to change the resolution of an image. By way of example,

Applescript:

do shell script "sips -s dpiWidth 72 -s dpiHeight 72 /Users/peavine/in.png --out /Users/peavine/out.png"

I ran this on a test PNG image and, as expected, it changed the resolution (dpi width and height) but not the size (pixel width and height) of the test image.

Last edited by peavine (2019-06-22 09:07:10 am)

Offline

 

#8 2019-07-03 11:12:01 pm

CWilcke
Member
Registered: 2012-06-14
Posts: 7

Re: Change resolution of images not the size

Hi,

thanks so far - can I build a Applescript like this to do a shell script?

set theFolder to POSIX path of (choose folder with prompt "Choose folder")
set theFolderList to list folder theFolder without invisibles

repeat with x from 1 to count of theFolderList
    set theFile to theFolder & item x of theFolderList
    set theNewFile to theFolder & theFile
    try
        do shell script "sips -s dpiWidth 250 -s dpiHeight 250"
    end try
   
   
end repeat

Somehow it seems not to work…

Thanks for all ur help!

Offline

 

#9 2019-07-04 12:37:47 am

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

Re: Change resolution of images not the size

Applescript:

-- CHOOSE SOURCE FOLDER
set theFolder to choose folder with prompt "CHOOSE FOLDER"

-- MAKE NEW DESTINATION FOLDER ON DESKTOP
do shell script "mkdir -p ~/Desktop/" & quoted form of "ImagesFolder"
set DestinationFolder to POSIX path of (((path to desktop) as string) & "ImagesFolder")

tell application "System Events"
   -- GET ALL FILES OF SOURCE FOLDER
   set theImages to file of theFolder
   
   -- PROCESSING PNG AND JPG IMAGES ONLY
   repeat with theImage in theImages
       if type identifier of theImage is in {"public.jpg", "public.png"} then
           do shell script "sips -s dpiWidth 72 -s dpiHeight 72 " & (POSIX path of theImage) & " --out " & DestinationFolder & "/" & (name of theImage)
       end if
   end repeat
end tell

Last edited by KniazidisR (2019-07-04 05:38:17 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#10 2019-07-04 12:58:38 am

CWilcke
Member
Registered: 2012-06-14
Posts: 7

Re: Change resolution of images not the size

thank u - does it work with png files only? I do have several jpg also

KniazidisR wrote:

Applescript:

-- CHOOSE SOURCE FOLDER
set theFolder to choose folder with prompt "CHOOSE FOLDER"

-- MAKE NEW DESTINATION FOLDER ON DESKTOP
do shell script "mkdir -p ~/Desktop/" & quoted form of "ImagesFolder"
set DestinationFolder to POSIX path of (((path to desktop) as string) & "ImagesFolder")

tell application "System Events"
   -- GET ALL FILES OF SOURCE FOLDER
   set theImages to file of theFolder
   
   -- PROCESSING PNG IMAGES ONLY
   repeat with theImage in theImages
       if (type identifier of theImage is "public.png") then
           do shell script "sips -s dpiWidth 72 -s dpiHeight 72 " & (POSIX path of theImage) & " --out " & DestinationFolder & "/" & (name of theImage)
       end if
   end repeat
end tell

Offline

 

#11 2019-07-04 05:40:34 am

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

Re: Change resolution of images not the size

CWilcke wrote:

thank u - does it work with png files only? I do have several jpg also



OK, I  updated script to process jpg images too.


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#12 2019-07-04 08:19:36 am

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

Re: Change resolution of images not the size

KniazidisR has answered the OP's question but I thought I would provide a different (but not necessarily better) approach. The sips command-line utility supports wild cards and this can be simply implemented as follows:

Applescript:

set inFolder to quoted form of POSIX path of (choose folder)

set outFolder to quoted form of POSIX path of (path to desktop)

repeat with anExtension in {"png", "jpg"}
   try
       do shell script "sips -s dpiWidth 72 -s dpiHeight 72 " & inFolder & "*." & (contents of anExtension) & " --out " & outFolder
   end try
end repeat

The outFolder variable needs to be revised to whatever fits the OP's needs.

Last edited by peavine (2019-07-04 06:28:24 pm)

Offline

 

#13 2019-07-04 11:15:23 pm

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

Re: Change resolution of images not the size

To be pedantic, all these solutions don't actually change the resolution of an image, but rather produce new images with the new resolution. The likely loss of metadata may not matter, but in the case of jpegs it does introduce a new sampling stage, which can lead to image degradation.

It looks like sips defaults to high for jpeg formatOptions. In some cases that might actually lead to files becoming much bigger.


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

Offline

 

#14 2019-07-05 09:38:19 am

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

Re: Change resolution of images not the size

Shane Stanley wrote:

To be pedantic, all these solutions don't actually change the resolution of an image, but rather produce new images with the new resolution. The likely loss of metadata may not matter, but in the case of jpegs it does introduce a new sampling stage, which can lead to image degradation.

It looks like sips defaults to high for jpeg formatOptions. In some cases that might actually lead to files becoming much bigger.



Shane.

The second line of my script can be changed as follows and a new file will not be created:

set outFolder to inFolder

A temporary file might be created before overwriting the original and I agree this could lead to some loss of image quality of a JPG file.

If image quality of a JPG image is a concern, the OP can use ExifTool, a simple example of which is:

exiftool -xresolution=72 -yresolution=72 "/Users/peavine/Downloads/test.jpg"

According to the author of this utility:

"ExifTool does not modify the image data itself, so editing a file with ExifTool is "lossless" as far as the image is concerned."

In my tests with ExifTool and JPG images, the file sizes before and after modifying the resolution were essentially identical.

Last edited by peavine (2019-07-05 05:23:35 pm)

Offline

 

#15 2019-07-05 05:36:39 pm

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

Re: Change resolution of images not the size

peavine wrote:

The second line of my script can be changed as follows and a new file will not be created:



Well it is really -- it's just replacing the old one. If you start with a png from a screen capture, for example, you will see the size change. You will also see some of the metadata associated with it disappears.

In my tests with ExifTool and JPG images, the file sizes before and after modifying the resolution were essentially identical.



Yes, that's the sort of thing I mean. It sounds like it's editing the file rather than replacing it.


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

Offline

 

#16 2019-07-05 06:43:43 pm

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

Re: Change resolution of images not the size

Shane Stanley wrote:
peavine wrote:

The second line of my script can be changed as follows and a new file will not be created:



Well it is really -- it's just replacing the old one. If you start with a png from a screen capture, for example, you will see the size change. You will also see some of the metadata associated with it disappears.

In my tests with ExifTool and JPG images, the file sizes before and after modifying the resolution were essentially identical.



Yes, that's the sort of thing I mean. It sounds like it's editing the file rather than replacing it.



Shane. If possible changes in metadata is a concern, then the OP can use ExifTool to change the resolution of PNG images as well. A sample command for PNG images is:

exiftool -pixelsperunitx=2835 -pixelsperunity=2835 "/Users/peavine/Downloads/test.png"

BTW, the units are meters, which is the reason for the odd numbers. Also, I ran some tests and there is no change in image size with ExifTool and PNG images.

I guess in retrospect, ExifTool would have been a better choice to begin with, but it's another utility to install and take up space (a free one though) when sips would probably do the job. I guess the OP can make that choice.

Last edited by peavine (2019-07-05 06:51:41 pm)

Offline

 

#17 2019-07-05 07:42:59 pm

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

Re: Change resolution of images not the size

I suspect there's sometimes stuff other than simple metadata, such as color info.

I've used ExifTool for changing resolution myself, but was hesitant to suggest it, mainly for the reasons you mention. I kept hoping there was another way.


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

Offline

 

#18 2019-07-06 09:45:27 am

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

Re: Change resolution of images not the size

FWIW, I wrote a basic script using ExifTool to change the resolution of JPG and PNG image files. This script creates resolution-modified copies of the source files in a destination folder, which the script creates.

Applescript:

property folderNumber : 1

set exifUtility to "/usr/local/bin/exiftool"

set jpgResolution to 72
set pngResolution to jpgResolution * 39.3701

set inFolder to quoted form of POSIX path of (choose folder)

set outFolder to ((POSIX path of (path to desktop)) & "ExifTool Images " & folderNumber & "/")

try
   do shell script exifUtility & " -ext png " & inFolder & " -o " & quoted form of outFolder & ¬
       " -pixelsperunitx=" & pngResolution & " -pixelsperunity=" & pngResolution
   do shell script exifUtility & " -ext jpg " & inFolder & " -o " & quoted form of outFolder & ¬
       " -xresolution=" & jpgResolution & " -yresolution=" & jpgResolution
on error
   set dialogText to "ExifTool reported an error. A destination folder or file may already exist."
   display dialog dialogText buttons {"OK"} default button 1 cancel button 1 with icon stop
end try

try
   alias (POSIX file outFolder)
   set folderNumber to folderNumber + 1
end try

Last edited by peavine (2019-07-07 12:47:53 pm)

Offline

 

#19 2019-07-07 11:30:18 pm

CWilcke
Member
Registered: 2012-06-14
Posts: 7

Re: Change resolution of images not the size

Perfect! Exactly what I needed! Great! Thank u!!!

big_smile

peavine wrote:

FWIW, I wrote a basic script using ExifTool to change the resolution of JPG and PNG image files. This script creates resolution-modified copies of the source files in a destination folder, which the script creates.

Applescript:

property folderNumber : 1

set exifUtility to "/usr/local/bin/exiftool"

set jpgResolution to 72
set pngResolution to jpgResolution * 39.3701

set inFolder to quoted form of POSIX path of (choose folder)

set outFolder to ((POSIX path of (path to desktop)) & "ExifTool Images " & folderNumber & "/")

try
   do shell script exifUtility & " -ext png " & inFolder & " -o " & quoted form of outFolder & ¬
       " -pixelsperunitx=" & pngResolution & " -pixelsperunity=" & pngResolution
   do shell script exifUtility & " -ext jpg " & inFolder & " -o " & quoted form of outFolder & ¬
       " -xresolution=" & jpgResolution & " -yresolution=" & jpgResolution
on error
   set dialogText to "ExifTool reported an error. A destination folder or file may already exist."
   display dialog dialogText buttons {"OK"} default button 1 cancel button 1 with icon stop
end try

try
   alias (POSIX file outFolder)
   set folderNumber to folderNumber + 1
end try

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)