Skip to content

Instantly share code, notes, and snippets.

Last active Jul 28, 2017
What would you like to do?
image_trans 图像去黑色背景
# -*- coding: utf-8 -*-
# !/usr/bin/env python
import sys
from PIL import Image, ImageDraw
file_name = sys.argv[1]
def binarizing(img,threshold): # input: gray image
pixdata = img.load()
w, h = img.size
for y in range(h):
for x in range(w):
if pixdata[x, y] > threshold:
pixdata[x, y] = 0
pixdata[x, y] = 255
return img
def main():
image =
image = image.convert("L")
binarizing(image, 30)"trans_"+file_name)
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment