0


图片批量下载 +图片马赛克:多张图片组成端午安康

📢📢📢📣📣📣
哈喽!大家好,我是Yunlord,CSDN2021年博客之星人工智能领域第三名,多年人工智能学习工作经验,一位兴趣稀奇古怪的【人工智能领域博主】!😜😜😜
擅长图像识别、自然语言处理等多个人工智能领域,同时精通python,并且在不断拓展自身领域进行学习,致力于有趣好玩的技术推广和应用!!!💞💞💞
✨ ✨✨如果有对新奇技术感兴趣的朋友们,欢迎持续关注Yunlord❤️❤️❤️

前言

前段时间看到有人问如何使用Python实现多张图片组成文字的效果?觉得还挺有意思,于是尝试做了一下,刚好赶上端午节,所以打算从网上下载1000张王心凌的照片,组成端午安康的字样,给大家送上祝福。

一、图片批量下载

首先我们需要从百度下载大量王心凌的图片,但是如果会去百度图片里一张张右键下载,但这样未免太麻烦了,所以打算直接用python写一个批量下载图片的代码,输入想要下载图片的关键字,然后输入想要下载图片的数量,就可以成功下载图片了!

代码主要分成三个部分:

  1. 下载图片
  2. 检测图片数量
  3. 查找相似图片

1.下载图片

  1. def dowmloadPicture(html, keyword):
  2. global num
  3. # t =0
  4. pic_url = re.findall('"objURL":"(.*?)",', html, re.S) # 先利用正则表达式找到图片url
  5. print('找到关键词:' + keyword + '的图片,即将开始下载图片...')
  6. for each in pic_url:
  7. print('正在下载第' + str(num + 1) + '张图片,图片地址:' + str(each))
  8. try:
  9. if each is not None:
  10. pic = requests.get(each, timeout=7)
  11. else:
  12. continue
  13. except BaseException:
  14. print('错误,当前图片无法下载')
  15. continue
  16. else:
  17. string = file + r'\\' + keyword + '_' + str(num) + '.jpg'
  18. fp = open(string, 'wb')
  19. fp.write(pic.content)
  20. fp.close()
  21. num += 1
  22. if num >= numPicture:
  23. return

2.检测图片数量

  1. def Find(url, A):
  2. global List
  3. print('正在检测图片总数,请稍等.....')
  4. t = 0
  5. i = 1
  6. s = 0
  7. while t < 1000:
  8. Url = url + str(t)
  9. try:
  10. # 这里搞了下
  11. Result = A.get(Url, timeout=7, allow_redirects=False)
  12. except BaseException:
  13. t = t + 60
  14. continue
  15. else:
  16. result = Result.text
  17. pic_url = re.findall('"objURL":"(.*?)",', result, re.S) # 先利用正则表达式找到图片url
  18. s += len(pic_url)
  19. if len(pic_url) == 0:
  20. break
  21. else:
  22. List.append(pic_url)
  23. t = t + 60
  24. return s

3.查找相似图片

  1. def dowmloadPicture(html, keyword):
  2. global num
  3. # t =0
  4. pic_url = re.findall('"objURL":"(.*?)",', html, re.S) # 先利用正则表达式找到图片url
  5. print('找到关键词:' + keyword + '的图片,即将开始下载图片...')
  6. for each in pic_url:
  7. print('正在下载第' + str(num + 1) + '张图片,图片地址:' + str(each))
  8. try:
  9. if each is not None:
  10. pic = requests.get(each, timeout=7)
  11. else:
  12. continue
  13. except BaseException:
  14. print('错误,当前图片无法下载')
  15. continue
  16. else:
  17. string = file + r'\\' + keyword + '_' + str(num) + '.jpg'
  18. fp = open(string, 'wb')
  19. fp.write(pic.content)
  20. fp.close()
  21. num += 1
  22. if num >= numPicture:
  23. return

使用效果:

二、图片马赛克

当我们下载好了所需要的王心凌的照片,下一步我们需要用这些图片组成我们需要的文字。

所以首先准备一张要拼成的图片,比如需要组成的文字,如端午安康,我们可以用 PowerPoint)制作一个喜欢的文字样式,然后保存成jpg格式。

1.使用photomosaic库实现图片马赛克

代码如下所示:

  1. import photomosaic as pm
  2. # 加载要拼成的图片image(jpg 格式,图片越大,得到的拼图的每个小图分辨率越高)
  3. image = pm.imread("1.jpg", as_gray=False)
  4. # 从指定文件夹加载图片库(需要比较多的图片才能获得较好的效果)
  5. pool = pm.make_pool("wxl/*.jpg")
  6. # 制作 50*50 的拼图马赛克
  7. mosaic = pm.basic_mosaic(image, pool, (200, 200))
  8. # 保存拼图
  9. pm.imsave("mosaic.jpg", mosaic)

不过由于这个库版本问题,所以需要在库函数里面做一点点修改才能成才运行。

  1. def crop_to_fit(image, shape):
  2. """
  3. Return a copy of image resized and cropped to precisely fill a shape.
  4. To resize a colored 2D image, pass in a shape with two entries. When
  5. ``len(shape) < image.ndim``, higher dimensions are ignored.
  6. Parameters
  7. ----------
  8. image : array
  9. shape : tuple
  10. e.g., ``(height, width)`` but any length <= ``image.ndim`` is allowed
  11. Returns
  12. -------
  13. cropped_image : array
  14. """
  15. # Resize smallest dimension (width or height) to fit.
  16. d = np.argmin(np.array(image.shape)[:2] / np.array(shape))
  17. enlarged_shape = (tuple(np.ceil(np.array(image.shape[:len(shape)]) *
  18. shape[d]/image.shape[d])) +
  19. image.shape[len(shape):])
  20. resized = resize(image, enlarged_shape,
  21. mode='constant', anti_aliasing=False)
  22. # Now the image is as large or larger than the shape along all dimensions.
  23. # Crop any overhang in the other dimension.
  24. crop_width = []
  25. for actual, target in zip(resized.shape, shape):
  26. overflow = actual - target
  27. # Center the image and crop, biasing left if overflow is odd.
  28. left_margin = int(np.floor(overflow / 2))
  29. right_margin = int(np.ceil(overflow / 2))
  30. crop_width.append((left_margin, right_margin))
  31. # Do not crop any additional dimensions beyond those given in shape.
  32. for _ in range(resized.ndim - len(shape)):
  33. crop_width.append((0, 0))
  34. cropped = crop(resized, crop_width)
  35. return cropped

在裁剪图片的时候输入需要是int型,所以把left_margin和right_margin强制转化成int型。

实现效果:

效果确实一般般,局部放大,可以观察出,生成图是由照片拼接而成的,所以整体大图的清晰度也与小照片的数量有关,也有可能是图片分辨率大小以及组成图片尺寸不一的原因,所以又尝试了另一种方法。

2.计算颜色相似度实现图片马赛克

  1. def readSourceImages(sourcepath,blocksize):
  2. print('开始读取图像')
  3. # 合法图像列表
  4. sourceimages = []
  5. # 平均颜色列表
  6. avgcolors = []
  7. for path in tqdm(glob.glob("{}/*.jpg".format(sourcepath))):
  8. image = cv2.imread(path, cv2.IMREAD_COLOR)
  9. if image.shape[-1] != 3:
  10. continue
  11. image = cv2.resize(image, (blocksize, blocksize))
  12. avgcolor = np.sum(np.sum(image, axis=0), axis=0) / (blocksize * blocksize)
  13. sourceimages.append(image)
  14. avgcolors.append(avgcolor)
  15. print('结束读取')
  16. return sourceimages,np.array(avgcolors)

通过这个方法能够获取照片集中的每张照片与组成的大图之间颜色的相似度,将相似的照片放到对应的位置。

实现效果:

感觉效果比原来要好一些,但是还是不够清晰。

另一篇文章2万8千张图片如何用python组成一张你女朋友的自画像!做出的效果图:

感觉效果好很多,可能还是因为文字图片分辨率不够高的原因吧。

总结

这次分享了如何利用图片批量下载以及图片马赛克实现多张图片组成文字的效果,希望小伙伴们多多支持,有什么问题可以在评论区里留言讨论。


参考:

​​​​​​Python 爬虫系列教程一爬取批量百度图片

如何使用Python做出,很多很多张图片组成文字的效果?

2万8千张图片如何用python组成一张你女朋友的自画像!


本文转载自: https://blog.csdn.net/kobepaul123/article/details/125142951
版权归原作者 Yunlord 所有, 如有侵权,请联系我们删除。

“图片批量下载 +图片马赛克:多张图片组成端午安康”的评论:

还没有评论