Pillow(PIL)入门教程(非常详细)以及python实现jpg,png、ico、bmp格式互转大全 您所在的位置:网站首页 1211413705_16347938900451n.png Pillow(PIL)入门教程(非常详细)以及python实现jpg,png、ico、bmp格式互转大全

Pillow(PIL)入门教程(非常详细)以及python实现jpg,png、ico、bmp格式互转大全

2023-12-02 04:21| 来源: 网络整理| 查看: 265

目录

概述

Pillow库的特点:

python3安装pillow:

​编辑​Pillow是什么

Pillow创建Image对象:

jpg,png:

jpg与png格式互转代码:

代码实现:

其他格式转换成ico格式:

其他格式转换成ico格式代码:

其他格式转成svg格式:

其他格式转成svg格式代码:

AI.svg:

概述

Pillow 库(有时也称 PIL 库) 是 Python 图像处理的基础库,它是一个免费开源的第三方库,由一群 Python 社区志愿者使用 Python 语言开发而成(主要贡献者:Alex Clark)。

Pillow 提供了非常强大的图像处理功能,它能够很轻松地完成一些图像处理任务。与 Python 的其他图像处理库相比(OpenCV、Scikit-image 等),Pillow 库简单易用,非常适合初学者学习。

Pillow 库提供了非常丰富的功能,主要有以下几点:

Pillow 库能够很轻松的读取和保存各种格式的图片;Pillow 库提供了简洁易用的 API 接口,可以让您轻松地完成许多图像处理任务;Pillow 库能够配合 GUI(图形用户界面) 软件包 Tkinter 一起使用;Pillow 库中的 Image 对象能够与 NumPy ndarray 数组实现相互转换。

丰富功能的实现得益于 Pillow 提供了众多的模块。在 Pillow 库中有二十多个模块,比如 Image 图像处理模块、ImageFont 添加文本模块、ImageColor 颜色处理模块、ImageDraw 绘图模块等等,每个模块各自实现了不同的功能,同时模块之间又可以互相配合。

Pillow库的特点:

Pillow 库作为图像处理的常用库,主要有以下三大特点:

1) 支持广泛的文件格式

Pillow 支持广泛的图像格式如下:

图像文件格式 — Pillow (PIL Fork) 7.0.0 文档https://www.osgeo.cn/pillow/_build/html/handbook/image-file-formats.html同时,它也支持图像格式之间的相互转换。总之, Pillow 几乎能够处理任何格式的图像。

2) 提供了丰富的功能

Pillow 提供了丰富的图像处理功能,可概括为两个方面:

图像归档图像处理

图像归档,包括创建缩略图、生成预览图像、图像批量处理等;而图像处理,则包括调整图像大小、裁剪图像、像素点处理、添加滤镜、图像颜色处理等。

3) 配合GUI工具使用

Pillow 库可以配合 Python GUI(图形用户界面)工具 Tkinter 一起使用。

除上述特点之外,Pillow 库还能实现一些较为复杂的图像处理操作,比如给图像添加水印、合成 GIF 动态效果图等等。

PIL(Python Imaging Library)是Python一个强大方便的图像处理库,只支持到Python2.7。 Pillow是PIL的一个派生分支,在Python3中用Pillow代替PIL。

Tutorial - Pillow (PIL Fork) 10.0.0.dev0 documentationUsing the Image class: The most important class in the Python Imaging Library is the Image class, defined in the module with the same name. You can create instances of this class in several ways; e...https://pillow.readthedocs.io/en/latest/handbook/tutorial.html

python3安装pillow: pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pillow ​Pillow是什么

PIL( Python Imaging Library)是 Python 的第三方图像处理库,由于其功能丰富,API 简洁易用,因此深受好评。

自 2011 年以来,由于 PIL 库更新缓慢,目前仅支持 Python 2.7 版本,这明显无法满足 Python3 版本的使用需求。于是一群 Python 社区的志愿者(主要贡献者:Alex Clark 和 Contributors)在 PIL 库的基础上开发了一个支持 Python3 版本的图像处理库,它就是 Pillow。

Pillow 不仅是 PIL 库的“复制版”,而且它又在 PIL 库的基础上增加了许多新的特性。Pillow 发展至今,已经成为了比 PIL 更具活力的图像处理库。

Pillow 的初衷只是想作为 PIL 库的分支和补充,如今它已是“青出于蓝而胜于蓝”。

除了 PIL 和 Pillow 库之外,Python 还提供了一些其他图像处理库:

Scikit-image:一款基于 scipy 科学计算的图像处理软件包,以数组的形式对图像进行处理;OpenCV:其实是一个 C++ 图像处理库,不过它提供了 Python 语言的接口。

Pillow 是 Python 中较为基础的图像处理库,主要用于图像的基本处理,比如裁剪图像、调整图像大小和图像颜色处理等。与 Pillow 相比,OpenCV 和 Scikit-image 的功能更为丰富,所以使用起来也更为复杂,主要应用于机器视觉、图像分析等领域,比如众所周知的“人脸识别”应用。

Pillow创建Image对象: from PIL import Image def read_images(): jpg_image = Image.open("美女.jpg") # jpg_image.show() print("image的size=>>>", jpg_image.size) print("image的width,height", jpg_image.width, jpg_image.height) print("image的format=>>>", jpg_image.format) print("image的格式详情=>>>", jpg_image.format_description) print("image的模式=>>>", jpg_image.mode) jpg_image.show() if __name__ == '__main__': read_images() jpg,png:

PNG和JPG都是常见的图像文件格式,它们之间的主要区别如下:

压缩方式:JPG使用有损压缩,而PNG使用无损压缩。JPG可以压缩图像文件的大小,但会降低图像的质量,尤其是在高度压缩的情况下。PNG压缩图像文件时不会丢失任何信息,因此图像质量不会受到损失。

色彩深度:JPG支持24位真彩色和灰度色,而PNG支持24位真彩色、32位真彩色和灰度色。因此,PNG可以处理更高级别的色彩深度,使其更适合处理复杂的图像。

透明度:PNG支持透明度通道,这意味着可以将图像的某些部分设置为透明,而JPG不支持透明度通道。

文件大小:JPG文件大小通常比PNG文件小,因为它使用有损压缩。但是,在某些情况下,PNG文件大小可能比JPG文件更小,例如,PNG对于具有相同颜色的像素可以进行更好的压缩。

综上所述,如果需要处理高级别的色彩深度和透明度通道,则PNG是更好的选择。如果需要处理文件大小较小的图像,则JPG可能更适合。

jpg与png格式互转代码: 代码实现: from PIL import Image import sys def read_image(image_path): """ 读取任意格式图片 :param image_path: 任意格式图片路径 :return: """ jpg_image = Image.open(image_path) # jpg_image.show() print("image的size=>>>", jpg_image.size) print("image的width,height", jpg_image.width, jpg_image.height) print("image的format=>>>", jpg_image.format) print("image的格式详情=>>>", jpg_image.format_description) print("image的模式=>>>", jpg_image.mode) jpg_image.show() def transform_jpg_2_png(): jpg_image = Image.open("美女.jpg", mode="r") jpg_image.save("./美女_with_png.png", format="png") def transform_png_2_jpg(): png_image = Image.open("./美女_with_png.png", mode="r") png_image.save("./美女_with_jpg.jpg", format="jpeg") def read_png_image(): read_image("./美女_with_png.png") def read_jpg_image(): read_image("./美女_with_jpg.jpg") if __name__ == '__main__': # read_images() transform_jpg_2_png() read_png_image() transform_png_2_jpg() read_jpg_image() sys.exit(0)

运行结果:

其他格式转换成ico格式:

icon是一种图标格式,用于系统图标、软件图标等,这种图标扩展名为*.icon、*.ico。常见的软件或windows桌面上的那些图标一般都是ICON格式的。icon元素包括两个可选的子元素:small-icon子元素和large-icon子元素。文件名是Web应用归档文件(WAR)的根的相对路径。部署描述符并没有使用icon元素。但是,如果使用XML工具编辑部署描述符,XML编辑器可以使用icon元素。

其他格式转换成ico格式代码:

人工智能.png:

# ---encoding:utf-8--- # @Time : 2023/6/13 07:41 # @Author : CBAiotAigc # @Email :[email protected] # @Site : # @File : python实现icon-其他格式互转.py # @Project : PythonUtils # @Software: PyCharm from PIL import Image, ImageOps def png_2_icon(): png_image = Image.open("人工智能.png") # 将输出的ico转成大小维256*256 png_image.save("./人工智能.ico", sizes=[(256, 256)]) if __name__ == '__main__': png_2_icon()

 

 

其他格式转成svg格式: 其他格式转成svg格式代码: # ---encoding:utf-8--- # @Time : 2023/6/13 08:25 # @Author : CBAiotAigc # @Email :[email protected] # @Site : # @File : python实现svg-其他格式互转.py # @Project : PythonUtils # @Software: PyCharm import sys import os import operator from collections import deque import io from optparse import OptionParser from PIL import Image def add_tuple(a, b): return tuple(map(operator.add, a, b)) def sub_tuple(a, b): return tuple(map(operator.sub, a, b)) def neg_tuple(a): return tuple(map(operator.neg, a)) def direction(edge): return sub_tuple(edge[1], edge[0]) def magnitude(a): return int(pow(pow(a[0], 2) + pow(a[1], 2), .5)) def normalize(a): mag = magnitude(a) assert mag > 0, "Cannot normalize a zero-length vector" return tuple(map(operator.truediv, a, [mag] * len(a))) def svg_header(width, height): return """ """ % (width, height) def rgba_image_to_svg_pixels(im): s = io.StringIO() s.write(svg_header(*im.size)) width, height = im.size for x in range(width): for y in range(height): here = (x, y) rgba = im.getpixel(here) if not rgba[3]: continue s.write( """ \n""" % ( x, y, rgba[0:3], float(rgba[3]) / 255)) print("Converting pixels: " + str(x * 100 / width) + "%") s.write("""\n""") return s.getvalue() def joined_edges(assorted_edges, keep_every_point=False): pieces = [] piece = [] directions = deque([ (0, 1), (1, 0), (0, -1), (-1, 0), ]) while assorted_edges: if not piece: piece.append(assorted_edges.pop()) current_direction = normalize(direction(piece[-1])) while current_direction != directions[2]: directions.rotate() for i in range(1, 4): next_end = add_tuple(piece[-1][1], directions[i]) next_edge = (piece[-1][1], next_end) if next_edge in assorted_edges: assorted_edges.remove(next_edge) if i == 2 and not keep_every_point: # same direction piece[-1] = (piece[-1][0], next_edge[1]) else: piece.append(next_edge) if piece[0][0] == piece[-1][1]: if not keep_every_point and normalize(direction(piece[0])) == normalize(direction(piece[-1])): piece[-1] = (piece[-1][0], piece.pop(0)[1]) # same direction pieces.append(piece) piece = [] break else: raise Exception("Failed to find connecting edge") return pieces def rgba_image_to_svg_contiguous(im, keep_every_point=False): # collect contiguous pixel groups adjacent = ((1, 0), (0, 1), (-1, 0), (0, -1)) visited = Image.new("1", im.size, 0) color_pixel_lists = {} width, height = im.size for x in range(width): for y in range(height): here = (x, y) if visited.getpixel(here): continue rgba = im.getpixel((x, y)) if not rgba[3]: continue piece = [] queue = [here] visited.putpixel(here, 1) while queue: here = queue.pop() for offset in adjacent: neighbour = add_tuple(here, offset) if not (0


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有