小陆同学

python 中文名:蟒蛇,设计者:Guido van Rossum

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

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.
复制代码

 

posted on   小陆同学  阅读(2667)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
历史上的今天:
2019-01-16 MongoDB导入导出备份恢复
点击右上角即可分享
微信分享提示