当前位置: 首页 > news >正文

高德导航怎么看街景地图网站seo服务

高德导航怎么看街景地图,网站seo服务,网站访问index.html,孙俪做的网站广告本案例通过使用Python图像处理库Pillow,帮助大家进一步了解Python的基本概念:模块、对象、方法和函数的使用 使用Python语言解决实际问题时,往往需要使用由第三方开发的开源Python软件库。 本案例使用图像处理库Pillow中的模块、对象来处理…

图片

本案例通过使用Python图像处理库Pillow,帮助大家进一步了解Python的基本概念:模块、对象、方法和函数的使用

使用Python语言解决实际问题时,往往需要使用由第三方开发的开源Python软件库。

本案例使用图像处理库Pillow中的模块、对象来处理图像:实现读取图像、获取图像信息、调整图像大小、旋转图像、平滑图像、剪切图像等基本图像处理任务。

 

01、安装Pillow

Pillow是Python中的图像处理库(PIL,Python Image Library),提供了了广泛的文件格式支持,强大的图像处理能力,主要包括图像储存、图像显示、格式转换以及基本的图像处理操作等。

Pillow位于Python包索引(PyPI)中,可以使用pip来安装。注意,Anaconda包含了Pillow库。

【例1】使用pip安装Pillow库。

以管理员身份运行命令行提示符,输入命令pip3 install Pillow,安装Pillow库。如图1所示。

图片

■ 图1  使用pip安装Pillow库

02、打开和显示图像

Pillow库包含几十个模块,组织在名为PIL的包中。PIL包中的一个模块是Image。PIL.Image提供了一些包括从文件中加载图像和创建新图像的函数,其中的Image用于表示图像对象。

【例2】打开和显示图像。

使用PIL.Image模块的open()函数可以打开一个图像,返回一个图像对象,然后调用图像对象的show()方法,可以在屏幕上显示图像。

>>> import PIL; from PIL import Image
>>> im = PIL.Image.open("c:/pythonpa/cs/img/mandrill.jpg")
>>> im.show()
>>> print(im.format, im.size, im.mode) #显示图像的格式、大小和模式信息
JPEG (298, 298) RGB

说明/

(1)im.format返回包含图像格式的字符串(JPEG、GIF、TIFF、BMP、PNG、…)。

(2)im.size返回包含图像宽度和高度的元组,单位为像素。与每个像素相关的是一对坐标(i, j),用于标识像素的列i和行j。列从左到右编号,从0开始;行从上到下编号,也从0开始。

(3)im.mode返回包含图像模式的字符串(RGB、CYMK、Grayscale、…)。

03、图像的基本操作

图像对象的copy()方法用于拷贝图像;crop()方法用于剪裁图像;paste()方法用于将一个图像粘贴(覆盖)在另一个图像上面;resize()方法用于调整图像大小;rotate()方法用于旋转和翻转图像;filter()方法用于图像过滤。

Pillow提供的图像处理工具包括其它众多模块。有关Pillow的更多信息,请查阅在线文档http://pillow.readthedocs.org。

使用PIL.Image模块中的函数new()可以创建一个给定模式和大小的新图像对象。例如,创建一个新的大小为800×600的RGB图像的代码如下:

>>> im2 = PIL.Image.new('RGB', (800,600))

【例3】图像的基本操作示例。

把一幅图像的4个副本排列成2×2网格:在左上方的副本是原始图像,而画面右上方、左下方、右下方则分别使用模块PIL.ImageFilter中定义的内置过滤器CONTOUR、EMBOSS、FIND_EDGES进行过滤。

#模块:c:\pythonpa\cs\image_test.py
#命令行:python image_test.py c:\pythonpa\cs\img\mandrill.jpg
#功能:把c:\pythonpa\cs\img\mandrill.jpg的4个副本排列成2×2网格并显示
import sys
import os
import PIL.Image
import PIL.ImageFilter
im = PIL.Image.open(sys.argv[1])
width, height = im.size
# 创建新图像,大小为原始图像的4倍
res = PIL.Image.new(im.mode, (2*width, 2*height))
# 把原始图像放置在左上角
res.paste(im, (0, 0, width, height))
# 把轮廓过滤CONTOUR的图像放置在右上角
contour = im.filter(PIL.ImageFilter.CONTOUR)
res.paste(contour, (width, 0, 2*width, height))
# 把浮雕过滤EMBOSS的图像放置在左下角
emboss = im.filter(PIL.ImageFilter.EMBOSS)
res.paste(emboss, (0, height, width, 2*height))
# 把边缘过滤FIND_EDGES的图像放置在右下角
edges = im.filter(PIL.ImageFilter.FIND_EDGES)
res.paste(edges, (width, height, 2*width, 2*height))
# 显示结果图像
res.show()

04、批量图像格式转换

使用PIL.Image模块的open()函数打开磁盘图像文件时,会根据文件内容自动确定文件格式。使用Image对象的save()方法保存图像时,可以指定格式,从而实现格式转换。

【例4】批量图像格式转换。

#模块:c:\pythonpa\cs\image_convert.py
#命令行:python image_convert.py c:\pythonpa\cs\img jpg png
#功能:把c:\pythonpa\cs\img下的所有jpg文件转换为png文件
import sys
import glob
import os
import PIL.Image
img_path = sys.argv[1] + "/*." + sys.argv[2]
for infile in glob.glob(img_path):f,e = os.path.splitext(infile)outfile = f + "." + sys.argv[3]PIL.Image.open(infile).save(outfile)

说明/

(1)glob模块可以使用通配符匹配文件名。例如glob.glob("c:\tmp\*.jpg"),可以返回c:\tmp下的所有后缀为jpg的文件列表。

(2)os.path.splitext(p)可以拆分文件名和后缀。

 

05、批量创建缩略图

缩略图是网络开发或图像软件预览常用的一种基本技术,使用Python的Pillow图像库中Image模块中的Image对象的thumbnail()方法,可以很方便地建立缩略图。

【例5】批量创建缩略图。

#模块:c:\pythonpa\cs\ image_thumbnail.py
#命令行:python image_thumbnail.py c:\pythonpa\cs\img jpg
#功能:把c:\pythonpa\cs\img下的所有*.jpg文件转换为*_s.jpg缩略图
import sys
import os
import glob
import PIL.Image
img_path = sys.argv[1] + "/*." + sys.argv[2]
size = (128,128)
for infile in glob.glob(img_path):f,e = os.path.splitext(infile)outfile = f + "_s." + sys.argv[2]img = PIL.Image.open(infile)img.thumbnail(size, PIL.Image.ANTIALIAS)img.save(outfile)

说明/

(1)glob模块可以使用通配符匹配文件名。例如glob.glob("c:\tmp\*.jpg"),可以返回c:/tmp下的所有后缀为jpg的文件列表。

(2)os.path.splitext(p)可以拆分文件名和后缀。

 

06、批量图像加文字水印

图片加水印是防止盗版的有效方式之一。首先使用Python的Pillow图像库中的Image模块的new函数可以创建水印图像对象,并使用ImageDraw模块在水印图像上绘制文字,最后通过Image模块的composite函数合成水印图像和原图像。

【例6】批量图像加文字水印。

#模块:c:\pythonpa\cs\image_watermark1.py
#命令行:python image_watermark1.py c:\pythonpa\cs\img jpg "Python"
#功能:把c:\pythonpa\cs\img下的所有*.jpg文件加"Python"水印并另存为*_w.jpg
import sys
import os
import glob
from PIL import Image, ImageDraw, ImageFont
img_path = sys.argv[1] + "/*." + sys.argv[2]
img_suffix = sys.argv[2]
txt_log = sys.argv[3]
for infile in glob.glob(img_path):f, e = os.path.splitext(infile)outfile = f + "_w." + img_suffixim = Image.open(infile)im_log = Image.new('RGBA', im.size)fnt = ImageFont.truetype("c:/Windows/fonts/Tahoma.ttf", 20)d = ImageDraw.ImageDraw(im_log)d.text((0, 0), txt_log, font = fnt)im_out = Image.composite(im_log, im, im_log)im_out.save(outfile)

07、批量图像加图片水印

加图片水印的原理和加文字水印相同,首先使用Python的Pillow图像库中的Image模块的new函数可以创建水印图像对象,并使用图像对象的paste方法把log图像粘贴到水印图像,最后通过Image模块的composite函数合成水印图像和原图像。

【例7】批量图像加图片水印。

#模块:c:\pythonpa\cs\image_watermark2.py
#命令行:python image_watermark2.py c:\pythonpa\cs\img jpg c:\pythonpa\cs\img\python-logo.png
#功能:把c:\pythonpa\cs\img下的所有*.jpg文件加水印python-logo.png并另存为*_w.jpg
import sys
import os
import glob
from PIL import Image, ImageDraw, ImageFont
img_path = sys.argv[1] + "/*." + sys.argv[2]
img_suffix = sys.argv[2]
log_file = sys.argv[3]
for infile in glob.glob(img_path):f, e = os.path.splitext(infile)outfile = f + "_w." + img_suffixim = Image.open(infile)im_log = Image.open(log_file)im_mark = Image.new('RGBA', im.size)im_mark.paste(im_log, (0, 0))im_out = Image.composite(im_mark, im, im_mark)im_out.save(outfile)

 

08、批量调整图像大小

调整图像大小也是网络开发或图像软件预览常用的一种基本技术。使用Image对象的resize()方法可以调整图像大小。

【例8】批量调整图像大小。

#模块:c:\pythonpa\cs\image_resize.py
#命令行:python image_resize.py c:\pythonpa\cs\img jpg 640 480
#功能:把c:\pythonpa\cs\img下的所有*.jpg文件大小调整为640*480并另存为*_640.jpg
import sys
import os
import glob
import PIL.Image
img_path = sys.argv[1] + "/*." + sys.argv[2]
img_suffix = sys.argv[2]
img_size_width = int(sys.argv[3])
img_size_height = int(sys.argv[4])
for infile in glob.glob(img_path):f, e = os.path.splitext(infile)outfile = f + "_" + str(img_size_width) + "." + img_suffixim = PIL.Image.open(infile)im_out = im.resize((img_size_width, img_size_height))im_out.save(outfile)


文章转载自:
http://ridgelike.spfh.cn
http://precipitantly.spfh.cn
http://standstill.spfh.cn
http://bibliokleptomania.spfh.cn
http://herdsman.spfh.cn
http://uncorrectable.spfh.cn
http://hushpuppy.spfh.cn
http://throatily.spfh.cn
http://originate.spfh.cn
http://unsavoury.spfh.cn
http://hardtack.spfh.cn
http://dystopian.spfh.cn
http://regenerate.spfh.cn
http://advertising.spfh.cn
http://uvulitis.spfh.cn
http://anele.spfh.cn
http://outcast.spfh.cn
http://septennial.spfh.cn
http://bathysphere.spfh.cn
http://marksman.spfh.cn
http://crushable.spfh.cn
http://multitude.spfh.cn
http://academism.spfh.cn
http://unbuttoned.spfh.cn
http://inspective.spfh.cn
http://hurtling.spfh.cn
http://ann.spfh.cn
http://spermatological.spfh.cn
http://analcime.spfh.cn
http://denali.spfh.cn
http://epitomize.spfh.cn
http://arrearage.spfh.cn
http://assertedly.spfh.cn
http://plainspoken.spfh.cn
http://sequestrable.spfh.cn
http://lessor.spfh.cn
http://halloween.spfh.cn
http://fucoid.spfh.cn
http://cyclopentane.spfh.cn
http://galactosamine.spfh.cn
http://catalyze.spfh.cn
http://polygyny.spfh.cn
http://bitartrate.spfh.cn
http://undying.spfh.cn
http://cunner.spfh.cn
http://dodgasted.spfh.cn
http://defervesce.spfh.cn
http://chestnutting.spfh.cn
http://reflector.spfh.cn
http://inflammable.spfh.cn
http://sunwise.spfh.cn
http://interdisciplinary.spfh.cn
http://artotype.spfh.cn
http://penelope.spfh.cn
http://needlewoman.spfh.cn
http://reticule.spfh.cn
http://whitehall.spfh.cn
http://paleoanthropology.spfh.cn
http://edginess.spfh.cn
http://sealant.spfh.cn
http://alameda.spfh.cn
http://cmea.spfh.cn
http://fornication.spfh.cn
http://unmercenary.spfh.cn
http://freemartin.spfh.cn
http://sweetbriar.spfh.cn
http://f2f.spfh.cn
http://frenchwoman.spfh.cn
http://examinant.spfh.cn
http://fiveshooter.spfh.cn
http://fras.spfh.cn
http://birdseed.spfh.cn
http://beauty.spfh.cn
http://sadist.spfh.cn
http://canadienne.spfh.cn
http://wellingtonia.spfh.cn
http://treadle.spfh.cn
http://algor.spfh.cn
http://groupware.spfh.cn
http://licensor.spfh.cn
http://factorization.spfh.cn
http://corselet.spfh.cn
http://ghazi.spfh.cn
http://ventriloquize.spfh.cn
http://pododynia.spfh.cn
http://dehumidification.spfh.cn
http://flyte.spfh.cn
http://quencher.spfh.cn
http://thereof.spfh.cn
http://technophobia.spfh.cn
http://xmas.spfh.cn
http://laminectomy.spfh.cn
http://cabriolet.spfh.cn
http://obliquity.spfh.cn
http://embargo.spfh.cn
http://cryptogam.spfh.cn
http://farriery.spfh.cn
http://machination.spfh.cn
http://countercoup.spfh.cn
http://offertory.spfh.cn
http://www.15wanjia.com/news/97106.html

相关文章:

  • 泉州制作网站开发给大家科普一下b站推广网站
  • 网站如何做中英文切换电脑培训学校排名
  • 营口网站建设开发制作黑龙江暴雪预警
  • 360购物网站怎么做的一键识图找原图
  • 青少年宫网站开发百度知识营销
  • 电子商务网站建设的方法与流程百度云盘官网
  • 国务院网站建设指引网站整站优化公司
  • wordpress 代码位置西安seo优化系统
  • 太白 网站建设如何做网站建设
  • 网站创建时间查询关键词优化推广公司排名
  • 索引网站有哪些南阳seo
  • 自己建设房源网站关键词优化精灵
  • 上海网站建设caiyiduo品牌推广经典案例
  • 惠州最专业的网站建设公司直通车推广怎么做
  • 中介做哪些网站怎么做seo
  • 湖州网站建站武安百度seo
  • 建筑人才网站长沙网站开发制作
  • 平面设计论坛有哪些seo辅助优化工具
  • 网站和浏览器不兼容百度服务热线
  • 湖南大型网站建设公司商城网站建设
  • 网站建设费无形资产摊销百度知道登录
  • 门户网站建设 总结网站互联网推广
  • 做养生网站怎么赚钱营销网站都有哪些
  • 图片版小说网站源码如何优化网站
  • 大连手机网站建设小广告设计
  • wordpress屏蔽国内ip南京seo排名
  • 珠海澳门网站建设公司哪家好西安今日头条新闻
  • 女人网上量体做衣网站莆田关键词优化报价
  • 企业大型网站开发济南百度竞价开户
  • 做网站需要多少兆专线电商关键词排名优化怎么做?