بکندباز

متد Image.crop()‎ در کتابخانه PIL در پایتون

PIL کتابخانه تصویربرداری پایتون است که قابلیت ویرایش تصویر را در اختیار مفسر پایتون قرار می دهد. متد PIL.Image.crop() برای برش یک قسمت مستطیلی از هر تصویر استفاده می شود.

سینتکس:

PIL.Image.crop(box = None)

پارامترها: 
box – یک تاپل 4 تایی که مختصات پیکسل چپ، بالا، راست و پایین را مشخص می کند.

نوع داده بازگشتی: Image
مقدار بازگشتی: یک شیء  تصویری .

 

کد شماره 1:

# Importing Image class from PIL module
from PIL import Image

# Opens a image in RGB mode
im = Image.open(r"C:\Users\Admin\Pictures\geeks.png")

# Size of the image in pixels (size of original image)
# (This is not mandatory)
width, height = im.size

# Setting the points for cropped image
left = 5
top = height / 4
right = 164
bottom = 3 * height / 4

# Cropped image of above dimension
# (It will not change original image)
im1 = im.crop((left, top, right, bottom))

# Shows the image in image viewer
im1.show()

تصویر اصلی

خروجی:

کد شماره 2:

# Importing Image class from PIL module
from PIL import Image

# Opens a image in RGB mode
im = Image.open(r"C:\Users\Admin\Pictures\network.png")

# Setting the points for cropped image
left = 155
top = 65
right = 360
bottom = 270

# Cropped image of above dimension
# (It will not change original image)
im1 = im.crop((left, top, right, bottom))

# Shows the image in image viewer
im1.show()

 

تصویر اصلی:

خروجی:

 

backendbaz

مدیر وب سایت بکندباز

دیدگاه‌ها

*
*