Last active
July 28, 2017 14:34
-
-
Save M2shad0w/7529707cffc1a43f3d338206f6055045 to your computer and use it in GitHub Desktop.
image_trans 图像去黑色背景
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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