现在时间是:
当前位置:首 页 >> 数据采集>> 文章列表

Pillow 图片处理的使用

作者:   发布时间:2016-09-09 09:12:10   浏览次数:3899

终于考完了试,再次回归,近期主要是处理图片,完成一个小小的项目:

__author__ = 'www.yeayee.com'
# -*- coding: utf-8 -*-

from PIL import Image, ImageFilter, ImageDraw, ImageFont, ImageEnhance, ImageFilter image1 = Image.open('C:/Users/hengli/Desktop/1.jpg')
image2 = Image.open('C:/Users/hengli/Desktop/2.jpg')


def 图片大小(image):
w, h = image.size # 获得图片的大小(分辨率)
return w, h def 图片剪裁(image, p1, p2, p3, p4):
box = (p1, p2, p3, p4)
image = image.crop(box)
image.show()
return image


def 图片压缩(image, size1, size2):
image.thumbnail((size1, size2), Image.ANTIALIAS)
image.show()
return image


def 图片旋转(image, jiaodu):
image = image.rotate(jiaodu)
image.show()
return image


def 图片黑白转换1(image):
image = image.convert('L') # (8-bit pixels, black and white)
image.show()
return image


def 图片过滤(image):
image.filter(ImageFilter.DETAIL)


def 图片写字(image, p1, p2, text):
draw = ImageDraw.Draw(image)
draw.text((p1, p2), text)
image.show()


# w, h = image1.size
# 图片写字(image1, w-100, h-20, 'hello')
def 图片拼接(image1, image2):
images = (image1, image2)
w, h = image1.size target = Image.new('RGB', (w * 2, h))
left = 0
right = w for image in images:
temp = image.resize((w, h), Image.ANTIALIAS)
target.paste(temp, (left, 0, right, h))
left += w right += w target.show()


# 图片拼接(image1, image2)
def 图片黑白转换2(image):
image = image.convert('1') # (1-bit pixels, black and white, stored with one pixel per byte)
image.show()
return image


def 图片虚化(image):
image = image.convert('P') # (8-bit pixels, mapped to any other mode using a colour palette)
image.show()
return image


def 图片转换怀旧(image):
image = image.convert('LA')
image.show()
return image


def 图片锐化(image, qiangdu):
enhancer = ImageEnhance.Sharpness(image)
enhancer.enhance(qiangdu).show()


def 图片色彩增强(image, qiangdu):
enhancer = ImageEnhance.Color(image)
enhancer.enhance(qiangdu).show()


def 图片亮度增强(image, qiangdu):
enhancer = ImageEnhance.Brightness(image)
enhancer.enhance(qiangdu).show()


def 图片对比度增强(image, qiangdu):
enhancer = ImageEnhance.Contrast(image)
enhancer.enhance(qiangdu).show()


def 图片BlUR(image):
image = image.filter(ImageFilter.BLUR)
image.show()


def 图片MinFilter(image):
image = image.filter(ImageFilter.MinFilter)
image.show()


def 图片转换黑白线条(image):
image = image.filter(ImageFilter.CONTOUR)
image.show()


def 图片EMBOSS(image):
image = image.filter(ImageFilter.EMBOSS)
image.show()


def 图片FIND_EDGES(image):
image = image.filter(ImageFilter.FIND_EDGES)
image.show()


#
# def chu_li(t0,t1,b0):
# '''
# 1.二值化图片
# 2.分割图片独立字符
# '''
# img = Image.open(t0)
# img = img.convert("RGBA")
# pixdata=img.load()
#
# for y in range(img.size[1]):
# for x in range(img.size[0]):
# if pixdata[x,y][0]>b0:
# pixdata[x,y]=(255,255,255,1)#R<100时,白色填充
#
#
# for y in range(img.size[1]):
# for x in range(img.size[0]):
# if pixdata[x,y][1]>b0:
# pixdata[x,y]=(255,255,255,1) #G<100时,白色填充
#
#
# for y in range(img.size[1]):
# for x in range(img.size[0]):
# if pixdata[x,y][2]>b0:
# pixdata[x,y]=(255,255,255,1)#B<100时,白色填充
#
# img.save(t1)
#
# from os import listdir
# trainingFileList = listdir('ksk4')
# m = len(trainingFileList)
#
# for i in range(m):
# fileNameStr = trainingFileList[i]
# jpg_path0 = 'ksk4'+fileNameStr
# jpg_path1 = 'ksk4'+fileNameStr
# chu_li(jpg_path0,jpg_path1,170)
# print(i,jpg_path0,jpg_path1)
#

# chu_li()

仔细看上面已注释掉的代码,可以用来处理bmp图片,二值化、KNN形成识别库哦,还可以对已有图片进行像素级别的处理!

 

经过测试上面的代码,部分可能不能实现,如写字:

输不出汉字,一方面是开始对编码的混乱认识;二是与指定的中文字体大小有关,字体必须大于等于19才可以输出汉字,过小可能该库渲染不起来,具体原因不清楚。这点是我没想到的。写代码往往带给你不同的意外。但事后有的能解释有的不能。正确的姿势:

 

def 图片写字(image, p1, p2, text):
font = ImageFont.truetype('simsun.ttc',120)
draw = ImageDraw.Draw(image)
draw.text((p1, p2), text,(0,0,0),font=font)
image.show()
return image

im = Image.open('001.jpg')
print(im.format, im.size, im.mode)

w, h = im.size 图片写字(im, w/2, h/2, '我曹').save('004.jpg')

 

 







上一篇:没有了    下一篇:没有了

Copyright ©2018    易一网络科技|www.yeayee.com All Right Reserved.

技术支持:自助建站 | 领地网站建设 |短信接口 版权所有 © 2005-2018 lingw.net.粤ICP备16125321号 -5