-1

I want to make a python screenshot taker, so i tried to make it take screenshots, and i thought that i could just f.write(NewByteArray), but it didn't work, i got some errors, here:

Traceback (most recent call last):
  File "C:\Users\*******\OneDrive\Área de Trabalho\Meu Computador\Programas\Python\GC YT Maker\Gacha Club Screenshot taker.py", line 22, in <module>
    BiteArray = bytearray(image)
TypeError: cannot convert 'PngImageFile' object to bytearray

and i don't know what to do anymore, here's all my code btw:

import os
import sys
import pyautogui
import pyscreenshot
from PIL import Image

SCREEN_X1 = 10
SCREEN_Y1 = 10
# X1 Position of the background
# Y1 Position of the background
SCREEN_X2 = 255
SCREEN_Y2 = 255
# X2 Position of the background
# Y2 Position of the background
    
Filename = 0
FILENAME = f'{Filename}.png'

image = pyscreenshot.grab(bbox=(SCREEN_X1, SCREEN_Y1, SCREEN_X2, SCREEN_Y2))
#image.show()

BiteArray = bytearray(image)

with open(FILENAME, 'wb') as f:
    f.write(BiteArray)
    f.close



Filename += 1

2 Answers2

0

the module returns a PIL image, which has the save function for this exact purpose.

image.save(FILENAME)

but if you want to manually write it to disk yourself, you can first save it in a BytesIO object then save it to desk ...

import io

BiteArray = io.BytesIO()
image.save(BiteArray,format='png')


with open(FILENAME, 'wb') as f:
    f.write(BiteArray.getvalue())
    f.close() # notice don't need to call this function

this is probably useful if you get paid by the number of lines in your code and 6 lines instead of 1 means more pay

jokes aside, this may be useful if you weren't writing it to disk the normal way, like sending it over network.

Ahmed AEK
  • 8,584
  • 2
  • 7
  • 23
  • Alright, thank you very much, but i was wondering, what i also wanted to do, is, the program takes one screenshot, and then it saves as 1.png, i was wondering if i could make it so that after the program takes a screenshot, it also takes another, and saves that as 2.png, without deleting the first one, is this possible? – TH1211.EXE Sep 14 '22 at 23:25
  • first of all you need to understand that it will be taking thousands of screenshots every second, and if you just made it do so you will run out of disk space in seconds, you **must** add a `time.sleep(1)` from the `time` module after the line you take the screenshot as a delay to limit it to only 1 screenshot every second, now in order to keep doing something repeatedly you must put it in a loop, specifically a `while True:` loop, that you should learn about, again don't do this until you insert that delay orelse you might run into disk problems. – Ahmed AEK Sep 14 '22 at 23:29
  • not sure what you think that my education in programming is, but i'm not much of a newbie, i do know what a loop is, and i do know what the time module does, sorry if that was rude, anyway, i wanna make a program, that takes a screenshot after you press the key "Enter" on your keyboard, and then do all the stuff i said on the comment before – TH1211.EXE Sep 14 '22 at 23:44
  • I actually did a autoclicker without any help, here's the link if you wanna see it, it's for portuguese speaking people only though, here: https://github.com/Thatguy1211/DESTROYER-AUTOCLICKER – TH1211.EXE Sep 14 '22 at 23:45
  • 1) add a `while True:` the line between `Filename` and `FILENAME`, 2) indent the code, 3) add the delay, 4) detect the key store [using this answer](https://stackoverflow.com/questions/53381360/how-do-i-detect-a-keypress-event-with-pyautogui). – Ahmed AEK Sep 14 '22 at 23:53
  • Alright, but then my filename is going to be "1", and what if i wanna take like 100 screenshots with specific coordinates, i'll have to individually go and take the 0.png file, and save it into a folder, and then run the program again, and then rename the new 0.png to 1.png, i'm doing this whole thing to speedup something that i wanna do in my free time as a passion, i don't wanna have to worry about each single screenshot i take, let's just say i wanna work as a "animator" youtuber, that's why i'm doing this whole thing – TH1211.EXE Sep 14 '22 at 23:59
  • The enter key is specified as `RETURN`. – Ahmed AEK Sep 14 '22 at 23:59
  • The +1 at the end of your code will increment the counter, so long as you don't close the application. – Ahmed AEK Sep 15 '22 at 00:00
  • Just a question, but, do you wanna chat in a DM? But thanks for letting me know! that was real useful! – TH1211.EXE Sep 15 '22 at 00:01
  • Even if i don't close the application, the garbage collector resets the value, i can show you the source code in a DM if you want to – TH1211.EXE Sep 15 '22 at 00:02
  • If you want to save the counter between sessions, you should write it to a text file every iteration of the loop and read it everytime you open the application, it's already 2AM here, so nah on the offer. – Ahmed AEK Sep 15 '22 at 00:03
  • that's actually really smart, making a .json and read it, and save it every time i make a session, that's brilliant – TH1211.EXE Sep 15 '22 at 00:05
  • i might of aswell make a new question somewhere else, my brain is too small to actually think of good ways to do this, but thx for your help, you helped more than i could have imagined! – TH1211.EXE Sep 15 '22 at 00:09
0

You can do this only using the pyautogui module.
pyautogui.screenshot(region=(0,0, 300, 400))
Reference

walker
  • 444
  • 2
  • 12