Alon123
Alon123

Reputation: 174

Converting float list to byte-like object results in a corrupted image

I am trying to convert data from a Keysight scope to .png Image. When I looked how to do it online, I stumbled the following code:

# Download the screen image.
# --------------------------------------------------------
sDisplay = do_query_ieee_block(":DISPlay:DATA? PNG")
# Save display data values to file.
f = open("screen_image.png", "wb")
f.write(sDisplay)
f.close()

The problem with code above is that I'm not sure what do_query_ieee_block is. I thought it was under pyvisa but couldn't find it there. After looking online, I understood that query_binary_values is more or less the same thing as pyvisa assumes that data is in IEEE.

According to this information, I wrote the following code:

import pyvisa
import struct

IDN="SomeScopeIDN" #something like 'USB0::0xhhhh::0xhhhh::MYdddddddd::0::INSTR' 
scope=pyvisa.ResourceManager().open_resource(IDN)
binImage=self.scope.query_binary_values(":DISPlay:DATA? PNG") #This gets a list of floats
byteImage=struct.pack('%sf' % len(binImage),*binImage) #Convert the list to bytes-like object
path=r"c:\Users\user\Desktop\Scope_Image.png"
with open (path,'wb') as f:
  f.write(byteImage)

This creates an image, but the outcome is bad:

Corrupted Image

and from snipping tool:

Corrupted Image

As you can see, only the upper 10% of the image is fine but the rest is not (it will be mentioned that each time I retrieve new data from the scope I get a different area that is corrupted, which is between 50-90% of the picture. Most of the time the corrupted area is between 80-90%, as seen in the picture above).

So basically my questions are:

  1. Could it be that I don't read the whole data from the instrument? and if so, how can overcome this issue?

  2. Could it be happening because of packing in a wrong way? If so, what's the solution?

Upvotes: 2

Views: 746

Answers (2)

Radar2400
Radar2400

Reputation: 1

I had a similar issue using query_binary_values(), only instead of trying to generate a png I was trying to read back ARB waveforms.

The solution I found related to the endianess of my data. Look into the is_big_endian parameter to see if that may be causing the issue. I would also look into the header_fmt, delay, and chunk_size parameters.

https://pyvisa.readthedocs.io/en/latest/api/resources.html#pyvisa.resources.MessageBasedResource.query_binary_values

Upvotes: 0

Aviv Yaniv
Aviv Yaniv

Reputation: 6298

Seen an example that may be helpful:

  1. Add datatype='B', container=bytearray as arguments to query_binary_values()

  2. Try ':DISP:DATA? ON,OFF,PNG' interchangeably

import pyvisa

IDN="SomeScopeIDN" #something like 'USB0::0xhhhh::0xhhhh::MYdddddddd::0::INSTR' 
scope=pyvisa.ResourceManager().open_resource(IDN)
binImage=self.scope.query_binary_values(":DISPlay:DATA? PNG", datatype='B', container=bytearray)
path=r"c:\Users\user\Desktop\Scope_Image.png"
with open (path,'wb') as f:
  f.write(byteImage)

Upvotes: 0

Related Questions