Python实现多张图片合成文字的效果
作者:Yunlord 时间:2021-02-16 17:12:12
前言
前段时间看到有人问如何使用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
标签:Python,图片,合成,文字
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
mysql5在rhel5下乱码问题及解决方法
2010-12-03 16:26:00
![](https://img.aspxhome.com/file/UploadPic/201012/3/2010123165047481s.png)
ipad上运行python的方法步骤
2021-12-06 19:05:36
php中Ctype函数用法详解
2023-06-19 01:43:13
python基础while循环及if判断的实例讲解
2021-02-18 06:56:06
python opencv 简单阈值算法的实现
2023-04-04 04:23:03
![](https://img.aspxhome.com/file/2023/9/83819_0s.png)
Django基础之Model操作步骤(介绍)
2022-04-12 12:17:50
![](https://img.aspxhome.com/file/2023/2/82692_0s.png)
新Orcas语言特性:扩展方法
2007-09-23 12:49:00
![](https://img.aspxhome.com/file/uploadpic/20079/23/200792312539649.jpg)
python文件目录操作之os模块
2023-01-10 14:22:59
Oracle逗号分隔列转行实现方法
2011-01-04 20:13:00
python3 BeautifulSoup模块使用字典的方法抓取a标签内的数据示例
2022-12-24 23:49:30
简化翻页提升可用性
2009-03-28 11:52:00
![](https://img.aspxhome.com/file/UploadPic/20093/28/wp_pagenavi_1_en-28s.png)
Python使用当前时间、随机数产生一个唯一数字的方法
2022-02-10 14:57:07
![](https://img.aspxhome.com/file/2023/6/67926_0s.png)
Django模型层实现多表关系创建和多表操作
2022-12-01 09:13:46
详解python使用Nginx和uWSGI来运行Python应用
2023-07-25 20:40:55
oracle执行cmd的实现方法
2009-04-24 12:10:00
laravel多视图共享数据实例代码
2023-06-19 01:34:14
python+ffmpeg视频并发直播压力测试
2023-09-02 10:30:33
Mysql日期和时间函数大全
2011-03-08 09:52:00
PyQt5高级界面控件之QTableWidget的具体使用方法
2023-09-05 15:36:41
![](https://img.aspxhome.com/file/2023/0/72240_0s.png)
微信小程序使用Vant Weapp组件库的方法步骤
2023-09-11 15:17:34
![](https://img.aspxhome.com/file/2023/6/56286_0s.jpg)