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?的更多相关文章

  1. 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 ...

  2. Byte Array to Hexadecimal String

    Lookup Text: 23,879.41 (20.8X faster) Sentence: 1.15 (23.9X faster) /// <summary> /// Hex stri ...

  3. C# byte array 跟 string 互转

    用 System.Text.Encoding.Default.GetString() 转换时,byte array 中大于 127 的数据转 string 时会出问题. 把这里的 Default 换成 ...

  4. C#中使用Buffer.BlockCopy()方法将string转换为byte array的方法:

    public static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count); 将指定数目的字 ...

  5. PHP write byte array to file

    /********************************************************************************* * PHP write byte ...

  6. XAF 如何将数据库中Byte array图片显示出来

    问题比较简单,直接上代码. private Image _Cover; [Size(SizeAttribute.Unlimited), ValueConverter(typeof(ImageValue ...

  7. [原] XAF 如何将数据库中Byte array图片显示出来

    问题比较简单,直接上代码. private Image _Cover; [Size(SizeAttribute.Unlimited), ValueConverter(typeof(ImageValue ...

  8. DBus send byte array over gdbus ----Send dbus data

    遇到一个问题,如何通过dbus传送uint8数组元素 有3种方法, 1.直接传 ay 2.传  a(y) 3.xml定义为 ay,但是通过annotation 强行将 guchar 转为GVarian ...

  9. 「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: ...

随机推荐

  1. (zhuan) How to Train Neural Networks With Backpropagation

    this blog from: http://blog.demofox.org/2017/03/09/how-to-train-neural-networks-with-backpropagation ...

  2. [分享]Active-HDL 9.2 安装

    Download 点击下载Active-HDL 9.2 How to Install ? 解压后依次进行以下操作 1.运行Active_HDL_9.2sp1_main_setup.exe,允许程序所有 ...

  3. Ajax - 汇总

    1,什么是ajax? 为什么要使用ajax? 1.ajax是"asynchornous javascript and xml "的缩写,指一种创建交互式网页应用的网页开发技术. 2 ...

  4. 【Python】【有趣的模块】【Requests】session & cookie

    保存http请求的状态(请求的上下文) [区别&联系] 1. cookie保存在客户端的浏览器,比如标识是哪个请求者.购物车应用等 session保存在服务端,http连接时无则创建,有则用现 ...

  5. phantomjs 下载

    http://phantomjs.org/download.html

  6. iterrows(), iteritems(), itertuples()对dataframe进行遍历

      iterrows(): 将DataFrame迭代为(insex, Series)对. itertuples(): 将DataFrame迭代为元祖. iteritems(): 将DataFrame迭 ...

  7. 要使用myConfig.properties配置文件作为实体类的映射文件的话,格式要用=,最关键的要和实例类中通过反射获取值的KEY要一样,不样会反射取不到值

    ABC=https://fsdfsdf.iy.comABCId=L2345345ZhP345ABCKey=sfdf4234f234dhE6Ut0aABCName=Gassd010 上面是myConfi ...

  8. Dreamweaver 1 网页制作

    1.站点 1.1 创建站点 点击菜单栏中站点进行站点创建,输入站点名称,路径 1.2 设置图像文件夹 1.3 站点管理 站点的编辑.复制.删除 2.页面属性栏 2.1 外观 1.设置页面整体的字体.大 ...

  9. Java中的包扫描(工具)

    在现在好多应用场景中,我们需要得到某个包名下面所有的类, 包括我们自己在src里写的java类和一些第三方提供的jar包里的类,那么怎么来实现呢? 今天带大家来完成这件事. 先分享代码: 1.这个类是 ...

  10. office2016如何激活

    office2016如何激活 一.总结 一句话总结:office2016没有激活成功,下了一个office2013破解版 office2013破解版 二.自己做法 三中的方法点赞蛮多的(100多个), ...