python3.6—读取图片,处理图片,新建图片
参考:
https://www.cnblogs.com/kongzhagen/p/6295925.html
https://www.cnblogs.com/denny402/p/5096001.html
参考:https://www.shiyanlou.com/questions/5275
# -*- coding: utf-8 -*-
from PIL import Image
from PIL import ImageDraw
#参考http://ju.outofmemory.cn/entry/34825
#img = Image.new("1",(80,40),"white")#单色模式
#img = Image.new("L",(80,40),200)#灰色模式
img = Image.new("RGBA",(80,40),(10,255,100))#RGBA的真色模式
draw = ImageDraw.Draw(img)
draw.text((0,0),"hello world!")
img.show()
img = img.convert("RGB")#jpg格式必须转换,单色和灰色不用
img.save("e:/pic/hl1.jpg")
遍历图片中的所有像素。
参考:http://fengmm521.blog.163.com/blog/static/25091358201512013147207/
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 7 15:38:13 2018
@author: Administrator
"""
from PIL import Image
import sys
img = Image.open("e:/pic/222.jpg")
#img.show(img)#显示读取的图片
#img.save("e:/pic/222.png")#保存别的格式
#用image模块,可以用getpixel获得像素值,可以遍历整个图片像素
width=img.size[0]
height=img.size[1]
print ('/*width:%d */' %(width))
print ('/*height:%d */' %(height))
count = 0
for h in range(0,height):
for w in range(0,width):
pixel=img.getpixel((w,h))
for i in range(0,3):
count = (count+1)%16
if (count == 0):
print ("0x%02x,/n" %(pixel[i]))
else:
print ("0x%02x," %(pixel[i]))
#使用像素把图片截取出来并保存
box = (0,0,80,40)#box变量是一个四元组(左,上,右,下)。
region=img.crop(box)
region.save("e:/pic/out.png")