python3.6---读取图片,处理图片,新建图片

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")
  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐为波

看着给就好了,学习写作有点累!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值