from PIL import Image
im = Image.open(r'd:\test2.png')
r, g, b, a = im.split()
im = Image.merge("RGB", (r, g, b))
im.save(r'D:\Python_Programming.jpg')
===============================
#coding:utf-8
from PIL importImage, ImageDraw, ImageFontdefadd_text_to_image(image, text):
font= ImageFont.truetype('C:\Windows\Fonts\STXINGKA.TTF', 36)#添加背景
new_img = Image.new('RGBA', (image.size[0] * 3, image.size[1] * 3), (0, 0, 0, 0))
new_img.paste(image, image.size)#添加水印
font_len =len(text)
rgba_image= new_img.convert('RGBA')
text_overlay= Image.new('RGBA', rgba_image.size, (255, 255, 255, 0))
image_draw=ImageDraw.Draw(text_overlay)for i in range(0, rgba_image.size[0], font_len * 40 + 100):for j in range(0, rgba_image.size[1], 200):
image_draw.text((i, j), text, font=font, fill=(0, 0, 0, 50))
text_overlay= text_overlay.rotate(-45)
image_with_text=Image.alpha_composite(rgba_image, text_overlay)#裁切图片
image_with_text = image_with_text.crop((image.size[0], image.size[1], image.size[0] * 2, image.size[1] * 2))returnimage_with_textif __name__ == '__main__':
img= Image.open(r"d:\ww.jpg")
im_after= add_text_to_image(img, u'群主是个大流X')
im_after.save(r'd:\ww.png')
彩色转黑白
from PIL importImageimportmatplotlib.pyplot as pltimportnumpy as npimport matplotlib.cm as cm #cm是colormap的缩写
image_gray=Image.open('D:/ww.png').convert("L")
data=np.array(image_gray) #在显示灰度图像时array()方法将图像转换成Numpy的数组对象,图片得以显式,否则会出现错误
plt.imshow(data,cmap=cm.gray)#cmap:代表颜色图谱,默认绘制为RGB(A)颜色空间。
plt.savefig("d:/ww1.png")
plt.show()
裁剪
from PIL importImageimportmatplotlib.pyplot as pltimportnumpy as np
image=Image.open('D:/ww.jpg')
box=(100,100,400,400)
region=image.crop(box)
data=np.array(region)
plt.imshow(data)
plt.show()
旋转
from PIL importImageimportmatplotlib.pyplot as plt
image=Image.open('D:/ww.png')#读取图像
plt.imshow(image.rotate(180))#逆时针旋转180度
plt.show()#需要调用show()方法,不然图像只会在内存中而不显示出来