Group Abstract Group Abstract

Message Boards Message Boards

0
|
52 Views
|
2 Replies
|
2 Total Likes
View groups...
Share
Share this post:

How to batch insert all image files in a specified folder?

Posted 1 day ago

enter image description here

Usually, we insert a small number of images in a notebook using the method shown in the figure. If there are many images to insert, what method can be used to insert all image files in a certain folder at once?

POSTED BY: Bill Blair
2 Replies

Assuming they're all of the same file extension, you can use the * character to get all the files of a given extension in a directory. For example if they were PNG images

Import[FILEPATH, "*.png"]

Be careful to note that this will extract all PNG images at any level, even within subdirectories of FILEPATH. If you only want files explicitly in FILEPATH you can append *.png to the FILEPATH:

Import[FileNameJoin[{FILEPATH, "*.png"}]]
POSTED BY: David Trimas

One idea would be to loop over the list of filenames in FileNames[All, dir] and use one of the following to write into the notebook:

CellPrint[ExpressionCell[Import[filename], "Input"]]

NotebookWrite[EvaluationNotebook[], 
 Cell[BoxData[Import[filename] // MakeBoxes[#, StandardForm] &], "Input"]]

And it does not have to be in "Input" style, but that's what the menu command does.

One could also use

CopyToClipboard[DocumentNotebook[listOfImageCells]]

where listOfImageCells is computed from looping through filenames and has the form

{ExpressionCell[img1, "Input"], ExpressionCell[img2, "Input"],…}

After copy, then paste. It should paste each cell separately if the insertion point is between cells. Maybe beware the total size of the images.

POSTED BY: Michael Rogers
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard