Copy link to clipboard
Copied
I would like to know if there is a way to automate the cropping of images with oversized bounding boxes. For example, if the bounding box around an image measures 5in x 5in, but the actual image within the bounding box is only 2.50in x 2.5in, is there a way to write a script so that Illustrator could automatically crop the image down to just before the image within the box?
If this is possible, I am basing it on the fact that when an effect is applied to an object or image within a bounding box, the effect is applied to the object itself rather than the bounding box. This leads me to think that Illustrator is already capable of identifying the object, so why wouldn't it be able to crop it as well?
Copy link to clipboard
Copied
Hi @MrIHaZe, it might be possible with a script. Can you post an example document here? Save it as .pdf with Illustrator editing capability because .ai files aren't accepted by the forum software unfortunately.
- Mark
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Hi @MrIHaZe, thanks for the demo file. I'm not sure if this script does what you want, but can you try it out and let me know. Note that it doesn't alter the image placed in Illustrator, it justs crops it.
- Mark
/**
* Clips a placed item based on it's traceable area.
* @author m1b
*/
(function () {
var doc = app.activeDocument,
item = clipToTraceableArea(doc.selection[0]);
/**
* Returns a clipping group using a rectangle
* based on the traceable area of the given image.
* @param {PlacedItem|RasterItem} item - a traceable item.
* @param {Number} margin - the distance in points to add to each side of the clipping rectangle (default: 5).
* @returns {GroupItem}
*/
function clipToTraceableArea(item, margin) {
if (margin == undefined)
margin = 5;
if (item.trace == undefined)
return;
var traceThis = item.duplicate().trace();
var tracingOptions = traceThis.tracing;
tracingOptions.tracingMode = TracingModeType.TRACINGMODEBLACKANDWHITE;
tracingOptions.ignoreWhite = true;
tracingOptions.threshold = 255;
tracingOptions.pathFitting = 5.0;
tracingOptions.fills = true;
tracingOptions.strokes = false;
tracingOptions.cornerAngle = 135;
var tracedItem = tracingOptions.expandTracing();
tracedItem.pathItems[tracedItem.pathItems.length - 1].remove();
var bounds = tracedItem.visibleBounds;
tracedItem.remove();
bounds[0] -= margin;
bounds[1] += margin;
bounds[2] += margin;
bounds[3] -= margin;
// draw the clipping rectangle
var rect = doc.pathItems.rectangle(bounds[1], bounds[0], bounds[2] - bounds[0], bounds[1] - bounds[3]);
var group = doc.activeLayer.groupItems.add();
group.move(item, ElementPlacement.PLACEBEFORE);
item.move(group, ElementPlacement.PLACEATEND);
rect.move(group, ElementPlacement.PLACEATBEGINNING);
group.clipped = true;
rect.clipping = true;
if (item.selected == true) {
item.selected = false;
group.selected = true;
}
};
})();
Copy link to clipboard
Copied
Another possibility would be to open the images in photoshop and use a batch action *trim* command to remove transparent pixels from all sides.
Copy link to clipboard
Copied
MrIHaZe,
Maybe a silly question, but are you sure there is no unforeseen/unwanted circumstance such as a Transform Effect giving you a caling to 50%, or a Group with an invisible path/stray points?
The Appearance panel and the expanded Layer can be your friends, or deselecting and then ClickDragging with the Direct Selection Tool across the whole area beyound what is covered by the Bounding Box.
Copy link to clipboard
Copied
The images in question have no effect applied to them. If I were to bring the images in on a blank document the initial bounding box/image frame is already set based on the image itself.
Copy link to clipboard
Copied
MrIHaZe,
"If I were to bring the images in on a blank document the initial bounding box/image frame is already set based on the image itself"
This made me wonder whether there is some strangeness/corruption in the document.
What happens if you copy and paste the different parts of artwork into a new document?
Copy link to clipboard
Copied
Mark's script works very well. It even recognises linked files.
I have an action set that can do something similar. Either it clips or it really crops the images.
What is your preferred method?