前言:

GAN的原理很简单,但是它有很多变体,如:DCGAN、CycleGAN、DeblurGAN等,它们也被用在不同地方,本文将用到DCGAN来生成头像图片,可以做到以假乱真的地步。

1.首先调用程序对图片进行标准化

代码如下:

from skimage import io,transform,color
import numpy as np

def convert_gray(f,**args):
"""
将彩色图片转换为灰度图片和调整大小,改变图像分辨率
:return:
"""
rgb = io.imread(f)
# gray = color.rgb2gray(rgb) #
dst = transform.resize(rgb, (96, 96)) # 由于在后期测试的时候用的图片尺寸是96x96 return dst

datapath='your train path'
str=datapath+'/*.jpg' #识别.jpg的图像
coll = io.ImageCollection(str,load_func=convert_gray)#批处理
for i in range(len(coll)):
io.imsave(r'your save paths'+np.str(i)+'.jpg',coll[i])

2.调用程序

  • 训练图像 代码只能引用DCGAN的github代码:carpedm20/DCGAN-tensorflow

  • 调用命令:

    python main.py --input_height 96 --input_width 96 --output_height 48 --output_width 48 --dataset faces --crop --train --epoch 300 --input_fname_pattern "*.jpg"

3. 切割图片

由于生成的图片是由64张小尺寸图片拼接成一整张的,故需要进行对图片切割。

切割代码如下:

# encoding:utf-8
from PIL import Image
import sys
import math
import os

def fill_image(image):
"""
将图片填充为正方形
:param image:
:return:
"""
width, height = image.size
#选取长和宽中较大值作为新图片的
new_image_length = width if width > height else height
#生成新图片[白底]
new_image = Image.new(image.mode, (new_image_length, new_image_length), color='white')
#将之前的图粘贴在新图上,居中
if width > height:#原图宽大于高,则填充图片的竖直维度
#(x,y)二元组表示粘贴上图相对下图的起始位置
new_image.paste(image, (0, int((new_image_length - height) / 2)))
else:
new_image.paste(image,(int((new_image_length - width) / 2),0))

return new_image


def cut_image(image,cut_num):
"""
切图
:param image:
:return:
"""
flag_value = int(math.sqrt(cut_num))
width, height = image.size
item_width = int(width / flag_value)
box_list = []
for i in range(0,flag_value):
for j in range(0,flag_value):
box = (j*item_width,i*item_width,(j+1)*item_width,(i+1)*item_width)
box_list.append(box)
image_list = [image.crop(box) for box in box_list]

return image_list


def save_images(image_list):
"""
保存
:param image_list:
:return:
"""
index = 1
dirs = './img_add/'
if not os.path.exists(dirs):
os.makedirs(dirs)
for image in image_list:
image.save(dirs+str(index) + '.png', 'PNG')
index += 1

def main(file_path,batch_size):

image = Image.open(file_path)
image = fill_image(image)
image_list = cut_image(image,batch_size)
save_images(image_list)

if __name__ == '__main__':
batch_size = 64
file_path = "train.png" # 图片路径
main(file_path,batch_size)

参考链接:https://blog.csdn.net/qq_34739497/article/details/79902356

DCGAN生成目标训练图片的更多相关文章

  1. Tensorflow:DCGAN生成手写数字

    参考地址:https://blog.csdn.net/miracle_ma/article/details/78305991 使用DCGAN(deep convolutional GAN):深度卷积G ...

  2. 1.预处理,生成预编译文件(.文件): Gcc –E hello.c –o hello.i 2.编译,生成汇编代码(.s文件): Gcc –S hello.i –o hello.s 3.汇编,生成目标文件(.o文件): Gcc –c hello.s –o hello.o 4.链接,生成可执行文件: linux笔记

    1 动态查看日志 tail -f filename tail -1000f filename 2 解压当前目录内容为xxx.zip  zip -r xxx.zip ./* 3 查看内存使用情况 fre ...

  3. 网页小实验——用canvas生成精灵动画图片

    实验目标:借助canvas把一张国际象棋棋子图片转换为一组适用于WebGL渲染的精灵动画图片,不借助其他图片处理工具,不引用其他库只使用原生js实现. 初始图片如下: 一.图片分割 将初始图片分割为六 ...

  4. 【代码笔记】iOS-通过颜色来生成一个纯色图片

    一,效果图. 二,代码. RootViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional se ...

  5. 用ticons指令结合ImageMagickDisplay工具批量生成Android适应图片

    用ticons指令结合ImageMagickDisplay工具批量生成Android适应图片 ticons的用法可以百度 这里记录下具体的编译方法 在安装了ticons和ImageMagickDisp ...

  6. PHP 简易读取文件目录下的文件,生成css spirte图片

    因为个人不是对PS熟悉,不清楚如何在PS中生成一张横向有序的spirte图片,使用了"css sprite V4.3"版本,生成的图片会出现压缩图片大小的情况,本想修改原作者开发的 ...

  7. C#上传图片和生成缩略图以及图片预览

    因工作需要,上传图片要增加MIME类型验证和生成较小尺寸的图片用于浏览.根据网上代码加以修改做出如下效果图: 前台代码如下: <html xmlns="http://www.w3.or ...

  8. iOS 通过颜色来生成一个纯色图片

    //通过颜色来生成一个纯色图片- (UIImage *)buttonImageFromColor:(UIColor *)color{        CGRect rect = CGRectMake(0 ...

  9. PHP使用JPG生成GIF动画图片,基于php_imagick_st-Q8.dll

    PHP使用php_imagick_st-Q8.dll类库,把JPG图片连接生成GIF动画图片,需要事先下载好php_imagick_st-Q8.dll,文件,并配置php.ini文件,启用php_im ...

随机推荐

  1. Caffe2 玩玩回归(Toy Regression)[5]

    前言 这一节将讲述如何使用Caffe2的特征进行简单的线性回归学习.主要分为以下几步: - 生成随机数据作为模型的输入 - 用这些数据创建网络 - 自动训练模型 - 查看梯度递减的结果和学习过程中网络 ...

  2. JavaWeb开发记录全过程--(1)环境配置

    一. 开发工具:idea 理由:根据idea 如何连接服务器,可以直接在idea上连接服务器 安装:根据IntelliJ IDEA 下载安装(含注册码),进行非常规手段使用idea 二.分析问题: # ...

  3. 列举出给定数量的字符的所有组成情况-java实现

    老早以前刚学程序的时候碰到了这么个问题,当时没想出来,今天突然想起来了这么个问题于是写了下,也算留个纪念吧 public static String itr; public static void m ...

  4. Spring Schedule 实现定时任务

    很多时候我们都需要为系统建立一个定时任务来帮我们做一些事情,SpringBoot 已经帮我们实现好了一个,我们只需要直接使用即可,当然你也可以不用 SpringBoot 自带的定时任务,整合 Quar ...

  5. scrapy 中没有 crawl 命令

    确保两点: 1.把爬虫.py 复制到 spider 文件夹里 如 执行 scrapy crawl demo.py (spiders 中就要有 demo.py 文件) 2.在项目文件夹内执行命令 在 s ...

  6. B. Light bulbs

    B. Light bulbs There are NNN light bulbs indexed from 000 to N−1N-1N−1. Initially, all of them are o ...

  7. ubuntu 12.04 配置vsftpd 服务,添加虚拟用户,ssl加密

    1.对于12.04的vsftpd 有一些bug,推荐安装版本vsftpd_2.3.5-1ubuntu2ppa1_amd64.debapt-get install python-software-pro ...

  8. Laradock 下安装Beast扩展

    laradock/php-fpm/Dockfile ########################################################################## ...

  9. Vue 集成easyUI

    原 Vue 集成easyUI https://blog.csdn.net/m0_37948170/article/details/84960320   参考vue官网用cli创建了Vue项目之后: n ...

  10. 十三、web应用中路径总结

    1.路径的编写形式:1)<a href="/项目名/资源路径"></a>    <!--超链接--> <form action=" ...