[英]Python Script to convert Image into Byte array
I am writing a Python script where I want to do bulk photo upload.我正在编写一个 Python 脚本,我想在其中进行批量照片上传。 I want to read an Image and convert it into a byte array.
我想读取一个图像并将其转换为字节数组。 Any suggestions would be greatly appreciated.
任何建议将不胜感激。
#!/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')
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中的剪贴板中。
This works for me 这适合我
# 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
#!/usr/bin/env python
#__usage__ = "To Convert Image pixel data to readable bytes"
import numpy as np
import cv2
from cv2 import *
class Image2ByteConverter(object):
def loadImage(self):
# Load image as string from file/database
file = open('Aiming_ECU_Image.png', "rb")
return file
def convertImage2Byte(self):
file = self.loadImage()
pixel_data = np.fromfile(file, dtype=np.uint8)
return pixel_data
def printData(self):
final_data = self.convertImage2Byte()
pixel_count = 0
for data in final_data:
pixel_count += 1
print("Byte ", pixel_count,": ", int(data))
def run(self):
# self.loadImage()
# self.convertImage2Byte()
self.printData()
if __name__ == "__main__":
Image2ByteConverter().run()
This Worked for me.这对我有用。
Pre-requisites:先决条件:
python -m pip install opencv-python
python -m pip install numpy
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.