Storing/Retrieving array images with Pyautogui (AttributeError: 'Image' object has not attribute 'read')
Storing/Retrieving array images with Pyautogui (AttributeError: 'Image' object has not attribute 'read')
我正在尝试定位一张图片,然后将另一张图片相对于第一张图片存储在数组中。之后,我希望使用 docx 库将这些图像放入 word 文档中。目前,尽管我在下面尝试了一些不同的解决方案,但我还是收到了以下错误。这是代码:
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")
请让我知道我的误解,如果您看到任何使代码更像 pythonic、范围更广等的建议,请告诉我。
一如既往,真诚地感谢您的帮助!
想出了解决这个问题的方法。我不得不将图像保存到磁盘。我仍然可以引用数组,但我无法在不保存图像的情况下引用图像。这是我的解决方法:
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)
我正在尝试定位一张图片,然后将另一张图片相对于第一张图片存储在数组中。之后,我希望使用 docx 库将这些图像放入 word 文档中。目前,尽管我在下面尝试了一些不同的解决方案,但我还是收到了以下错误。这是代码:
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")
请让我知道我的误解,如果您看到任何使代码更像 pythonic、范围更广等的建议,请告诉我。
一如既往,真诚地感谢您的帮助!
想出了解决这个问题的方法。我不得不将图像保存到磁盘。我仍然可以引用数组,但我无法在不保存图像的情况下引用图像。这是我的解决方法:
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)