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


    方法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.
  • 相关阅读:
    $_SERVER
    下面介绍mysql中模糊查询的四种用法:
    qq第三方登录
    远程连接数据库出错
    lnmp中的tp的pathinfo模式
    TP5配置所谓的url_moudel
    tp3.2.3中的xss攻击基本防护
    tp中的Csv文件读取(原创)
    mysql语句整理
    SVN的详细使用
  • 原文地址:https://www.cnblogs.com/lutt/p/15808886.html
Copyright © 2020-2023  润新知