Skip to content

Instantly share code, notes, and snippets.

@M2shad0w
Last active Jul 28, 2017
Embed
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
else:
pixdata[x, y] = 255
return img
def main():
image = Image.open(file_name)
image = image.convert("L")
binarizing(image, 30)
image.save("trans_"+file_name)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment