利用Python批量生成任意尺寸的图片

yipeiwu_com6年前Python基础

实现效果

通过源图片,在当前工作目录的/img目录下生成1000张,分别从1*1到1000*1000像素的图片。

效果如下:


目录结构

实现示例

# -*- coding: utf-8 -*-
import threading

from PIL import Image

image_size = range(1, 1001)


def start():
  for size in image_size:
    t = threading.Thread(target=create_image, args=(size,))
    t.start()


def create_image(size):
  pri_image = Image.open("origin.png")
  pri_image.resize((size, size), Image.ANTIALIAS).save("img/png_%d.png" % size)


if __name__ == "__main__":
  start()

注意:该项目需要引用PIL库。

在这里,我们使用resize函数。

与大多数脚本库一样,resize函数也支持链式调用。先通过resize((size, size), Image.ANTIALIAS)指定大小与质量,其中对于参数二:

参数值  含义
Image.NEAREST 低质量
Image.BILINEAR 双线性
Image.BICUBIC  三次样条插值
Image.ANTIALIAS 高质量

最终调用save("img/png_%d.png" % size)方法,以指定格式写入指定位置。

另外,考虑到是大量的线性密集型运算,因此使用了多线程并发。

结束语

以上就是利用Python批量生成任意尺寸图片的全部内容了,希望对大家学习和使用Python能有所帮助。

相关文章

python3的print()函数的用法图文讲解

python3的print()函数的用法图文讲解

Python 3 print 函数 基础代码 1、print语法格式 print()函数具有丰富的功能,详细语法格式如下: print(value, ..., sep=' ', end=...

python执行使用shell命令方法分享

1. os.system(shell_command) 直接在终端输出执行结果,返回执行状态0,1 此函数会启动子进程,在子进程中执行command,并返回command命令执行完毕后的...

numpy.random.shuffle打乱顺序函数的实现

numpy.random.shuffle 在做将caffe模型和预训练的参数转化为tensorflow的模型和预训练的参数,以便微调,遇到如下函数: def gen_data(so...

python实现txt文件格式转换为arff格式

本文实例为大家分享了python实现txt文件格式转换为arff格式的具体代码,供大家参考,具体内容如下 将文件读取出来的时候默认都是字符型的,所以有转换出来有点问题,但是还是可以用的。...

浅析python 中大括号中括号小括号的区分

python语言最常见的括号有三种,分别是:小括号( )、中括号[ ]和大括号也叫做花括号{ }。其作用也各不相同,分别用来代表不同的python基本内置数据类型。 1.python中的...