将图像转换为字节数组的Python脚本

2024-05-29 07:49:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写一个Python脚本,我想在那里做批量照片上传。 我想读取图像并将其转换为字节数组。如有任何建议,将不胜感激。

#!/usr/bin/python
import xmlrpclib
import SOAPpy, getpass, datetime
import urllib, cStringIO
from PIL import Image
from urllib import urlopen 
import os
import io
from array import array
""" create a proxy object with methods that can be used to invoke
    corresponding RPC calls on the remote server """
soapy = SOAPpy.WSDL.Proxy('localhost:8090/rpc/soap-axis/confluenceservice-v2?wsdl') 
auth = soapy.login('admin', 'Cs$corp@123')

Tags: from图像import脚本字节binusr数组
3条回答
with BytesIO() as output:
    from PIL import Image
    with Image.open(filename) as img:
        img.convert('RGB').save(output, 'BMP')                
    data = output.getvalue()[14:]

我只是用这个在windows中向剪贴板添加一个图像。

我不知道如何转换为字节数组,但很容易将其转换为字符串:

import base64

with open("t.png", "rb") as imageFile:
    str = base64.b64encode(imageFile.read())
    print str

Source

使用bytearray

with open("img.png", "rb") as image:
  f = image.read()
  b = bytearray(f)
  print b[0]

您还可以查看struct,它可以执行许多此类转换。

相关问题 更多 >

    热门问题