Hi! Whenever I want to change the size of a JPEG I use the Adjust Size command from the Tools menu. I uncheck the box for “Scale Proportionally” and enter whatever dimensions I want for Width and Height in pixels.
I’m trying to work this into a script. However, I can’t find these items in the Preview Script Dictionary. I also can’t seem to find out how to do this in Image Events because I don’t want to crop, pad, or letterbox the pic, I just want to change the size just like I’m doing in Preview.
I don’t want to scale by size because I want independent control over height and width. I think the only way to do this is to use the sips in terminal in a shell script.
According to Sips man, I assume that you must use this function :
-z pixelsH pixelsW
–resampleHeightWidth pixelsH pixelsW
Resample image at specified size. Image apsect ratio may be
altered
Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) mardi 29 septembre 2015 10:31:46
Assuming you’re using 10.9 or later, you can use this:
use scripting additions
use framework "Foundation"
use framework "AppKit"
on jpegFromPath:imagePath toPath:newPath newWidth:theWidth newHeight:theHeight compressFactor:compFactor
set theImage to current application's NSImage's alloc()'s initWithContentsOfFile:imagePath -- load the file as an NSImage
set newImage to current application's NSImage's alloc()'s initWithSize:(current application's NSMakeSize(theWidth, theHeight)) -- make new blank image
-- draw from original to new
newImage's lockFocus()
theImage's drawInRect:{origin:{x:0, y:0}, |size|:{width:theWidth, height:theHeight}} fromRect:(current application's NSZeroRect) operation:(current application's NSCompositeSourceOver) fraction:1.0
newImage's unlockFocus()
set theData to newImage's TIFFRepresentation() -- get bitmap as data
set newRep to current application's NSBitmapImageRep's imageRepWithData:theData -- make bitmap from data
set theData to (newRep's representationUsingType:(current application's NSJPEGFileType) |properties|:{NSImageCompressionFactor:compFactor, NSImageProgressive:false}) -- turn the bitmap into JPEG data
(theData's writeToFile:newPath atomically:true) -- write it to disk
end jpegFromPath:toPath:newWidth:newHeight:compressFactor:
For such task, wouldn’t it be better to save as a Tiff or a Png so that complementary changes may be applied without the loss due to every jpeg decompression-compression process ?
When I do such things manually, I begin always by a conversion from jpeg to Tiff or Png.
I make the wanted changes and if it’s really required, I convert this very last result to Jpeg.
In fact, from time to time, I have no alternative because, I don’t know why ,but my HP OfficeJet Pro 8615 refuse to create jpeg when I use it to scan documents. It creates only PDF files.
So I edited one of your scripts to convert the PDFs into PNG files.
As it may be used with other image format as source, I post it here.
use scripting additions
use framework "Foundation"
use framework "AppKit"
on pngFromPath:imagePath destFormat:theFormat
-- in case of an alias or an Hfs+ path
if (class of imagePath is alias) or (imagePath contains ":") then set imagePath to POSIX path of imagePath
-- build destination path
set pathNSString to current application's NSString's stringWithString:imagePath
set destPath to pathNSString's stringByDeletingPathExtension()'s stringByAppendingPathExtension:theFormat
-- load the file as an NSImage
set theImage to current application's NSImage's alloc()'s initWithContentsOfFile:imagePath
--log theImage
if theImage = missing value then return false
-- get bitmap as data
set theData to theImage's TIFFRepresentation()
-- make imagerep from data
set newRep to current application's NSBitmapImageRep's imageRepWithData:theData
if theFormat is "tiff" then
-- turn the imagerep into Tiff data
set theData to (newRep's representationUsingType:(current application's NSTIFFFileType) |properties|:{NSTIFFCompression:1})
else if theFormat is "png" then
-- turn the imagerep into PNG data
set theData to (newRep's representationUsingType:(current application's NSPNGFileType) |properties|:{NSImageInterlaced:false})
else
error "The asked format "" & theFormat & "" is not recognized."
end if
-- write it to disk
set theResult to (theData's writeToFile:destPath atomically:true)
return (theResult = 1)
end pngFromPath:destFormat:
its pngFromPath:((choose file) as text) destFormat:"tiff"
I don’t remember if it’s for such conversion that you wrote once that the part:
|properties|:{NSTIFFCompressionNone:1}
is useless. Now I know the answer
Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) mardi 29 septembre 2015 11:58:40