Storing/Retrieving array images with Pyautogui (AttributeError: 'Image' object has not attribute 'read')

583 views Asked by At

I'm trying to locate an image, then store another image relative to the first one within an array. Afterwards, I want those images to drop into a word document using the docx library. Currently, I'm getting the following error, despite a few different solutions I've tried below. Here's the code:

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")

Please let me know what I'm misunderstanding, and if you see any suggestions to make the code more pythonic, better scoped, etc.

As always, thanks for the help, sincerely!

1

There are 1 answers

0
phfb On BEST ANSWER

Figured out a way around this. I had to save the images to disk. I could still reference the array, but I couldn't reference the image without saving it. Here's my workaround:

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)