.. note:: Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts. **Why Join?** - **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team. - **Learn & Share**: Exchange tips and tutorials to enhance your skills. - **Exclusive Previews**: Get early access to new product announcements and sneak peeks. - **Special Discounts**: Enjoy exclusive discounts on our newest products. - **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions. 👉 Ready to explore and create with us? Click [|link_sf_facebook|] and join today! .. _3.1.1_py: 3.1.1 Photograph Module ========================== Introduction ----------------- In this kit, equipped with a camera module, let's try to take a picture with Raspberry Pi. Required Components ------------------------------ In this project, we need the following components. .. image:: ../img/photo1.png :width: 800 It's definitely convenient to buy a whole kit, here's the link: .. list-table:: :widths: 20 20 20 :header-rows: 1 * - Name - ITEMS IN THIS KIT - LINK * - Raphael Kit - 337 - |link_Raphael_kit| You can also buy them separately from the links below. .. list-table:: :widths: 30 20 :header-rows: 1 * - COMPONENT INTRODUCTION - PURCHASE LINK * - :ref:`cpn_camera_module` - |link_camera_buy| Experimental Procedures ------------------------------ **Step 1:** Go into the Raspberry Pi Desktop. You may need a screen for a better experience, refer to: `Connect your Raspberry Pi `_. Or access the Raspberry Pi desktop remotely, for a detailed tutorial please refer to :ref:`remote_desktop`. **Step 2:** Check if the camera is enabled. For instructions, please refer to: :ref:`enable_camera`. **Step 3:** Open a Terminal and get into the folder of the code. .. code-block:: cd ~/raphael-kit/python/ **Step 4:** Run. .. code-block:: sudo python3 3.1.1_PhotographModule.py After the code runs, the camera will take a photo. Now you can see the photo named ``my_photo.jpg`` in the ``~`` directory. .. note:: You can also open ``3.1.1_PhotographModule.py`` in the ``~/raphael-kit/python/`` path with a Python IDE, click Run button to run, and stop the code with Stop button. If you want to download the photo to your PC, please refer to :ref:`filezilla`. **Code** .. code-block:: python from picamera import PiCamera import os user = os.getlogin() user_home = os.path.expanduser(f'~{user}') camera = PiCamera() def setup(): camera.start_preview(alpha=200) def main(): camera.capture(f'{user_home}/my_photo.jpg') while True: pass def destroy(): camera.stop_preview() if __name__ == '__main__': setup() try: main() except KeyboardInterrupt: destroy() **Code Explanation** .. code-block:: python from picamera import PiCamera camera = PiCamera() Import the ``picamera`` library and instantiate the ``PiCamera`` class to use the camera module. .. code-block:: python start_preview(**options) Show the preview overlay and change the transparency level of the preview with ``alpha`` - from 0 to 255. This method starts a camera preview as an overlay on the Pi’s primary display (HDMI or composite). By default, the renderer will be opaque and fullscreen. This means the default preview overrides whatever is currently visible on the display. More specifically, the preview does not rely on a graphical environment like X-Windows (it can run quite happily from a TTY console); it is simply an overlay on the Pi’s video output. To stop the preview and reveal the display again, call ``stop_preview()`` . The preview can be started and stopped multiple times during the lifetime of the ``PiCamera`` object. .. code-block:: python camera.capture(f'{user_home}/my_photo.jpg') Capture an image from the camera, storing it in ``~/``. .. note:: You can use ``camera.capture()`` function and ``for`` loop together to achieve continuous shooting. And use the ``delay`` function to adjust the time interval for taking pictures. .. code-block:: python for i in 5: camera.capture(f'{user_home}/my_photo%s.jpg' % i)