= How to take a screen shot (Python) =
'''Keywords :''' Screen, Screenshot, Selected area.

<<TableOfContents>>

--------
= Demonstrating : =
__'''''Tested''' py3.x, wx4.x and Win10. ''__

Are you ready to use some samples ? ;)

Test, modify, correct, complete, improve and share your discoveries ! (!)

--------
== Screen shot with wxPython : ==
=== First example ===
{{attachment:img_sample_one_a.png}}

{{{#!python
# sample_one_a.py

import wx

#-------------------------------------------------------------------------------

app = wx.App(False)

s = wx.ScreenDC()
w, h = s.Size.Get()
b = wx.Bitmap(w, h)

m = wx.MemoryDC(s)
m.SelectObject(b)
m.Blit(0, 0, w, h, s, 0, 0)
m.SelectObject(wx.NullBitmap)

b.SaveFile("Screenshot.png", wx.BITMAP_TYPE_PNG)
}}}
--------
=== Second example (Andrea Gavana) ===
{{attachment:img_sample_one_b.png}}

{{{#!python
# sample_one_b.py

"""

Author : Thank to Andrea Gavana.
Link : https://stackoverflow.com/questions/58539333/how-to-take-screenshot-of-whole-screen-using-wxpython

"""

import wx

#---------------------------------------------------------------------------

class MyFrame(wx.Frame):
    """
    ...
    """
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        #------------

        # Simplified init method.
        self.SetProperties()
        self.CreateCtrls()
        self.BindEvents()
        self.DoLayout()

        #------------

        self.CenterOnScreen(wx.BOTH)

    #-----------------------------------------------------------------------

    def SetProperties(self):
        """
        ...
        """

        self.SetSize((340, 250))

        #------------

        # Return icons folder.
        self.SetIcon(wx.Icon('./icons/wxwin.ico'))

    #-----------------------------------------------------------------------

    def CreateCtrls(self):
        """
        ...
        """

        # Create a panel.
        self.panel = wx.Panel(self, -1)

        #------------

        # Add buttons.
        self.btnScreenshot = wx.Button(self.panel, - 1,
                                       "Take a screenshot")

        self.btnClose = wx.Button(self.panel, -1,
                                  "&Close")


    def BindEvents(self):
        """
        Bind some events to an event handler.
        """

        # Bind events to an events handler.
        self.Bind(wx.EVT_BUTTON, self.OnScreenshot, self.btnScreenshot)
        self.Bind(wx.EVT_BUTTON, self.OnCloseMe, self.btnClose)
        self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)


    def DoLayout(self):
        """
        ...
        """

        # MainSizer is the top-level one that manages everything.
        mainSizer = wx.BoxSizer(wx.VERTICAL)

        # wx.BoxSizer(window, proportion, flag, border).
        # wx.BoxSizer(sizer, proportion, flag, border).
        mainSizer.Add(self.btnScreenshot, 1, wx.EXPAND | wx.ALL, 10)
        mainSizer.Add(self.btnClose, 1, wx.EXPAND | wx.ALL, 10)

        # Finally, tell the panel to use the sizer for layout.
        self.panel.SetAutoLayout(True)
        self.panel.SetSizer(mainSizer)

        mainSizer.Fit(self.panel)


    def OnScreenshot(self, event):
        """
        Thank to Andrea Gavana.
        """

        screen = wx.ScreenDC()
        size = screen.GetSize()

        # Debugging : see if pixel values are okay.
        print(wx.DisplaySize())

        width = size[0]
        height = size[1]

        # Compare with above values.
        print(width, height)

        # Use Bitmap here instead of wx.Bitmap.
        bmp = wx.Bitmap(width, height)
        mem = wx.MemoryDC(bmp)
        # Tell mem to use the actual bitmap.
        mem.SelectObject(bmp)
        mem.Blit(0, 0, width, height, screen, 0, 0)
        del mem
        bmp.SaveFile('screenshot_for_working.png', wx.BITMAP_TYPE_PNG)


    def OnCloseMe(self, event):
        """
        ...
        """

        self.Close(True)


    def OnCloseWindow(self, event):
        """
        ...
        """

        self.Destroy()

#---------------------------------------------------------------------------

if __name__ == '__main__':
     app = wx.App()
     frame = MyFrame(None, title="wxPython screenshot")
     frame.Show(True)
     app.MainLoop()
}}}
--------
=== Third example (Mike driscoll) ===
{{attachment:img_sample_one_c.png}}

{{{#!python
# sample_one_c.py

"""

Author : Mike Driscoll
Link : https://www.blog.pythonlibrary.org/2010/04/16/how-to-take-a-screenshot-of-your-wxpython-app-and-print-it/

"""

import sys
import wx

#---------------------------------------------------------------------------

class MyForm(wx.Frame):
    """
    ...
    """
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial", size=(300, 250))

        #------------

        # Return icons folder.
        self.SetIcon(wx.Icon('./icons/wxwin.ico'))

        #------------

        # Add a panel so it looks the correct on all platforms
        panel = wx.Panel(self, wx.ID_ANY)
        screenshotBtn = wx.Button(panel, wx.ID_ANY, "Take Screenshot")
        screenshotBtn.Bind(wx.EVT_BUTTON, self.OnTakeScreenShot)

        #------------

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(screenshotBtn, 0, wx.ALL|wx.CENTER, 5)
        panel.SetSizer(sizer)

    #-----------------------------------------------------------------------

    def OnTakeScreenShot(self, event):
        """
        Takes a screenshot of the screen at give pos & size (rect).
        """

        print('Taking screenshot...')

        rect = self.GetRect()
        # See http://aspn.activestate.com/ASPN/Mail/Message/wxpython-users/3575899
        # Created by Andrea Gavana.

        # Adjust widths for Linux (figured out by John Torres.
        # http://article.gmane.org/gmane.comp.python.wxpython/67327)
        if sys.platform == 'linux2':
            # client_x, client_y = self.ClientToScreen((0, 0))
            client_x, client_y = self.ClientToScreen((0, 0))
            border_width = client_x - rect.x
            title_bar_height = client_y - rect.y
            rect.width += (border_width * 2)
            rect.height += title_bar_height + border_width

        # Create a DC for the whole screen area.
        dcScreen = wx.ScreenDC()

        # Create a Bitmap that will hold the screenshot image later on.
        # Note that the Bitmap must have a size big enough to hold the screenshot
        # -1 means using the current default colour depth.
        bmp = wx.Bitmap(rect.width, rect.height)

        # Create a memory DC that will be used for actually taking the screenshot.
        memDC = wx.MemoryDC()

        # Tell the memory DC to use our Bitmap
        # all drawing action on the memory DC will go to the Bitmap now.
        memDC.SelectObject(bmp)

        # Blit (in this case copy) the actual screen on the memory DC
        # and thus the Bitmap.
        memDC.Blit( 0, # Copy to this X coordinate.
                    0, # Copy to this Y coordinate.
                    rect.width, # Copy this width.
                    rect.height, # Copy this height.
                    dcScreen, # From where do we copy ?
                    rect.x, # What's the X offset in the original DC ?
                    rect.y  # What's the Y offset in the original DC ?
                    )

        # Select the Bitmap out of the memory DC by selecting a new
        # uninitialized Bitmap.
        memDC.SelectObject(wx.NullBitmap)

        img = bmp.ConvertToImage()
        fileName = "MyScreenshot.png"
        img.SaveFile(fileName, wx.BITMAP_TYPE_PNG)
        print('...saving as png!')

#---------------------------------------------------------------------------

# Run the program.
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()
}}}
--------
=== QQ-screenshot (้†‰ไป™็ต่Š™ / CorttChan) : ===
==== First example ====

'''Link :'''

https://github.com/guzhenping/QQ-screenshot-made-by-wxPython/blob/master/CopyScreen.pyw

https://github.com/guzhenping/QQ-screenshot-made-by-wxPython

https://github.com/guzhenping/

--------
== Screen shot with Python : ==
=== Pyscreenshot : ===
==== First example ====
{{attachment:img_sample_three_a.png}}

Grab and show the whole screen.

You must install this package for use it :

'''pip install pyscreenshot'''

{{{#!python
# sample_three_a.py

import pyscreenshot as ImageGrab

#---------------------------------------------------------------------------

# grab fullscreen
im = ImageGrab.grab()

# save image file
im.save('fullscreen.png')
}}}
==== Second example ====
{{attachment:img_sample_three_b.png}}

{{{#!python
# sample_three_b.py

import pyscreenshot as ImageGrab

#---------------------------------------------------------------------------

# part of the screen
im = ImageGrab.grab(bbox=(10, 10, 510, 510))  # X1,Y1,X2,Y2

# save image file
im.save('box.png')
}}}
'''Link :'''

https://pypi.org/project/pyscreenshot/

https://github.com/ponty/pyscreenshot

--------
=== PyAutoGUI : ===
==== First example ====
{{attachment:img_sample_four_a.png}}

You must install this package for use it :

'''pip install PyAutoGUI'''

{{{#!python
# sample_four_a.py

import pyautogui

#---------------------------------------------------------------------------

im1 = pyautogui.screenshot()
im1.save('my_screenshot.png')
im2 = pyautogui.screenshot('my_screenshot2.png')
}}}
'''Link :'''

https://pypi.org/project/PyAutoGUI/

https://pyautogui.readthedocs.io/en/latest/

https://datatofish.com/screenshot-python/

--------
=== Mss : ===
==== First example ====
{{attachment:img_sample_five_a.png}}

You must install this package for use it :

'''pip install mss'''

{{{#!python
# sample_five_a.py

from mss import mss

#---------------------------------------------------------------------------

# The simplest use, save a screen shot of the 1st monitor
with mss() as sct:
    sct.shot()
}}}
'''Link :'''

https://pypi.org/project/mss/

--------
=== Screenshot (Alex DeLorenzo) : ===
==== First example ====
{{attachment:img_sample_six_a.png}}

Better macOS screenshots via the Terminal.

You must install this package for use it :

'''pip install screenshot'''

'''Link :'''

https://pypi.org/project/screenshot/

https://github.com/alexdelorenzo/screenshot

--------
= Additional Information =
'''Link :'''

- - - - -

https://wiki.wxpython.org/TitleIndex

https://docs.wxpython.org/

--------
= Thanks to =
The wxPython community...

--------
= About this page =
Date(d/m/y)     Person (bot)    Comments :

24/10/20 - Ecco (Created page for wxPython Phoenix).

--------
= Comments =
- blah, blah, blah....