Skip to content

Instantly share code, notes, and snippets.

@Cushychicken
Last active May 23, 2020 15:52
Show Gist options
  • Save Cushychicken/9813039 to your computer and use it in GitHub Desktop.
Save Cushychicken/9813039 to your computer and use it in GitHub Desktop.
Easily Grab Agilent Scope Shots Using Python

Easily Screencap Agilent Scopes

Grabbing scope shots has always been a pain in the ass. First it was the shitty Tektronix scopes my alma mater used: you needed a floppy disk to get shots. (Read that again. Floppy drive. FUCK.) Then it was slightly less shitty scopes at an internship that were USB stick enabled. Better. Not great, but better. Now, I'm employed, and got my boss to cash out for one of these beauties. (Scope, how do I love thee; let me count the ways...) It has the perk of being web enabled, but it's still a hassle to get the scope shot.

Navigate to a webpage? Click a button? Right click and "Save Target As"?! You've got to be fucking kidding me.

There is a better way. Guido made Python so lazy people's lives are easy. You can run this as a standalone script to save your .png in the current working directory; if you run it from iPython, like I do, you'll have to %pwd to find where your notebook lives if you don't know already.

import requests
from bs4 import BeautifulSoup

BASE_URL = 'http://10.20.20.91'         # This is the IP Address of my scope
CAPTURE  = '/getImage.asp?inv=false'    # This is the URI of the image capture script

r = requests.get(BASE_URL+CAPTURE, stream=True) # Call it this way to get the latest image
for img in BeautifulSoup(r.content).find_all('img'):
    f_name = img['src'].split('/')[-1]  # chop down img['src'] to the filename w/o the path
    with open(f_name, 'wb') as f:
        f.write(requests.get(BASE_URL + img['src']).content)  # open, read, write, beer

Improvements

It would be slick if you didn't need to import both requests and urllib2. I'm sure there's a way to do it with just requests, but couldn't find a decent how-to. Feel free to link me if you know where one is!

UPDATE @ 9:13AM, March 28 Thanks to stunsch for pointing out how to do this using just requests - no urllib2 necessary. Nice spot!

@stunsch
Copy link

stunsch commented Mar 28, 2014

What's wrong with this?

with open('f_name', 'wb') as f:
    f.write(requests.get(BASE_URL + img['src']).content)

@Cushychicken
Copy link
Author

Nothing! I didn't think to call requests again at the source of the image. D'oh. Thanks for the suggestion, I'll edit to remove urllib2.

@schrockwell
Copy link

Another idea: connect to the TCP SCPI on port 5024 (or 5025) and query ":DISPlay:DATA? PNG", then just save the results as a PNG file.

Source: http://www.home.agilent.com/upload/cmc_upload/All/4000_series_prog_guide.pdf?&cc=US&lc=eng

See "Telnet Sockets" (page 67) and ":DISPlay:DATA" (page 322).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment