用于将Image转换为Byte数组的Python脚本

时间:2022-12-12 23:00:18

I am writing a Python script where I want to do bulk photo upload. I want to read an Image and convert it into a byte array. Any suggestions would be greatly appreciated.

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

#!/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')

3 个解决方案

#1


30  

Use bytearray:

使用bytearray:

with open("img.png", "rb") as imageFile:
  f = imageFile.read()
  b = bytearray(f)

print b[0]

You can also have a look at struct which can do many conversions of that kind.

你也可以看看struct可以做很多转换。

#2


4  

i don't know about converting into a byte array, but it's easy to convert it into a string:

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

import base64

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

Source

资源

#3


2  

with BytesIO() as output:
    from PIL import Image
    with Image.open(filename) as img:
        img.convert('RGB').save(output, 'BMP')                
    data = output.getvalue()[14:]

I just use this for add a image to clipboard in windows.

我只是用这个来为windows中的剪贴板添加图像。

#1


30  

Use bytearray:

使用bytearray:

with open("img.png", "rb") as imageFile:
  f = imageFile.read()
  b = bytearray(f)

print b[0]

You can also have a look at struct which can do many conversions of that kind.

你也可以看看struct可以做很多转换。

#2


4  

i don't know about converting into a byte array, but it's easy to convert it into a string:

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

import base64

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

Source

资源

#3


2  

with BytesIO() as output:
    from PIL import Image
    with Image.open(filename) as img:
        img.convert('RGB').save(output, 'BMP')                
    data = output.getvalue()[14:]

I just use this for add a image to clipboard in windows.

我只是用这个来为windows中的剪贴板添加图像。