To simplify a process I do often, I would like to have a Python function to a) take the clipboard data, which is in dibv5 format with alpha, b) save it to a file, and c) upload it to an API. I don't need help with the API thing, but I do need help converting that dibv5 to a working file.
I had this code set up, which worked for the most part:
def dib_to_image(dib_data):
if not dib_data:
return None
bmp_header = b'BM' + struct.pack('<I', len(dib_data) + 14) + b'\x00\x00\x00\x00' + struct.pack('<I', 14 + struct.unpack('<I', dib_data[0:4])[0])
image = Image.open(io.BytesIO(bmp_header + dib_data))
return image
However, the last however-many vertical lines of pixels in the image would be (typically) moved to the left side of the image and shifted vertically typically one pixel. This didn't really work the way I wanted it to, and I figured the issue may lie with the bitmap header, but I'm completely unsure.
If some code sample exists that does this in an easier way, I'm fine with replacing my entire code function here...