पायथन इमेज प्रोसेसिंग के लिए कई लाइब्रेरी प्रदान करता है जिसमें पिलो, पायथन इमेजिंग लाइब्रेरी, स्किकिट-इमेज या ओपनसीवी शामिल हैं।
हम यहां इमेज प्रोसेसिंग के लिए पिलो लाइब्रेरी का उपयोग करने जा रहे हैं क्योंकि यह इमेज मैनिपुलेशन के लिए कई मानक प्रक्रियाएं प्रदान करता है और छवि फ़ाइल स्वरूपों की श्रेणी का समर्थन करता है जैसे कि jpeg, png, gif, tiff, bmp और अन्य।
पिलो लाइब्रेरी पायथन इमेजिंग लाइब्रेरी (पीआईएल) के शीर्ष पर बनाई गई है और इसकी मूल लाइब्रेरी (पीआईएल) की तुलना में अधिक सुविधाएं प्रदान करती है।
इंस्टॉलेशन
हम पिप का उपयोग करके तकिया स्थापित कर सकते हैं, इसलिए बस कमांड टर्मिनल में निम्नलिखित टाइप करें -
$ pip install pillow
तकिए पर बुनियादी संचालन
आइए पिलो लाइब्रेरी का उपयोग करके छवियों पर कुछ बुनियादी संचालन करें।
from PIL import Image image = Image.open(r"C:\Users\rajesh\Desktop\imagefolder\beach-parga.jpg") image.show() # The file format of the source file. # Output: JPEG print(image.format) # The pixel format used by the image. Typical values are “1”, “L”, “RGB”, or “CMYK.” # Output: RGB print(image.mode) # Image size, in pixels. The size is given as a 2-tuple (width, height). # Output: (2048, 1365) print(image.size) # Colour palette table, if any. #Output: None print(image.palette)
आउटपुट

JPEG RGB (2048, 1365) None
आकार के आधार पर छवियों को फ़िल्टर करें
कार्यक्रम के नीचे एक विशेष पथ से सभी छवियों के आकार को कम कर देगा (डिफ़ॉल्ट पथ:वर्तमान कार्यशील निर्देशिका)। हम नीचे दिए गए कार्यक्रम में छवि की अधिकतम_ऊंचाई, अधिकतम_चौड़ाई या विस्तार बदल सकते हैं:
कोड
import os
from PIL import Image
max_height = 900
max_width = 900
extensions = ['JPG']
path = os.path.abspath(".")
def adjusted_size(width,height):
if width > max_width or height>max_height:
if width > height:
return max_width, int (max_width * height/ width)
else:
return int (max_height*width/height), max_height
else:
return width,height
if __name__ == "__main__":
for img in os.listdir(path):
if os.path.isfile(os.path.join(path,img)):
img_text, img_ext= os.path.splitext(img)
img_ext= img_ext[1:].upper()
if img_ext in extensions:
print (img)
image = Image.open(os.path.join(path,img))
width, height= image.size
image = image.resize(adjusted_size(width, height))
image.save(os.path.join(path,img)) आउटपुट
another_Bike.jpg clock.JPG myBike.jpg Top-bike-wallpaper.jpg
स्क्रिप्ट के ऊपर चलने पर, वर्तमान कार्यशील निर्देशिका (जो वर्तमान में पाइटन स्क्रिप्ट फ़ोल्डर है) में मौजूद छवियों का आकार अधिकतम 900 (चौड़ाई/ऊंचाई) होगा।