Convert PIL Image to byte array?
1.
import io img = Image.open(fh, mode='r')
roiImg = img.crop(box) imgByteArr = io.BytesIO()
roiImg.save(imgByteArr, format='PNG')
imgByteArr = imgByteArr.getvalue()
2.
from PIL import Image
import io # I don't know what Python version you're using, so I'll try using Python 3 first
try:
import urllib.request as urllib
except ImportError:
# You are using Python 2 it turns out
import urllib def my_func(filename, ext):
# Get the image from the URL
im = Image.open(urllib.urlopen(filename)) fp = io.BytesIO()
format = Image.registered_extensions()['.'+ext]
im.save(fp, format)
return fp.getvalue() jpg_bin = my_func("http://p1.pstatp.com/list/300x196/pgc-image/152923179745640a81b1fdc.webp", "jpg")
3.
import io
from PIL import Image # 注意我的Image版本是pip3 install Pillow==4.3.0
import requests res = requests.get('http://images.xxx.com/-7c0dc4dbdca3.webp', stream=True) # 获取字节流最好加stream这个参数,原因见requests官方文档 byte_stream = io.BytesIO(res.content) # 把请求到的数据转换为Bytes字节流(这样解释不知道对不对,可以参照[廖雪峰](https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431918785710e86a1a120ce04925bae155012c7fc71e000)的教程看一下) roiImg = Image.open(byte_stream) # Image打开Byte字节流数据 imgByteArr = io.BytesIO() # 创建一个空的Bytes对象 roiImg.save(imgByteArr, format='PNG') # PNG就是图片格式,我试过换成JPG/jpg都不行 imgByteArr = imgByteArr.getvalue() # 这个就是保存的图片字节流 # 下面这一步只是本地测试, 可以直接把imgByteArr,当成参数上传到七牛云
with open("./abc.png", "wb") as f:
f.write(imgByteArr)
Convert PIL Image to byte array?的更多相关文章
- Convert a byte[] array to readable string format. This makes the "hex" readable!
		/* * Java Bittorrent API as its name indicates is a JAVA API that implements the Bittorrent Protocol ... 
- Byte Array to Hexadecimal String
		Lookup Text: 23,879.41 (20.8X faster) Sentence: 1.15 (23.9X faster) /// <summary> /// Hex stri ... 
- C# byte array 跟 string 互转
		用 System.Text.Encoding.Default.GetString() 转换时,byte array 中大于 127 的数据转 string 时会出问题. 把这里的 Default 换成 ... 
- C#中使用Buffer.BlockCopy()方法将string转换为byte array的方法:
		public static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count); 将指定数目的字 ... 
- PHP write byte array to file
		/********************************************************************************* * PHP write byte ... 
- XAF 如何将数据库中Byte array图片显示出来
		问题比较简单,直接上代码. private Image _Cover; [Size(SizeAttribute.Unlimited), ValueConverter(typeof(ImageValue ... 
- [原] XAF 如何将数据库中Byte array图片显示出来
		问题比较简单,直接上代码. private Image _Cover; [Size(SizeAttribute.Unlimited), ValueConverter(typeof(ImageValue ... 
- DBus send byte array over gdbus ----Send dbus data
		遇到一个问题,如何通过dbus传送uint8数组元素 有3种方法, 1.直接传 ay 2.传 a(y) 3.xml定义为 ay,但是通过annotation 强行将 guchar 转为GVarian ... 
- 「Python」Convert map object to numpy array in python 3
		转自Stackoverflow.备忘用. Question In Python 2 I could do the following: import numpy as np f = lambda x: ... 
随机推荐
- 论文笔记之:Semi-supervised Classification with Graph Convolutional Networks
			Semi-supervised Classification with Graph Convolutional Networks 2018-01-16 22:33:36 1. 文章主要思想: 2. ... 
- Unity3D学习笔记(二十七):MVC框架下的背包系统(2)
			Tools FileTools using System.Collections; using System.Collections.Generic; using UnityEngine; using ... 
- C++笔记(2017/2/9)
			this指针 this指针作用就是指向成员函数所作用的对象. 非静态成员函数中可以直接使用this来代表指向该函数作用的对象的指针. 静态成员函数中不能使用this指针. 静态成员 static 定义 ... 
- CCF计算机网络会议日期
			SenSys: November 5-8 2017, Deadline: April 3, 2017 CoNEXT: December 12-15 2017, Deadline: June 12, 2 ... 
- python2.7 qt4
			https://jaist.dl.sourceforge.net/project/pyqt/PyQt4/PyQt-4.11.4/PyQt4-4.11.4-gpl-Py2.7-Qt4.8.7-x64.e ... 
- C# linq 最大、最小对象的扩展
			public static class LinqExtension { public static T MaxBy<T, TR>(this IEnumerable<T> en, ... 
- JS基础---常见的Bom对象
			BOM(Browser Object Mode)浏览器对象模型,是Javascript的重要组成部分.它提供了一系列对象用于与浏览器窗口进行交互,这些对象通常统称为BOM. 一张图了解一下先 1.wi ... 
- MySQL 的 DISTINCT 应用于2列时
			SELECT DISTINCT vend_id告诉MySQL只返回不同(唯一)的 vend_id行,也就是在vend_id 有重复的行中,只保留一行,其他的不作输出.比如我创建了如下的student表 ... 
- d3 data()数据绑定中的key函数
			官网https://github.com/d3/d3-selection/blob/master/README.md#selection_data var data = [ {name: " ... 
- Golang获得执行文件的当前路径
			运行环境:golang1.9.2+win7x64golang1.9.2+centos6.5×64 /*获取当前文件执行的路径*/ func GetCurPath() string { file, _ ... 
