python中paste函数的作用_PIL使用小结(crop和paste函数)

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

from PIL import Image

或者

import Image

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

cca66094-377e-39b7-9cf9-cbad8da5b3bb.png

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

实现的代码如下:

import Image

import sys

if len(sys.argv)<2:

print '%s ' % __file__

sys.exit()

else:

filename = sys.argv[1]

img = Image.open(filename)

width = img.size[0]

height = img.size[1]

img1 = img.crop((0,0,width,9))

#img1 = Image.new('RGBA',(width,10))

img.paste(img1,(0,height-9))

img.save(filename)

img = Image.open(filename)

img.show()

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

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

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

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

ee017baf-1306-34e5-a627-927cafd9b5f9.png

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

#!-*- encoding: utf-8 -*-

#!/usr/bin/python

from pychartdir import *

# The data for the bar chart

data = [450, 560, 630, 800, 1100, 1350, 1600, 1950, 2300, 2700, 3200, 3800]

# The labels for the bar chart

labels = ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月",

"十月", "十一月", "十二月"]

# Create a XYChart object of size 600 x 360 pixels

c = XYChart(600, 360)

# Add a title to the chart using 18pts Times Bold Italic font

c.addTitle("卖家月份销售图表", "simsun.ttc", 18)

# Set the plotarea at (60, 40) and of size 500 x 280 pixels. Use a vertical gradient

# color from light blue (eeeeff) to deep blue (0000cc) as background. Set border and

# grid lines to white (ffffff).

c.setPlotArea(60, 40, 500, 280, c.linearGradientColor(60, 40, 60, 280, 0xeeeeff,

0x0000cc), -1, 0xffffff, 0xffffff)

# Add a multi-color bar chart layer using the supplied data. Use soft lighting effect

# with light direction from left.

c.addBarLayer3(data).setBorderColor(Transparent, softLighting(Left))

# Set x axis labels using the given labels

c.xAxis().setLabels(labels)

# Draw the ticks between label positions (instead of at label positions)

c.xAxis().setTickOffset(0.5)

# Add a title to the y axis with 10pts Arial Bold font

c.yAxis().setTitle("人民币 (元)", "simsun.ttc", 10)

# Set axis label style to 8pts Arial Bold

c.xAxis().setLabelStyle("simsun.ttc", 8)

c.yAxis().setLabelStyle("simsun.ttc", 8)

# Set axis line width to 2 pixels

c.xAxis().setWidth(2)

c.yAxis().setWidth(2)

# Output the chart

c.makeChart("1.png")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 Python 处理图片需要使用第三方库 Pillow,可以通过 pip 安装。 首先,使用 Pillow 库打开一张图片: ```python from PIL import Image image = Image.open("image.jpg") ``` 要将这个图片作为背景,可以使用以下代码: ```python from PIL import Image background = Image.open("background.jpg") image = Image.open("image.jpg") # 将图片放到背景央 x = (background.width - image.width) // 2 y = (background.height - image.height) // 2 background.paste(image, (x, y)) # 保存新图片 background.save("new_image.jpg") ``` 这段代码会将 `image.jpg` 放到 `background.jpg` 的央,然后保存为 `new_image.jpg`。 接下来,如果要裁剪白色背景上的图像,可以使用以下代码: ```python from PIL import Image image = Image.open("image.jpg") background = Image.new("RGB", image.size, (255, 255, 255)) # 从左上角开始扫描,找到第一个不是白色的像素 left = image.width top = image.height right = 0 bottom = 0 for x in range(image.width): for y in range(image.height): if image.getpixel((x, y)) != (255, 255, 255): left = min(left, x) top = min(top, y) right = max(right, x) bottom = max(bottom, y) # 裁剪图像 image = image.crop((left, top, right + 1, bottom + 1)) # 保存新图片 image.save("new_image.jpg") ``` 这段代码会创建一个大小和 `image.jpg` 相同的白色背景,然后从左上角开始扫描,找到第一个不是白色的像素,记录下左上角和右下角的坐标,然后裁剪出图像。最后,将裁剪出的图像保存为 `new_image.jpg`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值