In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to cut Python PIL pictures proportionally". In daily operation, I believe many people have doubts about how to cut Python PIL pictures proportionally. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to cut Python PIL pictures proportionally". Next, please follow the editor to study!
Description of the problem of how to cut PIL pictures proportionally
If the ratio of the picture is 1:1, cut to 4:3.
1.jpg
Solution from PIL import Imagedef image_clip (filename, savename, width_scale, height_scale): "" Image cropping: param filename: original image path: param savename: save picture path: param width_scale: width ratio: param height_scale: height ratio "" image = Image.open (filename) (width, height), (_ width, _ height) = image.size Image.size _ height = width / width_scale * height_scale if _ height > height: _ height = height_ width = width_scale * height / height_scale image.crop ((0,0, _ width, _ height)) .save (savename) # upper left corner # image.crop ((0, height-_ height, _ width, height). Save (savename) # lower left corner # image.crop (width-_ width, 0, width _ height)) .save (savename) # upper right corner # image.crop ((width-_ width, height-_ height, width, height)) .save (savename) # lower right corner if _ _ name__ ='_ main__': filename = '1.jpg' savename =' result.jpg' image_clip (filename, savename, width_scale=4, height_scale=3) # image_clip (filename, savename, width_scale=3, height_scale=4)
Effect.
PIL resizes the picture
Use PIL to change the image size with the same picture scale.
Introduction
Image.resize
Def resize (self, size, resample=BICUBIC, box=None, reducing_gap=None): "Returns a resized copy of this image. Returns a resized copy of this image. Param size: The requested size in pixels, as a 2-tuple: (width, height). Param size: the size of the request in pixels, which is a binary array: (width, height): param resample: An optional resampling filter. This can be one of: py:attr: `PIL.Image.NEAREST`,: py:attr: `PIL.Image.BOX`,: py:attr: `PIL.Image.BILINEAR`,: py:attr: `PIL.Image.HAMMING`,: py:attr: `PIL.Image.BICUBIC` or: py:attr: `PIL.Image.LANCZOS`. Default filter is: py:attr: `PIL.Image.BICUBIC`. If the image has mode "1" or "P", it is always set to: py:attr: `PIL.Image.NEAREST`. See:: ref: `concept-filters. Param resample: an optional resampling filter. : param box: An optional 4-tuple of floats providing the source image region to be scaled. The values must be within (0,0, width, height) rectangle. If omitted or None, the entire source is used. Param box: an optional 4-ary floating point number that provides the area of the source image to be scaled. : param reducing_gap: Apply optimization by resizing the image in two steps. First, reducing the image by integer times using: py:meth: `~ PIL.Image.Image.reduce`. Second, resizing using regular resampling. The last step changes size no less than by ``reducing_ gap`` times. ``reducing_ gap`` may be None (no first step is performed) or should be greater than 1.0. The bigger ``reducing_ gap``, the closer the result to the fair resampling. The smaller ``reducing_ gap``, the faster resizing. With ``reducing_ gap`` greater or equal to 3.0, the result is indistinguishable from fair resampling in most cases. The default value is None (no optimization). Param reducing_gap: apply optimization by resizing the image in two steps. : returns: An: py:class: `~ PIL.Image.Image` object. Returns: return a PIL.Image.Image object "" look at the code from PIL import Image image = Image.open ('picture path') # resize the picture And keep the scale unchanged # given a basic width base_width = 50 # the ratio of the basic width to the original width w_percent = base_width / float (image.size [0]) # the length of the new image h_size = int (float (image.size [1]) * float (w_percent)) # resize # by default, PIL uses the Image.NEAREST filter to resize As a result, good performance is obtained, but the quality is very poor. Image = image.resize ((base_width, h_size), Image.ANTIALIAS) at this point, the study on "how to cut Python PIL images proportionally" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.