Skip to content

Instantly share code, notes, and snippets.

@M2shad0w
Last active July 28, 2017 14:34
Show Gist options
  • Save M2shad0w/7529707cffc1a43f3d338206f6055045 to your computer and use it in GitHub Desktop.
Save M2shad0w/7529707cffc1a43f3d338206f6055045 to your computer and use it in GitHub Desktop.
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