Hello,
I’d look for a way to slice a 7001200 pixel image (wh) into a multiple page PDF. Preview doesn’t allow tiling and keeps the stone age options from Os to Os, meanwhile smartphone do a lot more nowadays.
Hi. Do you have Photoshop and/or InDesign? PS can slice images programatically, however, it would save the resultant files as single documents. ID could be made to export a multi-page PDF by adjusting the image’s—and/or the container’s—offset, however, I’m not really sure how housing picture fragments in a multi-page PDF is useful. Could you provide more details about what you hope to do with this and on which plane you want to perform slicing?
Hi Marc,
There are more than a dozen of pictures to slice up.
I noted a typing error in my previous post, -the format is 70012000 (not 1200 ) -wh pixels
Those documents could be divided in regular intervals-
But the print dialog of Preview doesn’t allow this flexibility (To print in tiles/convert in a multi page PDF) . Also Automaker has not the necessary tools.
Yes, I had Ps and inDesign on my hd, several Os ago - no more today. The free alternatives are Scribus or Gimp - I’ll try. Thanks for your encouragement
Try this:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
set thePath to POSIX path of (choose file)
set thePath to current application's NSString's stringWithString:thePath
set destPath to thePath's stringByDeletingPathExtension()'s stringByAppendingPathExtension:"pdf"
set destURL to current application's |NSURL|'s fileURLWithPath:destPath
-- set up print info
set printInfo to current application's NSPrintInfo's alloc()'s initWithDictionary:(current application's NSDictionary's dictionaryWithObject:destURL forKey:(current application's NSPrintJobSavingURL)) -- sets destination
printInfo's setJobDisposition:(current application's NSPrintSaveJob) -- save to file job
printInfo's setHorizontalPagination:(current application's NSAutoPagination)
printInfo's setVerticalPagination:(current application's NSAutoPagination)
-- get page size and margins
set pageSize to printInfo's paperSize()
set theLeft to printInfo's leftMargin()
set theRight to printInfo's rightMargin()
set theTop to printInfo's topMargin()
-- calculate width to fit on page; change to suit
set newWidth to (pageSize's width) - theLeft - theRight
-- get image and put in image view
set theImage to current application's NSImage's alloc()'s initWithContentsOfFile:thePath
set theSize to theImage's |size|()
set theView to current application's NSImageView's alloc()'s initWithFrame:{{0, 0}, {newWidth, newWidth / (width of theSize) * (height of theSize)}}
theView's setImage:theImage
-- create print operation and run it
set printOp to current application's NSPrintOperation's printOperationWithView:theView printInfo:printInfo
printOp's setShowsPrintPanel:false
printOp's setShowsProgressPanel:false
if current application's NSThread's isMainThread() then
set my theResult to printOp's runOperation()
else
my performSelectorOnMainThread:"runPrintOperation:" withObject:printOp waitUntilDone:true
end if
on runPrintOperation:printOp -- on main thread
set my theResult to printOp's runOperation()
end runPrintOperation:
Hi, Shane. Your code created a nice single page PDF by shrinking my test image, but there doesn’t appear to be a routine for slicing; i.e. segmenting the image in increments.
My understanding is that the OP wants something along these lines, but without the dependency and in a composite document; I accommodated both planes:
set exportFolder to (path to desktop as text) & "test:"
global exportFolder
property divisor : 5 --slice count
tell application "Finder" to try
make new folder at desktop with properties {name:"test"}
end try
tell (choose from list {"vertical", "horizontal"}) to if not it = false then tell its item 1 to try
if it is "horizontal" then
my sliceOnTheHorizontalPlane()
else
if it is "vertical" then my SliceOnTheVerticalPlane()
end if
end try
on SliceOnTheVerticalPlane()
tell application "Adobe Photoshop CS3"
tell document 1
set {leftmost, topmost, docWidth, docHeight} to {0, 0, width, height}
set decrement to docWidth / divisor
set docWidth to decrement
set counter to 1
set HistoryState to current history state
repeat divisor times
save (crop it bounds {leftmost, topmost, docWidth, docHeight}) in (exportFolder & "image" & counter & ".pdf") as Photoshop PDF copying 1
set current history state to HistoryState
set counter to counter + 1
set leftmost to leftmost + decrement
set docWidth to docWidth + decrement
end repeat
end tell
end tell
end SliceOnTheVerticalPlane
on sliceOnTheHorizontalPlane()
tell application "Adobe Photoshop CS3"
tell document 1
set {leftmost, topmost, docWidth, docHeight} to {0, 0, width, height}
set decrement to docHeight / divisor
set docHeight to decrement
set counter to 1
set HistoryState to current history state
repeat divisor times
save (crop it bounds {leftmost, topmost, docWidth, docHeight}) in (exportFolder & "image" & counter & ".pdf") as Photoshop PDF copying 1
set current history state to HistoryState
set counter to counter + 1
set topmost to topmost + decrement
set docHeight to docHeight + decrement
end repeat
end tell
end tell
end sliceOnTheHorizontalPlane
It will if the image is much deeper than wide (Joy’s 700 x 12000).
Essentially it’s scaling to fit the width exactly to the printing width of a page, and then printing that to as many pages as required vertically. If you instead set the image view’s size to the size of the image, the image will be sliced both vertically and horizontally (if it’s big enough to require it).
Oops. My test image was the wrong orientation, and I’m still not versant enough with ASObjC to grok the events through a casual read. Using the correct aspect, I can see that your solution actually does tile; it’s a neat approach for generating multiple pages, however, it inexplicably adds an extra page at the end of my test image. Your solution also adds inches of padding around all image sides. Are you able to try my code? I know multiple files are not desired—however, my method specifies the segment count, resulting in an edge-to-edge image. I can’t be sure which outcome Joy wants.
You can change the margins – they’re just your printing defaults as it stands. So you could do:
printInfo's setLeftMargin:0
And so on for the other margins.
All the code is doing is what happens when you press print and choose to save as a PDF in an app that supports tiling. Unfortunately that also sometimes adds an extra page at the end.
I can (using a more recent version), but honestly I didn’t because I gather the OP doesn’t have Photoshop. I’ve written and used similar code in the past, so I know that approach works. And it could also be done using sips, and even image cropping in ASObjC.
It just seemed to me that there was a much faster app-free way that provided what I guessed Joy was after.