This is the 16th day of my participation in Gwen Challenge

preface

Here’s some Python code for manipulating images. With this code, we can easily manipulate images in Python using the Pillow package

Install the Pillow pack

pip install Pillow
Copy the code

Image marking

from PIL import Image, ImageDraw
im = Image.open("blur.jpg")
draw = ImageDraw.Draw(im)
draw.line((0.0) + im.size, fill=128)
draw.line((0, im.size[1], im.size[0].0), fill=128)
del draw
# write to stdout
im.save("blur.jpg"."PNG")
Copy the code

Photos and write

Sometimes we may need to write some text on the picture, such as watermark, as long as we choose our local font file, can directly write on the picture

from PIL import ImageFont, ImageDraw,Image, ImageDraw
image = Image.open("image.png").convert('RGBA')
txt=Image.new('RGBA', image.size, (0.0.0.0))
font=ImageFont.truetype("c:/Windows/fonts/Tahoma.ttf".20)
draw=ImageDraw.Draw(txt)
draw.text((txt.size[0] -100, txt.size[1] -30), "linshiyingdi", font=font)
out=Image.alpha_composite(image, txt)
out.save("imageWithText.png"."png")
out.show()
Copy the code

Picture with picture watermarking

Text watermarking sometimes not beautiful, want to change the picture watermarking, also can

from PIL import ImageFont, ImageDraw, Image, ImageDraw

image = Image.open("image.png")
logo = Image.open("logo.png")
layer = Image.new('RGBA', image.size, (0.0.0.0))
layer.paste(logo, (50.60))
out = Image.composite(layer, image, layer)
out.save("imageWithLogo.png"."png")
out.show()
Copy the code

If the watermarked image is too large, you can zoom it out and use the image’s built-in thumbnail method

from PIL import ImageFont, ImageDraw, Image, ImageDraw

image = Image.open("image.png")
logo = Image.open("logo.png")
logo.thumbnail((200.100))
layer = Image.new('RGBA', image.size, (0.0.0.0))
layer.paste(logo, (50.60))
out = Image.composite(layer, image, layer)
out.save("imageWithLogo.png"."png")
out.show()
Copy the code

As shown below, our watermark image is placed in the upper left corner, and the size is also very appropriate.

Image fuzzy

from PIL import Image, ImageFilter
Open a JPG image file and place it directly in the current path.
im = Image.open('2822.jpg')
# Apply blur filter:
im2 = im.filter(ImageFilter.BLUR)
im2.save('blur.jpg'.'jpeg')
Copy the code

Image capture

from PIL import Image
img = Image.open("image.png")
print(img.size)
cropped = img.crop((0.0.1303.734))  # (left, upper, right, lower)
cropped.save("imageCropped.png")
Copy the code

You can see that if the length and width of the image is smaller than 1303 and 734, there will be a transparent fill, which will feel ugly. You can make a judgment by comparing the corresponding length and width with these two numbers

from PIL import Image

img = Image.open("image.png")
print(img.size)
right = 1303
lower = 734
if img.size[0] < 1303:
    right = img.size[0]
if img.size[1] < 734:
    lower = img.size[1]
cropped = img.crop((0.0, right, lower))  # (left, upper, right, lower)
cropped.save("imageCropped.png")

Copy the code

So white edge was killed

conclusion

Life is short, I use Python, and with the help of the powerful Pillow, we can do all sorts of things with images in just a few lines of code.