Open In App

Wand sparse_color() function – Python

Improve
Improve
Like Article
Like
Save
Share
Report

The sparse_color() function is an inbuilt function in the Python Wand ImageMagick library which is used to interpolates color values between points on an image.
 

Syntax: 
 

sparse_color(method, colors)

Parameters: This function accepts three parameters as mentioned above and defined below: 
 

  • method: This parameter stores the method for interpolation. Some of the available methods are ‘barycentric’, ‘bilinear’, ‘shepards’, ‘voronoi’, ‘inverse’, ‘manhattan’.
  • color: This parameter stores the color along with points having x, y co-ordinates.

Return Value: This function returns the Wand ImageMagick object.

Original Image: 
 

Example 1: 
 

Python3




# Import library from Image
from wand.image import Image
from wand.color import Color
colors = {
Color('RED'): (100, 150),
Color('YELLOW'): (250, 150),
Color('ORANGE'): (300, 123)
}
 
# Import the image
with Image(filename ='../geeksforgeeks.png') as image:
    # Clone the image in order to process
    with image.clone() as sparse_color:
        # Invoke sparse_color function with 'bilinear' method
        sparse_color.sparse_color('bilinear', colors)
        # Save the image
        sparse_color.save(filename ='sparse_color1.jpg')


Output: 
 

Example 2: 
 

Python3




# Import libraries from the wand 
from wand.image import Image
from wand.drawing import Drawing
from wand.color import Color
colors = {
Color('GREEN'): (100, 100),
Color('BLUE'): (50, 50),
Color('RED'): (200, 303)
}
 
with Drawing() as draw:
    # Set Stroke color the circle to black
    draw.stroke_color = Color('black')
    # Set Width of the circle to 2
    draw.stroke_width = 1
    # Set the fill color to 'White (# FFFFFF)'
    draw.fill_color = Color('white')
 
    # Invoke Circle function with center at 50, 50 and radius 25
    draw.circle((200, 200), # Center point
                (100, 100)) # Perimeter point
    # Set the font style
    draw.font = '../Helvetica.ttf'
    # Set the font size
    draw.font_size = 30
     
    with Image(width = 400, height = 400, background = Color('# 45ff33')) as pic:
        # Set the text and its location
        draw.text(int(pic.width / 3), int(pic.height / 2), 'GeeksForGeeks !')
        # Draw the picture
        draw(pic)
        # Invoke sparse_color function method as barycentric
        pic.sparse_color('barycentric', colors)
        # Save the image
        pic.save(filename ='sparse_color2.jpg')


Output: 
 

 



Last Updated : 13 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads