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: ...
随机推荐
- cas4.2.4 登添加验证码
看了很多添加验证码的博文,唯独没有4.24的 重点看第3条,其余的和别人博文大致相同 1.首先在cas工程的web.xml增加验证码功能的支持 <!-- 验证码功能 --> &l ...
- 解决Geoserver请求跨域的几种思路,第二种思路用过
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景描述 跨域问题是浏览器同源安全制引起的特别常见的问题.不同前端语 ...
- springboot集成logback日志
简介 spring boot内部使用Commons Logging来记录日志,但也保留外部接口可以让一些日志框架来进行实现,例如Java Util Logging,Log4J2还有Logback. 如 ...
- SyncDictionary
using System; using System.Collections; using System.Collections.Generic; using System.Threading; us ...
- Codeforces 786 C. Till I Collapse
题目链接:http://codeforces.com/contest/786/problem/C 大力膜了一发杜教的代码感觉十分的兹瓷啊! 我们知道如果$k$是给定的我们显然是可以直接一遍$O(n)$ ...
- Video 对象方法 canPlayType()
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- ros 充电topic
#!/usr/bin/env python #coding=utf- import rospy from std_msgs.msg import String i= def talker(): glo ...
- Wiener’s attack python
题目如下: 在不分解n的前提下,求d. 给定: e = 140586954170153340715880103465867497905399132874997078029388987191993846 ...
- Hadoop如何将TB级大文件的上传性能优化上百倍?
这篇文章,我们来看看,Hadoop的HDFS分布式文件系统的文件上传的性能优化. 首先,我们还是通过一张图来回顾一下文件上传的大概的原理. 由上图所示,文件上传的原理,其实说出来也简单. 比如有个TB ...
- JS异步函数 返回值
1. js 异步的几种情况 : 1.1 异步操作由浏览器内核的 webcore 来执行: onclick 由浏览器内核的 DOM Binding 模块来处理,当事件触发的时候,回调函数会立即添加到任 ...