最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

windows - Is there a way to convert DIBv5 data from the clipboard from to a file, preserving transparency, in Python? - Stack Ov

programmeradmin3浏览0评论

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...

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论