Python实现多张图片合成文字的效果
作者:Yunlord 发布时间:2021-02-16 17:12:12
标签:Python,图片,合成,文字
前言
前段时间看到有人问如何使用Python实现多张图片组成文字的效果?觉得还挺有意思,于是尝试做了一下,刚好赶上端午节,所以打算从网上下载1000张王心凌的照片,组成端午安康的字样,给大家送上祝福。
一、图片批量下载
首先我们需要从百度下载大量王心凌的图片,但是如果会去百度图片里一张张右键下载,但这样未免太麻烦了,所以打算直接用python写一个批量下载图片的代码,输入想要下载图片的关键字,然后输入想要下载图片的数量,就可以成功下载图片了!
代码主要分成三个部分:
下载图片
检测图片数量
查找相似图片
1.下载图片
def dowmloadPicture(html, keyword):
global num
# t =0
pic_url = re.findall('"objURL":"(.*?)",', html, re.S) # 先利用正则表达式找到图片url
print('找到关键词:' + keyword + '的图片,即将开始下载图片...')
for each in pic_url:
print('正在下载第' + str(num + 1) + '张图片,图片地址:' + str(each))
try:
if each is not None:
pic = requests.get(each, timeout=7)
else:
continue
except BaseException:
print('错误,当前图片无法下载')
continue
else:
string = file + r'\\' + keyword + '_' + str(num) + '.jpg'
fp = open(string, 'wb')
fp.write(pic.content)
fp.close()
num += 1
if num >= numPicture:
return
2.检测图片数量
def Find(url, A):
global List
print('正在检测图片总数,请稍等.....')
t = 0
i = 1
s = 0
while t < 1000:
Url = url + str(t)
try:
# 这里搞了下
Result = A.get(Url, timeout=7, allow_redirects=False)
except BaseException:
t = t + 60
continue
else:
result = Result.text
pic_url = re.findall('"objURL":"(.*?)",', result, re.S) # 先利用正则表达式找到图片url
s += len(pic_url)
if len(pic_url) == 0:
break
else:
List.append(pic_url)
t = t + 60
return s
3.查找相似图片
def dowmloadPicture(html, keyword):
global num
# t =0
pic_url = re.findall('"objURL":"(.*?)",', html, re.S) # 先利用正则表达式找到图片url
print('找到关键词:' + keyword + '的图片,即将开始下载图片...')
for each in pic_url:
print('正在下载第' + str(num + 1) + '张图片,图片地址:' + str(each))
try:
if each is not None:
pic = requests.get(each, timeout=7)
else:
continue
except BaseException:
print('错误,当前图片无法下载')
continue
else:
string = file + r'\\' + keyword + '_' + str(num) + '.jpg'
fp = open(string, 'wb')
fp.write(pic.content)
fp.close()
num += 1
if num >= numPicture:
return
使用效果:
二、图片马赛克
当我们下载好了所需要的王心凌的照片,下一步我们需要用这些图片组成我们需要的文字。
所以首先准备一张要拼成的图片,比如需要组成的文字,如端午安康,我们可以用 PowerPoint)制作一个喜欢的文字样式,然后保存成jpg格式。
1.使用photomosaic库实现图片马赛克
代码如下所示:
import photomosaic as pm
# 加载要拼成的图片image(jpg 格式,图片越大,得到的拼图的每个小图分辨率越高)
image = pm.imread("1.jpg", as_gray=False)
# 从指定文件夹加载图片库(需要比较多的图片才能获得较好的效果)
pool = pm.make_pool("wxl/*.jpg")
# 制作 50*50 的拼图马赛克
mosaic = pm.basic_mosaic(image, pool, (200, 200))
# 保存拼图
pm.imsave("mosaic.jpg", mosaic)
不过由于这个库版本问题,所以需要在库函数里面做一点点修改才能成才运行。
def crop_to_fit(image, shape):
"""
Return a copy of image resized and cropped to precisely fill a shape.
To resize a colored 2D image, pass in a shape with two entries. When
``len(shape) < image.ndim``, higher dimensions are ignored.
Parameters
----------
image : array
shape : tuple
e.g., ``(height, width)`` but any length <= ``image.ndim`` is allowed
Returns
-------
cropped_image : array
"""
# Resize smallest dimension (width or height) to fit.
d = np.argmin(np.array(image.shape)[:2] / np.array(shape))
enlarged_shape = (tuple(np.ceil(np.array(image.shape[:len(shape)]) *
shape[d]/image.shape[d])) +
image.shape[len(shape):])
resized = resize(image, enlarged_shape,
mode='constant', anti_aliasing=False)
# Now the image is as large or larger than the shape along all dimensions.
# Crop any overhang in the other dimension.
crop_width = []
for actual, target in zip(resized.shape, shape):
overflow = actual - target
# Center the image and crop, biasing left if overflow is odd.
left_margin = int(np.floor(overflow / 2))
right_margin = int(np.ceil(overflow / 2))
crop_width.append((left_margin, right_margin))
# Do not crop any additional dimensions beyond those given in shape.
for _ in range(resized.ndim - len(shape)):
crop_width.append((0, 0))
cropped = crop(resized, crop_width)
return cropped
在裁剪图片的时候输入需要是int型,所以把left_margin和right_margin强制转化成int型。
实现效果:
效果确实一般般,局部放大,可以观察出,生成图是由照片拼接而成的,所以整体大图的清晰度也与小照片的数量有关,也有可能是图片分辨率大小以及组成图片尺寸不一的原因,所以又尝试了另一种方法。
2.计算颜色相似度实现图片马赛克
def readSourceImages(sourcepath,blocksize):
print('开始读取图像')
# 合法图像列表
sourceimages = []
# 平均颜色列表
avgcolors = []
for path in tqdm(glob.glob("{}/*.jpg".format(sourcepath))):
image = cv2.imread(path, cv2.IMREAD_COLOR)
if image.shape[-1] != 3:
continue
image = cv2.resize(image, (blocksize, blocksize))
avgcolor = np.sum(np.sum(image, axis=0), axis=0) / (blocksize * blocksize)
sourceimages.append(image)
avgcolors.append(avgcolor)
print('结束读取')
return sourceimages,np.array(avgcolors)
通过这个方法能够获取照片集中的每张照片与组成的大图之间颜色的相似度,将相似的照片放到对应的位置。
实现效果:
感觉效果比原来要好一些,但是还是不够清晰。
来源:https://blog.csdn.net/kobepaul123/article/details/125142951
0
投稿
猜你喜欢
- 可以通过model.state_dict()或者model.named_parameters()函数查看现在的全部可训练参数(包括通过继承得
- 几个月来好像就现在暂时无需求,稍微轻松一下,然后在Q群中发现有人提问,怎么用CSS实现数学公式“四又二分之一”。对于这个公式个人
- 以前我浏览博客的时候记得别人说过,BCELoss与CrossEntropyLoss都是用于分类问题。可以知道,BCELoss是Binary
- 在使用爬虫爬取网络数据时,如果长时间对一个网站进行抓取时可能会遇到IP被封的情况,这种情况可以使用代理更换ip来突破服务器封IP的限制。随手
- Python3.8 的热乎劲还没过去,Python 就又双叒叕要更新了。近日,3.9 版本的第四个 alpha 版已经开源。从文档中,我们可
- asp之家注:那么为什么要使用分页呢?当记录不多的时候,如10个或20个,我们可以也没必要使用分页来显示数据,但是数据是在不断增加的,当到了
- 平时写得多的是python,最近看了一点go,今天碰到了一个问题,和大家分享一下package mainimport "fmt&q
- Todo清单需要实现的功能有添加任务、删除任务、编辑任务,操作要关联数据库。任务需要绑定用户,部门。用户需要绑定部门。{#自己编写一个基类模
- 前言CUDA(Compute Unified Device Architecture)是NVIDIA推出的异构计算平台,PyTorch中有专
- 什么是 Socket?Socket又称"套接字",应用程序通常通过"套接字"向网络发出请求或者应答网
- 字符串与数字类型的转换什么是类型转换?—> 将自身的数据类型变成新的数据类型,并拥有新的数据类型的所有功能的过程即
- 导语《我的世界》是一款自由度极高的游戏,每个新存档的开启,就像是作为造物主的玩家在虚拟空间开辟了一个全新的宇宙。方块连接世界,云游大好河山。
- Python的3.0版本,常被称为Python 3000,或简称Py3k。相对于Python的早期版本,这是一个较大的升级。为了不带入过多的
- 下面看下通过Pyinstaller打包Pygame库写的小游戏程序出现的问题解决方法# -基于Python的Pygame库的GUI游戏游戏内
- 跨浏览器的本地存储(一):userData behaviorDOM Storage,是基于 Web Applications 1.0 spe
- 方法一.Image { max-width:600px;height:
- 代码如下:<% function CheckFileContent(FileName) dim 
- 现在假如要写一个按照"标题",'内容','作者'等等进行针对性的选择,这时需要涉及到使用
- 众所周知,随着数据库体积的日益庞大,其备份文件的大小也水涨船高。虽然说通过差异备份与完全备份配套策略,可以大大的减小SQL Server数据
- 一. XSL入门1.XSL---XML的样式表 HTML网页使用预先确定的标识(tags),这就是说所有的标记都有明确的含义,例如<p