Home Backend Development Python Tutorial python处理图片之PIL模块简单使用方法

python处理图片之PIL模块简单使用方法

Jun 10, 2016 pm 03:12 PM
python Process pictures

本文实例讲述了python处理图片之PIL模块简单使用方法。分享给大家供大家参考。具体实现方法如下:

#!/usr/bin/env python  
#encoding: utf-8 
import Image  
class myimg: 
  def __init__(self, open_file, save_file): 
    self.img = Image.open(open_file) 
    self.save_file = save_file 
  def Change_Size(self, percent=100, height=None, width=None): 
    ''''' 
    percent:以百分比来决定大小 
    height, width:以指定的高、宽来决定大小 
    ''' 
    if not (height and width): 
      width,height = self.img.size   
    new_img = self.img.resize((width*percent/100,height*percent/100),Image.BILINEAR) 
    new_img.save(self.save_file) 
  def Rotation(self, angle): 
    ''''' 
    angle: 旋转的度数 
    ''' 
    rot_img = self.img.rotate(angle) #旋转 
    rot_img.save(self.save_file) 
  def Save_as(self, filename): 
    ''''' 
    filename: 另存为图片格式,直接根据后缀名来 
    ''' 
    self.img.save(filename)  
  def Draw_Something(self): 
    ''''' 
        利用ImageDraw来画图形 
    ''' 
    import ImageDraw 
    draw = ImageDraw.Draw(self.img) 
    width,height = self.img.size 
    draw.line(((0,0),(width-1,height-1)),fill=255) #画直线 
    draw.line(((0,height-1),(width-1,0)),fill=255) 
    draw.arc((0,0,width-1,height-1),0,360,fill=255) #画椭圆 
    self.img.save(self.save_file) 
  def Enhance_Something(self): 
    ''''' 
        利用 ImageEnhance来增强图片效果 
    ''' 
    import ImageEnhance 
    brightness = ImageEnhance.Brightness(self.img) 
    bright_img = brightness.enhance(2.0) ##亮度增强 
    bright_img.save(self.save_file) 
    sharpness = ImageEnhance.Sharpness(self.img) 
    sharp_img = sharpness.enhance(7.0) #锐度增强 
    sharp_img.save(self.save_file) 
    contrast = ImageEnhance.Contrast(self.img) #对比度增强 
    contrast_img = contrast.enhance(2.0)  
    contrast_img.save(self.save_file) 
if __name__ == "__main__": 
  file_name = r"D:\test.png" 
  save_file = r"D:\save.png" 
  saveas_file = r"D:\save_as.bmp" 
  oimg = myimg(file_name, save_file) 
  oimg.Change_Size(30) 
  oimg.Rotation(45) 
  oimg.Save_as(saveas_file) 
  oimg.Draw_Something() 
  oimg.Enhance_Something()
Copy after login

原图:

处理过的画图:(锐化过的)

PS:此外还有另一个比较常用的模块,image模块。

希望本文所述对大家的Python程序设计有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to download deepseek Xiaomi How to download deepseek Xiaomi Feb 19, 2025 pm 05:27 PM

How to download deepseek Xiaomi

What are the advantages and disadvantages of templating? What are the advantages and disadvantages of templating? May 08, 2024 pm 03:51 PM

What are the advantages and disadvantages of templating?

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Jul 01, 2024 am 07:22 AM

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step May 06, 2024 pm 03:52 PM

For only $250, Hugging Face's technical director teaches you how to fine-tune Llama 3 step by step

Share several .NET open source AI and LLM related project frameworks Share several .NET open source AI and LLM related project frameworks May 06, 2024 pm 04:43 PM

Share several .NET open source AI and LLM related project frameworks

A complete guide to golang function debugging and analysis A complete guide to golang function debugging and analysis May 06, 2024 pm 02:00 PM

A complete guide to golang function debugging and analysis

How do you ask him deepseek How do you ask him deepseek Feb 19, 2025 pm 04:42 PM

How do you ask him deepseek

How to save the evaluate function How to save the evaluate function May 07, 2024 am 01:09 AM

How to save the evaluate function

See all articles