有没有办法从文件夹中读取图像并将其保存在powerpoint中

时间:2013-07-29 12:40:28

标签: image matlab save

我有约。 100个图像,我想读取这些图像,进行调整大小并使用matlab将其保存在功率​​点中,是否可以将这些图像保存在每个幻灯片的标题中。 我正在使用以下代码阅读图片:

for i = 1:numel(pngfiles)
   im{i} = imread(pngfiles{i});
  imrgb{i} = rgb2gray(im{i});
   imrgb_z{i} = imrgb{i}(160:350,280:450);
end

3 个答案:

答案 0 :(得分:4)

在我看来,最好的方法是在Powerpoint中使用VBA脚本,而不是从Matlab操作ppt。步骤将是

  1. 使用合理的命名方案
  2. 在文件夹中创建图像列表
  3. 开放Powerpoint;转到VBA编辑器(Alt-F11)并添加一个包含以下代码行的模块(注意 - 这是直接从https://stackoverflow.com/a/5038907/1967396直接进行编辑):
  4. -

    Sub CreatePictureSlideshow( )
      Dim presentation
      Dim layout
      Dim slide
    
      Dim FSO
      Dim folder
      Dim file
      Dim folderName
      Dim fileType
    
      ' Set this to point at the folder you wish to import JPGs from
      ' Note: make sure this ends with a backslash \
      fileType = ".jpg"                ' <<< change this to the type you want
      folderName = "c:\somedirectory\" ' <<< change this to the directory you want
    
      ' setup variables
      Set presentation = Application.ActivePresentation
      ' choose the layout you want: e.g. if the title needs a particular format
      Set layout = Application.ActivePresentation.SlideMaster.CustomLayouts(1) 
      Set FSO = CreateObject("Scripting.FileSystemObject")
    
      ' Retrieve the folder's file listing and process each file
      Set folder = FSO.GetFolder(folderName)
      For Each file In folder.Files
    
         ' Filter to only process JPG images
         If LCase(Right(file.Name), 4)) = fileType Then
    
            ' Create the new slide and delete any pre-existing contents
            Set slide = presentation.Slides.AddSlide(presentation.Slides.count + 1, layout)
            While slide.Shapes.count > 0
              slide.Shapes(1).Delete  ' <<< You might not want to do this is you want to keep the title placeholder
            Wend
    
            ' Add the picture
            slide.Shapes.AddPicture folderName + file.Name, False, True, 10, 10
    
    
            ' Optional: create a textbox with the filename on the slide for reference
            ' alternatively, add text to the title shape
    
            Dim textBox
            Set textBox = slide.Shapes.AddTextbox(msoTextOrientationHorizontal, 10, 10, 200, 200)
            textBox.TextFrame.TextRange.Text = file.Name ' <<< or whatever "title" you wanted
         End If
      Next
    
    End Sub
    

    您可以进一步修改,以获得所需格式的标题等。

答案 1 :(得分:2)

你可以试试这个:

Is there an example of using MATLAB to create PowerPoint slides?

例如:

% before the following, you have to create the ppt as explained, see link above!
% I prefer using some name instead of i or j
for img_ind = 1:numel(pngfiles)
    % this depends on the ppt-version (see link above)-> here for 2007 and higher
    mySlide = Presentation.Slides.Add(1,'ppLayoutBlank')
    % Note: Change the image file full path names to where you save them
    Image1 = mySlide.Shapes.AddPicture('<full path>\name_of_image(img_ind).png','msoFalse','msoTrue',100,20,500,500)      
end
% then you have to save it, see link above!

在您的情况下,我猜您必须先保存图像,如示例所示:

print('-dpng','-r150','<full path>\test1.png')

修改

这仅适用于在Windows上使用Matlab,因为需要COM。看看有关Floris回答的评论!

答案 2 :(得分:1)

这个聚会迟到了:这是本周的Matlab选择&#34;工具:

http://www.mathworks.com/matlabcentral/fileexchange/30124-smart-powerpoint-exporter

请注意该页面上的一些评论,因为显然该工具未在几年内更新。