Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Image Not Saved Properly
#1
I have the following code from a video course that produces a screenshot image on the monitor and saves a file with a valid name and extension but when you go to Explorer in Windows 11 and click on the file it says the file has a bad format. Thoughts?

import time
import pyautogui

def screenshot():
    name = int(round(time.time() * 1000))
    name = 'C:/Users/jkres/AppData/Local/Programs/Python/Python312/Programs/test/{}.png'.format(name)
    time.sleep(2)
    img = pyautogui.screenshot(name)
    img.show()
    img.save(name)

screenshot()
Reply
#2
Works fine when for me.

You don't need this line:
img.save(name)
Calling screenshot with a name automatically saves the image to a file of that name.

What version of Python are you using? What version of pyautogui? What version of PIL (screenshot returns a PIL Image)?
jkreski likes this post
Reply
#3
Thanks! I am using IDLE version 3.12.5. Python is 3.12. pyautogui was just installed yesterday so I think it should be the most current version. The only PIL I can find is: PIL-1.1.7-py2.7. I didn't think I needed the SAVE but thought maybe if I forced it then it would save it properly.
Reply
#4
(Mar-15-2026, 04:15 PM)jkreski Wrote: Thanks! I am using IDLE version 3.12.5.
Do not run IDLE when using automation or any GUI related stuff.
Because IDLE can block operation.
From command line python your_code.py.
Reply
#5
(Mar-15-2026, 06:54 PM)snippsat Wrote:
(Mar-15-2026, 04:15 PM)jkreski Wrote: Thanks! I am using IDLE version 3.12.5.
Do not run IDLE when using automation or any GUI related stuff.
Because IDLE can block operation.
From command line python your_code.py.

I opened a command prompt and changed directory to my code directory and ran the command. Same result. Bad file type. Also, when I tried to run the code from Visual Studio Code I got the error message pyautogui could not be resolved from source. It did run and I got the same result.
Reply
#6
(Mar-15-2026, 04:15 PM)jkreski Wrote: The only PIL I can find is: PIL-1.1.7-py2.7.
This is an old version,unisntall and reinstall.
It's called pillow now for long time.
pip uninstall Pillow
pip install --upgrade pip
Check that it show Version 12.1.1.
λ pip show pillow 
Name: pillow
Version: 12.1.1
Summary: Python Imaging Library (fork)
Home-page: https://python-pillow.github.io
Author:
Author-email: "Jeffrey A. Clark" <[email protected]>
License-Expression: MIT-CMU
Location: C:\Python313\Lib\site-packages
Requires:
Required-by: weasyprint version 
Reply
#7
(Mar-15-2026, 08:56 PM)snippsat Wrote:
(Mar-15-2026, 04:15 PM)jkreski Wrote: The only PIL I can find is: PIL-1.1.7-py2.7.
This is an old version,unisntall and reinstall.
It's called pillow now for long time.
pip uninstall Pillow
pip install --upgrade pip
Check that it show Version 12.1.1.
λ pip show pillow 
Name: pillow
Version: 12.1.1
Summary: Python Imaging Library (fork)
Home-page: https://python-pillow.github.io
Author:
Author-email: "Jeffrey A. Clark" <[email protected]>
License-Expression: MIT-CMU
Location: C:\Python313\Lib\site-packages
Requires:
Required-by: weasyprint version 

I followed the directions and have the new version but get the same results. Thanks for trying!
Reply
#8
You can get screenshots very simply like this:

from PIL import ImageGrab

savepath = '/home/peterr/temp/images/ss1.jpg'
ss_area = (200, 200, 400, 400)
# get whole screen
# ss_img = ImageGrab.grab()

def grabit(area):
    ss_img = ImageGrab.grab(area)
    ss_img.save(savepath)

grabit(ss_area)
If you use datetime as well you can save with a timestamp!
Reply
#9
(Mar-16-2026, 05:37 AM)Pedroski55 Wrote: You can get screenshots very simply like this:

from PIL import ImageGrab

savepath = '/home/peterr/temp/images/ss1.jpg'
ss_area = (200, 200, 400, 400)
# get whole screen
# ss_img = ImageGrab.grab()

def grabit(area):
    ss_img = ImageGrab.grab(area)
    ss_img.save(savepath)

grabit(ss_area)
If you use datetime as well you can save with a timestamp!

Thanks but I am trying to get the code from the video course I am following to work on my machine. It works on others. I need to fix what is wrong with my software, not work around it.
Reply
#10
Try making a virtual environment(build into Python).
To make sure that not problem with Python and modules installed.
Use cmd same commands,i use cmder here.
# Make 
E:\div_code
λ python -m venv shoot_env

# Cd in
E:\div_code
λ cd shoot_env\

# Activate
E:\div_code\shoot_env
λ E:\div_code\shoot_env\Scripts\activate

# Install, when environment work will see (shoot_env)
E:\div_code\shoot_env
(shoot_env) λ pip install --no-cache-dir pyautogui pillow
I testet with your code save to folder in use,and can also use Pyautogui to save directly both work.
import time
import pyautogui

def screenshot():
    name = int(round(time.time() * 1000))
    name = f'{name}.png'.format(name)
    time.sleep(2)
    img = pyautogui.screenshot(name)
    img.show()
    img.save(name)

screenshot()
Pyautogui has own way to save,in script over it really save 2 times.
import pyautogui

img = pyautogui.screenshot()
img.save("testshot.png")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using .pb saved model for object detection hobbyist 2 2,615 Aug-03-2022, 05:55 AM
Last Post: hobbyist
  bytes object saved as .mp4 jttolleson 10 9,938 Feb-25-2022, 02:42 PM
Last Post: jttolleson
  Why can't numpy array be restored to a saved value? pjfarley3 1 2,715 Nov-25-2020, 07:40 AM
Last Post: pjfarley3
  Only getting last record saved...Why Milfredo 10 8,690 Sep-10-2020, 03:00 AM
Last Post: Milfredo
  Running scripts and location of saved interpreted user-defined classes and functions leodavinci1990 3 4,298 Aug-25-2020, 03:43 AM
Last Post: micseydel
  Saved Programs jpburrall 3 3,927 Sep-02-2019, 11:43 AM
Last Post: ThomasL
  How do I edit saved Python 3 script? Mocap 1 3,062 Jul-17-2019, 08:41 AM
Last Post: perfringo
  Openpyxl - When save existing xlsx sheet, images/drawing does not get saved shubhamjainj 2 13,721 Apr-16-2019, 07:09 AM
Last Post: shubhamjainj
  How retrieve sqlite3 database saved image and access in python3 tao01 1 3,739 Dec-22-2018, 09:04 PM
Last Post: Larz60+
  executing a file saved in memory Skaperen 0 3,883 Sep-04-2017, 04:23 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020