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

 来杯冰柠檬水 发布于 2023-01-18 15:29

我正在编写一个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')

Thomas Baruc.. 45

用途bytearray:

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

您还可以查看一下可以执行此类转换的struct.

4 个回答
  • 这对我有用

    # Convert image to bytes
    import PIL.Image as Image
    pil_im = Image.fromarray(image)
    b = io.BytesIO()
    pil_im.save(b, 'jpeg')
    im_bytes = b.getvalue()
    return im_bytes
    

    2023-01-18 15:30 回答
  • 我不知道转换成字节数组,但很容易将其转换为字符串:

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

    资源

    2023-01-18 15:31 回答
  • 用途bytearray:

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

    您还可以查看一下可以执行此类转换的struct.

    2023-01-18 15:31 回答
  • 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中的剪贴板添加图像。

    2023-01-18 15:33 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有