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

Python 2 methods to implement superimposed rectangular frame layers

Guanhui
Release: 2020-06-18 17:44:32
forward
2502 people have browsed it

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()
Copy after login

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()
Copy after login

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!

Related labels:
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template