In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to achieve python image proportional scaling, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Description
1. Initialize the screen of the target size, all values are the same.
2. The longer edges in the original image are scaled down to the target size, and the scaling ratio is calculated.
3, so that the short edge can also be scaled down in this proportion, the resulting picture will not be deformed.
4. The scaled image must be less than or equal to the target size.
Therefore, it must be able to paste in the center of the curtain, so that the uncovered areas of the curtain will automatically become blank, eliminating the filling step.
5. get the picture you want.
Example
Import numpy as npfrom PIL import Image def resize (img, size): # first create a target-sized screen, and then paste the scaled image to the center, thus saving the trouble of filling and leaving white space on both sides. Canvas = Image.new ("RGB", size=size, color= "# 7777") target_width, target_height = size width, height = img.size offset_x = 0 offset_y = 0 if height > width: # height is the long side height_ = target_height # directly adjust the height to the target size scale = height_ / height # calculate how much the height has been adjusted Get a scale width_ = int (width * scale) # wide and scale offset_x = (target_width-width_) / / 2 # to calculate the distance of white space on one side in the x direction else: # ditto width_ = target_width scale = width_ / width height_ = int (height * scale) offset_y = ( Target_height-height_) / / 2 img = img.resize ((width_ Height_), Image.BILINEAR) # canvas.paste (img, box= (offset_x, offset_y)) # paste the scaled image onto the screen # the box parameter is used to determine the position of the upper-left corner of the picture to be pasted. Offset_x is left blank on one side of the x axis, and offset_y on the one side of the y axis, which ensures that the picture can be filled in the center of the screen return canvas img= Image.open ('1.jpg') target__size= (500300) # Target size: width is 500W, height is 300res = resize (img,target__size) res.save (' new.jpg') above is all the content of the article "how does python achieve Image proportional scaling" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.