How to Change the Gradient Color of the QR Code Image Generated by Python's Module QR Code?


BarcodeBC > Articles > How to Change the Gradient Color of the QR Code Image Generated by Python's Module QR Code?


Change the Gradient Color of the QR Code Image Generated by Python's Module QR Code?

The qrcode module in Python generates QR Codes with a default gradient color. However, you can change the gradient color of the QR Code image by using the ImageColor and Image modules from the Python Imaging Library (PIL).

Here's an example code that demonstrates how to generate a QR Code image with a custom gradient color using Python's qrcode and PIL modules.


import qrcode
from PIL import ImageColor, Image

# Generate QR Code with qrcode module
qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=10,
    border=4,
)
qr.add_data("https://www.example.com")
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")

# Convert the image to RGB mode and get its size
img = img.convert("RGB")
width, height = img.size

# Define the gradient colors
start_color = ImageColor.getrgb("#FFD700")
end_color = ImageColor.getrgb("#FF8C00")

# Create a new gradient image
gradient = Image.new("RGB", (width, height), color=end_color)

# Draw the gradient
for x in range(width):
    for y in range(height):
        distance = x / width
        r = int(start_color[0] * distance + end_color[0] * (1 - distance))
        g = int(start_color[1] * distance + end_color[1] * (1 - distance))
        b = int(start_color[2] * distance + end_color[2] * (1 - distance))
        gradient.putpixel((x, y), (r, g, b))

# Merge the QR Code image and the gradient
gradient = gradient.convert("RGBA")
img = img.convert("RGBA")
result = Image.alpha_composite(img, gradient)

# Save the final image
result.save("qr_code.png")

In this code, we first generate a QR Code using the qrcode module and convert it to an RGB image using the convert() method. We then define the start and end colors of the gradient using the ImageColor.getrgb() method. We create a new gradient image with the same size as the QR Code image using the Image.new() method and draw the gradient using nested for loops. We then merge the QR Code image and the gradient using the Image.alpha_composite() method and save the final image as a PNG file.


Why does the piece of code color_mask=RadialGradiantColorMask() adds the gradient color to the QR Code but is not unable to change the colors?

It's difficult to say without seeing the full code and the implementation of the RadialGradiantColorMask() class. However, it's possible that the RadialGradiantColorMask() class is designed to create a radial gradient color mask for the QR Code that uses predefined color values, rather than allowing you to change the colors dynamically.

If you want to change the colors of the QR Code generated using a radial gradient color mask, you might need to modify the implementation of the RadialGradiantColorMask() class to accept color values as input parameters and use those values to create the gradient mask. Alternatively, you could use a different implementation of a radial gradient color mask that allows you to specify the start and end colors of the gradient.

For example, you could use the PIL (Python Imaging Library) module to create a radial gradient color mask with custom colors and apply it to the QR Code image. Here's an example code that demonstrates how to do this.


import qrcode
from PIL import Image, ImageDraw, ImageOps

# Define the start and end colors of the radial gradient
start_color = (255, 0, 0) # Red
end_color = (0, 0, 255) # Blue

# Generate the QR Code image
qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4)
qr.add_data("https://www.example.com")
qr.make(fit=True)
qr_img = qr.make_image(fill_color="black", back_color="white")

# Create a radial gradient color mask
mask_size = (qr_img.size[0] + 2, qr_img.size[1] + 2)
gradient = Image.new("L", mask_size, 0)
draw = ImageDraw.Draw(gradient)
draw.ellipse((1, 1, mask_size[0] - 1, mask_size[1] - 1), fill=255, outline=0)
gradient = ImageOps.gaussian_blur(gradient, 5)
gradient = gradient.crop((1, 1, mask_size[0] - 1, mask_size[1] - 1))
gradient = ImageOps.colorize(gradient, start_color, end_color)

# Apply the gradient mask to the QR Code image
qr_img = qr_img.convert("RGBA")
gradient = gradient.convert("RGBA")
qr_img.putalpha(gradient.getchannel("L"))

# Save the final QR Code image with the radial gradient color mask
qr_img.save("qr_code.png")

In this code, we define the start and end colors of the radial gradient using RGB tuples. We then generate the QR Code image using the qrcode module and create a new L mode image for the radial gradient mask using the Image.new() method. We draw an ellipse on the gradient image using the ImageDraw.Draw() method, blur the gradient using the ImageOps.gaussian_blur() method, and colorize the gradient using the ImageOps.colorize() method. We then apply the gradient mask to the QR Code image using the putalpha() method and save the final image as a PNG file.



Change the Color of a QR Code in Python

To change the color of a QR Code in Python, you can use the qrcode library. Here's an example of how to generate a QR Code with a custom color.


import qrcode

# Generate the QR Code
qr = qrcode.QRCode(version=1, box_size=10, border=5)
qr.add_data("https://www.example.com")
qr.make(fit=True)

# Change the color of the QR Code
img = qr.make_image(fill_color="red", back_color="white")

# Save the image to a file
img.save("example_qr_code.png")

In this example, we first create a QRCode object using the qrcode library, and then add the data we want to encode. Next, we use the make_image method to generate an image of the QR Code, and we pass in the fill_color parameter to specify the color we want to use for the QR Code, and the back_color parameter to specify the background color.

You can use any color you want by specifying its name or RGB values in the fill_color parameter.



The Background Color of the QR Code in Python

The default background color of a QR Code generated by the qrcode library in Python is white. However, you can change the background color using the back_color parameter when calling the make_image method.

For example, to generate a QR Code with a black background, you can use the following code.


import qrcode

# Generate the QR Code
qr = qrcode.QRCode(version=1, box_size=10, border=5)
qr.add_data("https://www.example.com")
qr.make(fit=True)

# Change the background color of the QR Code to black
img = qr.make_image(fill_color="black", back_color="black")

# Save the image to a file
img.save("example_qr_code.png")

In this example, we pass the back_color parameter with the value "black" to specify that we want a black background color for the QR Code. You can use any color you want by specifying its name or RGB values in the back_color parameter.