PIL笔记
图片颜色的类型
1 (1-bit pixels, black and white, stored with one pixel per byte)
L (8-bit pixels, black and white)
P (8-bit pixels, mapped to any other mode using a color palette)
RGB (3x8-bit pixels, true color)
RGBA (4x8-bit pixels, true color with transparency mask)
CMYK (4x8-bit pixels, color separation)
YCbCr (3x8-bit pixels, color video format)
I (32-bit signed integer pixels)
F (32-bit floating point pixels)
从数组产生图片
从数组产生彩色图片
numpy天然就是处理图像数据的好工具,skimage库大量的使用numpy,实现了部分opencv的功能,非常值得研究。
from PIL import Image
import numpy as np
data = -np.ones((100, 3)).reshape(10, 10, 3).astype('byte')
data[:5, :5, 2] = 0 # 只保留RG两色,即为黄色
data[5:, 5:, 1] = 0
data[:5, 5:, 0] = 0
img = Image.fromarray(data, "RGB")
img.show()
从数组产生灰度图片
from PIL import Image
import numpy as np
"""
当使用fromarray产生图片时,图片的类型(1,L,P之类的)跟数组每个元素的大小必须匹配!
"""
img = Image.fromarray(np.array([255]*100).astype(np.byte).reshape(10,10), 'L')
print(list(img.getdata()))
img.show()
PIL中的柱状图
from PIL import Image
img=Image.open("haha.jpg")
res=img.histogram()
print(len(res))
使用调色板
from PIL import Image
import numpy as np
"""
如果mode=1,不管数组的实际长度,总是取其中的前width*height位,mode=1是黑白图
mode=L是灰度图
mode=P是调色板图,可以容纳256种颜色,它可以是彩图
"""
# 首先创建一个灰度图,其中每个像素都是122灰度
img = Image.fromarray((122 * np.ones((20, 20))).astype(np.byte), "P")
def method1():
# 创建一个pallete,注意pallete只能用于mode=L和mode=P的情况,用于将灰度图彩色化
pallete = np.zeros(256 * 3).astype(np.byte) # 与pallete数组的形状无关,只取前768个byte
# 将灰度为122的元素置为红色
pallete[122 * 3] = 255
img.putpalette(pallete)
def method2():
# 创建一个pallete,注意pallete只能用于mode=L和mode=P的情况,用于将灰度图彩色化
pallete = np.zeros(256 * 3).astype(np.byte) # 与pallete数组的形状无关,只取前768个byte
# 将灰度为122的元素置为红色
pallete[122 * 3] = 255
img.putpalette(pallete)
def method3():
pallete = [0] * 768
pallete[122 * 3] = 255
img.putpalette(pallete)
method2()
img.show()
# 灰度122映射为RGB应为(122,122,122),RGB三色相同必然是灰色
使用图片的info
"""
能否保留文件info信息取决于图片类型
"""
img = Image.fromarray((128 * np.ones((20, 20))).astype(np.byte), "L")
img.info['user'] = "weidiao"
print(img.info)
img.save("haha.pdf")
img = Image.open("haha.pdf")
print(img.info)
不论何时,总是想着全部掌握一个库都是一种愚蠢的思想。一方面是没有必要,另一方面是不太可能。
不论何时,浏览一下库的全部内容都是一种智慧,一遍记不住浏览两遍,浏览的次数多了,不仅有助于知道库的功能,还有助于了解一些好的设计。
PIL笔记的更多相关文章
- 【Python笔记】图片处理库PIL的源代码安装步骤
前段时间项目须要对某些图片打水印,用到Python的PIL库,本文以Imaging-1.1.7为例,记录PIL库的源代码编译/安装步骤. PIL全称Python Image Library.它支持多种 ...
- 潭州课堂25班:Ph201805201 爬虫基础 第九课 图像处理- PIL (课堂笔记)
Python图像处理-Pillow 简介 Python传统的图像处理库PIL(Python Imaging Library ),可以说基本上是Python处理图像的标准库,功能强大,使用简单. 但是由 ...
- Python PIL模块笔记
利用python pil 实现给图片上添加文字 图片中添加文字#-*- coding: utf-8 -*- from PIL import Image,ImageDraw,ImageFont ttfo ...
- Python PIL库学习笔记
1.PIL简介 Python Imaging Library(缩写为PIL)(在新的版本中被称为Pillow)是Python编程语言的开源库,它增加了对打开,操作和保存许多不同图像文件格式的支持.它适 ...
- Python 学习笔记之—— PIL 库
PIL,全称 Python Imaging Library,是 Python 平台一个功能非常强大而且简单易用的图像处理库.但是,由于 PIL 仅支持到Python 2.7,加上年久失修,于是一群志愿 ...
- 【笔记】PIL 中的 Image 模块
Image 模块提供了一个同名类(Image),也提供了一些工厂函数,包括从文件中载入图片和创建新图片.例如,以下的脚本先载入一幅图片,将它旋转 45 度角,并显示出来: 1 >>> ...
- Python PIL、Pillow笔记
原文链接:https://blog.csdn.net/FlashKoala/article/details/90649464 一.PIL.Pillow简介 PIL(Python Imaging Lib ...
- ejoy2d源码阅读笔记1
一直想学lua,学它如何与C结合来作逻辑,所以找了云风的一份代码来研究.这份代码是个框架库,叫ejoy2d,据云风的博客说,他们最新的手机游戏用的就是这套框架,所以实用性应该很强,虽然我不是学游戏的, ...
- Requests:Python HTTP Module学习笔记(一)(转)
Requests:Python HTTP Module学习笔记(一) 在学习用python写爬虫的时候用到了Requests这个Http网络库,这个库简单好用并且功能强大,完全可以代替python的标 ...
随机推荐
- Subset II leetcode java
题目: Given a collection of integers that might contain duplicates, S, return all possible subsets. No ...
- 根据ip地址获取用户所在地
java代码: package com.henu.controller; import java.io.BufferedReader; import java.io.IOException; impo ...
- 微信小程序显示html格式内容(wxParse使用及循环解析数据渲染)
小程序默认是不支持html格式的内容显示的,那我们需要显示html内容的时候,就可以通过wxParse来实现. 首先我们下载wxParse,github地址:https://github.com/ic ...
- linux下性能分析命令[总结]
1.前言 在linux下开发程序,为了追求高性能,经常需要测试程序的性能,包括cpu.内存.io.网络等等使用情况.liunx下提供了众多命令方便查看各种资源的使用情况.经常用的有ps.top.fre ...
- GIF添加3D加速
由于浏览器内核对Gif格式的图片会产生卡的情况,所以我们需要告诉浏览器,开启一下加速,方法很简单,就是利用css3的特性,强制告诉浏览器,这是个元素,需要3D转换,请务必开启加速效果 方法1 给gif ...
- 阿里云centos安装ftp与svn过程
1.下载xshell或者secureCRT 2.登录centos或者服务器 3.安装vsftpd [root@xxx]# yum install vsftpd //安装vsftpd [root@xxx ...
- [Canvas]奔跑的马
下载地址:https://files.cnblogs.com/files/xiandedanteng/52-WalkingHorse.rar,请用Chrome浏览器打开观看动态效果. 图例: 源码: ...
- 【S6】当心C++编译器最烦人的分析机制
1.考虑一个包含int的文件,复制到list,如下: ifstream dataFile("ints.bat"); list<int> data(istream_ite ...
- Android 演示 Android ListView 和 github XListView(1-3)
本文内容 环境 项目结构 演示 1:ListView 演示 2:简单 XListView 演示 3:音乐列表 XListView 演示 4:另一个音乐列表 XListView 本文四个演示,循序渐进. ...
- oracle expdp/impdp 用法详解
http://hi.baidu.com/hzfsai/item/4a4b3fc4b1cf7e51ad00efbd oracle expdp/impdp 用法详解 Data Pump 反映了整个导出/导 ...