Tricks

ynycoding發表於2024-10-23

To capture the rendered image from the virtual display (since the program itself doesn't save it), you can use a screen capture tool that works with virtual displays like Xvfb. Here's how you can achieve this:

Steps to Capture the Image from the Virtual Display

  1. Install xwd (X Window Dump):

    • xwd is a utility that captures the contents of an X Window display (including virtual displays created by Xvfb).
    • Install x11-apps package, which includes xwd:
      sudo apt-get install x11-apps   # Debian/Ubuntu-based systems
      sudo yum install xorg-x11-apps  # RHEL/CentOS-based systems
      
  2. Run Your Program with Xvfb:
    After setting up Xvfb as mentioned before, run the program in the virtual display:

    Xvfb :99 -screen 0 1024x768x24 &
    export DISPLAY=:99
    ./your_program
    
  3. Capture the Screen Using xwd:
    Use xwd to capture the contents of the virtual display as an image:

    xwd -root -display :99 -out screenshot.xwd
    

    This will capture the entire virtual display and save it as a file named screenshot.xwd.

  4. Convert the Captured Image (Optional):
    The output format (.xwd) might not be suitable for direct use. You can convert it to a more common format (like PNG or JPEG) using tools such as ImageMagick:

    • Install ImageMagick:
      sudo apt-get install imagemagick
      
    • Convert .xwd to .png:
      convert screenshot.xwd screenshot.png
      
  5. View the Image:

    • Once the image is converted to a more common format like PNG, you can transfer the file back to your Mac and view it using any standard image viewer.
    • To transfer the file:
      scp user@yourserver.com:/path/to/screenshot.png /local/path/
      

Full Process Example:

# Start Xvfb
Xvfb :99 -screen 0 1024x768x24 &
export DISPLAY=:99

# Run your program
./your_program &

# Capture the virtual display
xwd -root -display :99 -out screenshot.xwd

# Convert to PNG format
convert screenshot.xwd screenshot.png

# (Optional) Transfer to local Mac
scp user@yourserver.com:/path/to/screenshot.png /local/path/

This process allows you to render the image in the virtual display, capture it, and then convert and view it locally.

Would this workflow work for you?

相關文章