PIL----crop()和paste()

PIL(Python Imaging Library)是python语言中对图像处理方面的一个开源库,其主要功能模块为Image,对于Image模块,可以使用

Python代码   收藏代码
  1. from PIL import Image  

或者

Python代码   收藏代码
  1. import Image  

 由于使用了试用版的chartdir库,在生成图片的时候下面会出现一行提示是非注册版的文字,看起来不太舒服。


 

所以想使用PIL自动地将下面一行去掉,查阅了一下PIL的文档,最后决定使用PIL的crop和paste函数实现功能

实现的代码如下:

Python代码   收藏代码
  1. import Image  
  2. import sys  
  3.   
  4. if len(sys.argv)<2:  
  5.     print '%s <image file>' % __file__  
  6.     sys.exit()  
  7. else:  
  8.     filename = sys.argv[1]  
  9.       
  10. img = Image.open(filename)  
  11.   
  12. width = img.size[0]  
  13. height = img.size[1]  
  14.   
  15. img1 = img.crop((0,0,width,9))  
  16.   
  17. #img1 = Image.new('RGBA',(width,10))  
  18. img.paste(img1,(0,height-9))  
  19. img.save(filename)  
  20.   
  21. img = Image.open(filename)  
  22. img.show()  

 这可以使用的方法有两种,第一种是以及被注释掉的方法,即生成一个新的Image,调用Image.new方法。然后将该image粘贴到需要修改的图片上。另外一种为了保持图片的前后背景色一致,从图片的最前头拷贝一部分图片(使用crop函数),然后在粘贴到需要修改的图片上,来完成最下端文字的覆盖。

 

crop函数带的参数为(起始点的横坐标,起始点的纵坐标,宽度,高度)

paste函数的参数为(需要修改的图片,粘贴的起始点的横坐标,粘贴的起始点的纵坐标)

 

下面是处理结果之后的图片:



 

PS.使用chartdir生成图片的Python脚本

Python代码   收藏代码
  1. #!-*- encoding: utf-8 -*-  
  2.   
  3. #!/usr/bin/python  
  4. from pychartdir import *  
  5.   
  6. # The data for the bar chart  
  7. data = [45056063080011001350160019502300270032003800]  
  8.   
  9. # The labels for the bar chart  
  10. labels = ["一月""二月""三月""四月""五月""六月""七月""八月""九月",  
  11.     "十月""十一月""十二月"]  
  12.   
  13. # Create a XYChart object of size 600 x 360 pixels  
  14. c = XYChart(600360)  
  15.   
  16. # Add a title to the chart using 18pts Times Bold Italic font  
  17. c.addTitle("卖家月份销售图表""simsun.ttc"18)  
  18.   
  19. # Set the plotarea at (60, 40) and of size 500 x 280 pixels. Use a vertical gradient  
  20. # color from light blue (eeeeff) to deep blue (0000cc) as background. Set border and  
  21. # grid lines to white (ffffff).  
  22. c.setPlotArea(6040500280, c.linearGradientColor(6040602800xeeeeff,  
  23.     0x0000cc), -10xffffff0xffffff)  
  24.   
  25. # Add a multi-color bar chart layer using the supplied data. Use soft lighting effect  
  26. # with light direction from left.  
  27. c.addBarLayer3(data).setBorderColor(Transparent, softLighting(Left))  
  28.   
  29. # Set x axis labels using the given labels  
  30. c.xAxis().setLabels(labels)  
  31.   
  32. # Draw the ticks between label positions (instead of at label positions)  
  33. c.xAxis().setTickOffset(0.5)  
  34.   
  35. # Add a title to the y axis with 10pts Arial Bold font  
  36. c.yAxis().setTitle("人民币 (元)""simsun.ttc"10)  
  37.   
  38. # Set axis label style to 8pts Arial Bold  
  39. c.xAxis().setLabelStyle("simsun.ttc"8)  
  40. c.yAxis().setLabelStyle("simsun.ttc"8)  
  41.   
  42. # Set axis line width to 2 pixels  
  43. c.xAxis().setWidth(2)  
  44. c.yAxis().setWidth(2)  
  45.   
  46. # Output the chart  
  47. c.makeChart("1.png")  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值