2017-09-15 11 views
1

イメージを見つけようとしていて、配列内の最初のイメージとは別のイメージを保存しようとしています。その後、それらの画像をdocxライブラリを使って単語文書にドロップします。現在、以下に試したいくつかの異なるソリューションにもかかわらず、次のエラーが発生しています。ここでは、コードです:Pyautoguiで配列イメージを保存/取得する(AttributeError: 'Image'オブジェクトに 'read'属性がありません)

import sys 
import PIL 
import pyautogui 
import docx 
import numpy 

def grab_paperclip_images(): 
    ''' 
    This'll look at the documents that're on 
    the current screen, and create images of 
    each document with a paperclip. I'll be 
    testing on an unsorted screen first. 
    ''' 
    image_array = [] 
    clip_array = find_all_objects("WHITE_PAPERCLIP.png") 
    for item in clip_array: 
     coordinates = item[0]+45, item[1], 222, item[3] 
     image_array.append(pyautogui.screenshot(region=coordinates)) 
    return image_array 

doc = docx.Document() 
images = grab_paperclip_images() 
for image in images: 
    #print image 
    #yields: [<PIL.Image.Image image mode=RGB size=222x12 at 0x7CC7770>,etc] 

    #Tried this - no dice 
    #img = PIL.Image.open(image) 
    #doc.add_picture(img) 

    doc.add_picture(image) 
doc.save("testDoc.docx") 

私が誤解だ何を知っている、とあなたはコードをよりニシキヘビにするために任意の提案を見れば、よりよいスコープください、など

はいつものように、助けのためのおかげで、誠実に!

答えて

0

これを回避する方法を考えました。イメージをディスクに保存する必要がありました。私はまだ配列を参照することができますが、私はそれを保存せずに画像を参照することができませんでした。私の回避策は次のとおりです。

def grab_paperclip_images(): 
    ''' 
    This'll look at the documents that're on 
    the current screen, and create images of 
    each document with a paperclip. I'll be 
    testing it on an unsorted screen first. 
    INSPIRATION: 
    bottom_record = pyautogui.screenshot(
     "LAST_RECORD.png", 
     region=(
      last_clip[0], 
      last_clip[1]+18, 
      1100, 
      14 
      ) 
     ) 
    ''' 
    image_array = [] 
    clip_array = find_all_objects("WHITE_PAPERCLIP.png") 
    count = 0 
    for item in clip_array: 
     coordinates = item[0]+45, item[1], 222, item[3] 
     filename = "image"+str(count)+".png" 
     image = pyautogui.screenshot(filename, region=coordinates) 
     image_array.append(filename) 
     count += 1 
    return image_array 


doc = docx.Document() 
images = grab_paperclip_images() 
for image in images: 
    doc.add_picture(image) 
doc.save("dingding2.docx") 
delete_all(images) 
関連する問題