Skip to content

Instantly share code, notes, and snippets.

@felix021 felix021/switch_pic
Created Sep 14, 2014

Embed
What would you like to do?
交换图片的左右两半
#!/usr/bin/python
#coding:utf-8
import sys
import PIL
from PIL import Image
if len(sys.argv) < 2:
raise Exception("bad parameter")
fn = sys.argv[1]
im = Image.open(fn)
width, height = im.size
mid = width / 2
box1 = (0, 0, mid, height)
box2 = (mid, 0, width, height)
region1 = im.crop(box1)
region2 = im.crop(box2)
im1 = Image.new('RGBA', (width, height))
im1.paste(region2, (0, 0))
im1.paste(region1, (mid, 0))
im1.save('x_' + fn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.