2012-02-29 17 views
32

मैं कुछ बहुत ही सरल छवि कुशलता के लिए पाइथन इमेजिंग लाइब्रेरी का उपयोग कर रहा हूं, हालांकि मुझे एक ग्रेस्केल छवि को एक मोनोक्रोम (काला और सफेद) में परिवर्तित करने में समस्या हो रही है) छवि। अगर मैं छवि को ग्रेस्केल में परिवर्तित करने के बाद सहेजता हूं (कन्वर्ट ('एल')) तो छवि की अपेक्षा की जाती है। हालांकि, अगर मैं छवि को एक मोनोक्रोम में परिवर्तित करता हूं, एकल-बैंड छवि यह मुझे शोर देता है जैसा कि आप नीचे दी गई छवियों में देख सकते हैं। क्या पीआईएल/पायथन का उपयोग कर एक शुद्ध काले और सफेद छवि में रंगीन पीएनजी छवि लेने का कोई आसान तरीका है?एक आरजीबी छवि को एक शुद्ध काले और सफेद छवि में बदलने के लिए पाइथन पीआईएल का उपयोग

from PIL import Image 
import ImageEnhance 
import ImageFilter 
from scipy.misc import imsave 
image_file = Image.open("convert_image.png") # open colour image 
image_file= image_file.convert('L') # convert image to monochrome - this works 
image_file= image_file.convert('1') # convert image to black and white 
imsave('result_col.png', image_file) 

Original Image Converted Image

+0

[जनहित याचिका प्रलेखन] से (http://www.pythonware.com/library/pil/handbook/image.htm): "" "एक द्विस्तरीय छवि को बदलने जब (मोड" 1 "), स्रोत छवि को पहले काले और सफेद में परिवर्तित किया जाता है। 127 से बड़े मानों का परिणाम फिर सफेद पर सेट किया जाता है, और छवि को कम किया जाता है। अन्य थ्रेसहोल्ड का उपयोग करने के लिए, बिंदु विधि का उपयोग करें। "" यह संबंधित लगता है, लेकिन मैं ' मैं पीआईएल और छवि कुशलता से परिचित नहीं हूँ। – Darthfett

उत्तर

48
from PIL import Image 
image_file = Image.open("convert_image.png") # open colour image 
image_file = image_file.convert('1') # convert image to black and white 
image_file.save('result.png') 

पैदावार

enter image description here

+0

इसके लिए धन्यवाद, मैं कोड के दूसरे भाग से imsave का उपयोग कर रहा था - मुझे एहसास नहीं हुआ कि यह एक समस्या का स्रोत होगा। – user714852

3

unutbu द्वारा प्राप्त परिणामों से आंकना मैं निष्कर्ष है कि scipy के imsave मोनोक्रोम (मोड 1) छवियों को नहीं समझता।

+0

मुझे संदेह है कि आप सही हैं - बहुत अजीब है कि। – user714852

13

एक अन्य विकल्प (जो वैज्ञानिक प्रयोजनों के लिए उपयोगी जैसे होता है जब आप विभाजन मास्क के साथ काम करने की जरूरत) बस एक सीमा लागू होता है:

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

"""Binarize (make it black and white) an image with Python.""" 

from PIL import Image 
from scipy.misc import imsave 
import numpy 


def binarize_image(img_path, target_path, threshold): 
    """Binarize an image.""" 
    image_file = Image.open(img_path) 
    image = image_file.convert('L') # convert image to monochrome 
    image = numpy.array(image) 
    image = binarize_array(image, threshold) 
    imsave(target_path, image) 


def binarize_array(numpy_array, threshold=200): 
    """Binarize a numpy array.""" 
    for i in range(len(numpy_array)): 
     for j in range(len(numpy_array[0])): 
      if numpy_array[i][j] > threshold: 
       numpy_array[i][j] = 255 
      else: 
       numpy_array[i][j] = 0 
    return numpy_array 


def get_parser(): 
    """Get parser object for script xy.py.""" 
    from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter 
    parser = ArgumentParser(description=__doc__, 
          formatter_class=ArgumentDefaultsHelpFormatter) 
    parser.add_argument("-i", "--input", 
         dest="input", 
         help="read this file", 
         metavar="FILE", 
         required=True) 
    parser.add_argument("-o", "--output", 
         dest="output", 
         help="write binarized file hre", 
         metavar="FILE", 
         required=True) 
    parser.add_argument("--threshold", 
         dest="threshold", 
         default=200, 
         type=int, 
         help="Threshold when to show white") 
    return parser 


if __name__ == "__main__": 
    args = get_parser().parse_args() 
    binarize_image(args.input, args.output, args.threshold) 

यह ./binarize.py -i convert_image.png -o result_bin.png --threshold 200 के लिए इस तरह दिखता है:

enter image description here

+3

'binarize_array' के लिए एक-लाइनर (जल्दी भी मुझे लगता है): ' numpy.where (numpy_array> threshold = 200, 255, 0) ' – Jacquot

+0

यह छवि को केवल काले और सफेद में परिवर्तित करने की मेरी Google खोज के लिए पूरी तरह से काम करता है ... धन्यवाद! – LampShade

0

क्योंकि पीआईएल convert("1") से मूल्य "ट्रू" या "गलत" मूल्य लौटाता है। इसे मुद्रित करने का प्रयास करें, दिखाया जाएगा: [False, False, True] एकल ब्रैकेट के साथ।

जबकि numpy सरणी इस [[False, False, True]] या [[0, 0, 1]] की तरह डबल ब्रैकेट का उपयोग करती है, है ना?

2

जैसा कि मार्टिन थॉम ने कहा है, आपको सामान्य रूप से थ्रेसहोल्डिंग लागू करने की आवश्यकता है। लेकिन आप इसे सरल वेक्टरलाइजेशन का उपयोग करके कर सकते हैं जो कि उस उत्तर में उपयोग किए जाने वाले लूप के मुकाबले बहुत तेज़ होगा।

नीचे दिया गया कोड छवि के पिक्सेल को 0 (काला) और 1 (सफेद) में परिवर्तित करता है।

from PIL import Image 
import numpy as np 
import matplotlib.pyplot as plt 

#Pixels higher than this will be 1. Otherwise 0. 
THRESHOLD_VALUE = 200 

#Load image and convert to greyscale 
img = Image.open("photo.png") 
img = img.convert("L") 

imgData = np.asarray(img) 
thresholdedData = (imgData > THRESHOLD_VALUE) * 1.0 

plt.imshow(thresholdedData) 
plt.show() 
संबंधित मुद्दे