ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python--将多张图片显示在一张画布上

2022-01-16 11:02:51  阅读:176  来源: 互联网

标签:python IMAGE cv2 画布 -- install pip image SIZE


方法1:pillow将多张图片显示在一张画布上

缺点:每张画布上图片个数必须是给定数量

class MakeCodeAction(BaseActionView):
    # 这里需要填写三个属性
    action_name = "create_assets_qr_code"
    description = u'Create selected IT二维码'
    model_perm = 'change'
    def do_action(self,queryset):
        img_list = []
        for obj in queryset:
            code_str = 'http://{url}/assets/decode_code/?num={OYcode}'.format(url=self.request.META['HTTP_HOST'],
                                                                              OYcode=obj.num)
            filename = CreateCode().make_code(text=code_str)
            img_list.append(filename)
        IMAGE_SIZE = 256  # 每张小图片的大小
        IMAGE_ROW = 5  # 图片间隔,也就是合并成一张图后,一共有几行
        IMAGE_COLUMN = 4  # 图片间隔,也就是合并成一张图后,一共有几列
        IMAGE_SAVE_PATH = 'media/qrcode_all/{}.png'.format(uuid.uuid4().hex[4:])  # 图片转换后的地址
        # 简单的对于参数的设定和实际图片集的大小进行数量判断
        if len(img_list) != IMAGE_ROW * IMAGE_COLUMN:
            raise ValueError("合成图片的参数和要求的数量不能匹配!")

        # 定义图像拼接函数
        import PIL.Image as Image
        # def image_compose():
        to_image = Image.new('RGB', (IMAGE_COLUMN * IMAGE_SIZE, IMAGE_ROW * IMAGE_SIZE))  # 创建一个新图
        # 循环遍历,把每张图片按顺序粘贴到对应位置上
        for y in range(1, IMAGE_ROW + 1):
            for x in range(1, IMAGE_COLUMN + 1):
                from_image = Image.open(img_list[IMAGE_COLUMN * (y - 1) + x - 1]).resize((IMAGE_SIZE, IMAGE_SIZE), Image.ANTIALIAS)
                to_image.paste(from_image, ((x - 1) * IMAGE_SIZE, (y - 1) * IMAGE_SIZE))
        to_image.save(IMAGE_SAVE_PATH)  # 保存新图
        if os.path.exists(IMAGE_SAVE_PATH):
            qrimg_data = open(IMAGE_SAVE_PATH, 'rb').read()
            return HttpResponse(qrimg_data, content_type="image/png")

方法2:opencv-pyplot多张图片显示在一张图片上

缺点: 只能横向或竖向排一列

import cv2
from pylab import * 

img1 = cv2.imread('logo.jpg',cv2.IMREAD_COLOR)
img2 = cv2.imread('logo.jpg',cv2.IMREAD_GRAYSCALE)
img3 = cv2.imread('logo.jpg',cv2.IMREAD_UNCHANGED)
img4 = cv2.imread('logo.jpg')

htitch= np.hstack((img1, img3,img4)) # 横向排列
vtitch = np.vstack((img1, img3)) # 纵向排列
cv2.imshow("test1",htitch)
cv2.imshow("test2",vtitch)

cv2.waitKey(0)
cv2.destroyAllWindows()


ImportError: No module named cv2
安装:pip2 install opencv-python==4.2.0.32
No module named 'pylab'
安装:pip2 install matplotlib
ERROR: Cannot uninstall 'pyparsing'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
解决方法是手动重装最新版 pyparsing
sudo pip install -I pyparsing==2.2.0


pip install matplotlib


ImportError: No module named Tkinter
安装:
yum install -y tkinter
yum install -y tk-devel

DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.

 

标签:python,IMAGE,cv2,画布,--,install,pip,image,SIZE
来源: https://www.cnblogs.com/lutt/p/15808886.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有