Answer the question
In order to leave comments, you need to log in
How to make a VK bot in python take a screenshot of the site, crop it and send a photo to the conversation without saving it?
My goal is to send a photo without saving it, I go to the site with requests, make a screen there using selenium, then crop it to the desired size using Pillow(PIL). Then I try to send a Post-request to VK, passing the byte representation of this screen, which I get using the tobytes function of the PIL library, which is available for the Image class, but in the Post-request that I send to VK I get a hash and a server, but the photo sheet is empty .
I did a similar trick, but not with a screenshot, but simply with a link to an image on the Internet and sending a request.get (url).content request to the Post, it works and the request returns everything that is needed
. Therefore, I make a mistake that I am not sending it right per request.
I read the documentation for VK api ( https://vk.com/dev/upload_files?f=2.%20Uploading%20...), but I couldn’t understand better thanks to it
. then significant bytes.
Help to implement this function please, I've been sitting with this for 2 weeks already...
def get_schedule_bus():
now = datetime.datetime.now()
URL = "https://igis.ru/gortrans/bus/izh/29"
driver = webdriver.PhantomJS()
driver.set_window_size(1600, 2070)
driver.get(URL)
elem = driver.find_element_by_class_name("table-st1") # находим нужный нам элемент
screen = driver.get_screenshot_as_png() # получаем байтовое представление скриншота
rect = elem.rect # {'height': 1041, 'width': 722, 'x': 562, 'y': 689} получаем расположение нужного элемента
box = (
rect['x'], rect['y'], rect['x'] + rect['width'], rect['y'] + rect['height']) # делаем коробку для обреза фото
im = Image.open(BytesIO(screen)) # открывает скрин
region = im.crop(box) # вырезаем
driver.quit()
return region.tobytes()
print(get_schedule_bus())
image_data = get_schedule_bus()
upload_url = vk_api.photos.getMessagesUploadServer(group_id=group_id)['upload_url'] #получаем url от вк куда можно будет залить фото
image_name = "kek{}.png".format(random.randint(0, 10)) # делаем рандомное имя фотки
r = requests.post(upload_url, files={'photo': (image_name, image_data) }).json() # посылаем пост запрос вк
print(r)
d = {}
d['server'] = r['server']
d['photo'] = r['photo']
d['hash'] = r['hash']
d['group_id'] = group_id
photo = vk_api.photos.saveMessagesPhoto(**d)[0] # сохраняем на их сервере,как гласит документация
attachment = "photo{}_{}_{}".format(photo['owner_id'], photo['id'], photo['access_key'])
vk_session.method("messages.send",
{"chat_id": 2, "message": "", "attachment": attachment, "random_id": random.randint(0, 2048)})
Answer the question
In order to leave comments, you need to log in
It's weird how you post photos.
Why pass a tuple in the photo field if you only need to pass image bytes there, which in the usual form, when loaded from a file, look like this
. In your case, as I understand it, it should be
Well, I don’t think that tobytes() is suitable for this . I don't do image processing, but I would do something like this
{'photo': (image_name, image_data) }
{'photo': open(image_name, 'rb') }
{'photo': image_data }
b = BytesIO()
region.save(b, 'jpeg')
return b.getvalue()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question