Python uses the PIL library to rotate the picture

table of Contents

 

1. Blog introduction

2. Content

3. Push

4. Conclusion


1. Blog introduction

There is a need to read the picture, then rotate it and save it as a new picture. I tried it and encountered a lot of problems. Record it here


2. Content

# 读取图片
src_img = Image.open(testPng)
src_img = src_img.rotate(90)
src_img.save(out2)

 We first read the picture, and then save the picture after rotating it by 90 degrees. The left picture in the above picture is the original picture, and the right picture is the saved picture. We found a problem. The size of the rotated picture is not based on the rotated size. And change, this is obviously not the result we want, improvement

 

# 读取图片
src_img = Image.open(testPng)
src_img = src_img.rotate(90)
src_img = src_img.resize((src_img.height, src_img.width))
src_img.save(out2)

 

 

 I changed my thinking here. We reset the image size after rotating it, which is very uncomfortable. We found that setting the size will affect the image content and be compressed, which is not the effect we want.

 

# 读取图片
src_img = Image.open(testPng)
src_img = src_img.rotate(90, expand = 1)
src_img.save(out2)

 Finally we have reached our ideal effect, which is very annoying. We found that the rotate method has a second parameter [expand]. If this parameter is set to true, after the picture is rotated, the size of the picture will be reset according to the situation after the rotation, which is very simple The problem has spared a whole circle.


3. Push

Github:https://github.com/KingSun5


4. Conclusion

If you feel that the blogger’s article is well written, you may wish to pay attention to the blogger and like the blog post. In addition, the ability of the blogger is limited. If there is any error in the article, you are welcome to comment and criticize.

       QQ exchange group: 806091680 (Chinar)

       This group was created by CSDN blogger Chinar, recommend it! I am also in the group!

       This article is an original article, please reprint the source of the famous author and stick to the top! ! ! !

 

Guess you like

Origin blog.csdn.net/Mr_Sun88/article/details/115051983