Home  >  Article  >  Backend Development  >  Python 2 methods to implement superimposed rectangular frame layers

Python 2 methods to implement superimposed rectangular frame layers

Guanhui
Guanhuiforward
2020-06-18 17:44:322391browse

Python 2 methods to implement superimposed rectangular frame layers

Two methods and effects:

Method one, use PIL.Image.blend method:

from PIL import Image, ImageDraw
im = Image.open('d:/tmp/58.249.0.220_01_20200604141800866_TIMING.jpg', 'r')
im2 = Image.open('d:/tmp/58.249.0.220_01_20200604141800866_TIMING.jpg', 'r')
draw = ImageDraw.Draw(im2)
draw.rectangle([(1000, 500), (1200, 800)], fill=(255, 0, 0), width=2)
out = Image.blend(im, im2, 0.5)
out.save('d:/tmp/demo1.jpg')
im.close()
im2.close()
out.show()
out.close()

Method 2: Direct pixel overlay:

from PIL import Image, ImageDraw

im = Image.open('d:/tmp/58.249.0.220_01_20200604141800866_TIMING.jpg', 'r')
b = (255, 0, 0)
opacity = 0.5

for x in range(1000, 1200):
  for y in range(500, 800):
    p = im.getpixel((x, y))
    p = [int(p[i]*(1-opacity) + b[i]*opacity) for i in range(3)]
    im.putpixel((x, y), tuple(p))

im.save('d:/tmp/demo2.jpg')
im.show()
im.close()

The two renderings are as follows:

Explanation: The first method can draw a rectangle There are some well-defined shapes such as ellipses, but the second type has better ability to control the shape independently. As long as the corresponding data formula is edited into the program, you can draw some desired outlines;

Chapter The two methods are defined by the first method (

out = image1 * (1.0 - alpha) image2 * alpha)

Then think of the color blindness test chart and another picture ( I don’t know what the name is, I just came up with it because different people in the picture may see different things.)

Recommended tutorial: "Python Tutorial"

The above is the detailed content of Python 2 methods to implement superimposed rectangular frame layers. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete
Previous article:Python decorator detailsNext article:Python decorator details