zoukankan      html  css  js  c++  java
  • [ Python ] matplotlib

    # -*- coding: utf-8 -*-
    
    import unittest
    
    class TestClass1(unittest.TestCase):
    
        @unittest.skip("core.ok")
        def test_tuple(self):
            data = (11,22,33)
            print(data)
            x,y,z = data
            print(x,y,z)
    
        @unittest.skip("matplotlib.screen")
        def test_scrap_screen(self):
            from PIL import Image, ImageGrab
            import numpy as np
            from matplotlib import pyplot as plt
            sim = ImageGrab.grab()
            w,h = sim.size
            del(sim)
            dpi = plt.rcParams['figure.dpi']
            print("screen size: %d, %d" % (w, h))
            print("screen inch: %2.2f, %2.2f" %(w/dpi, h/dpi))
            djx = np.sqrt(w*w + h*h)
            print("screen djx = %d" % djx)
            print("screen dpi = %f" % (djx/15.4))
            print("screen size= %d, %d" % (w/221, h/221))
    
        # @unittest.skip("xxxyyyxxxyyyyy")
        def test_full_screen(self):
            from matplotlib import pyplot as plt
            from PIL import Image
            img_name = "/Users/jacobzhao/Downloads/sunyunzhu1.jpeg"
            img = Image.open(img_name)
            print("dip = %d" % plt.rcParams['figure.dpi'])
            plt.figure()
            plt.axis('off')
            ax = plt.gca()
            ax.spines['top'].set_visible(False)
            leg = plt.legend()
            leg.get_frame().set_linewidth(0.0)
            plt.subplots_adjust(hspace=0, wspace=0)
            plt.imshow(img)
            plt.show()
    
    if __name__ == '__main__':
        unittest.main()
        # suite = unittest.TestSuite()
        # suite.addTest(TestClass1('test_scrap_screen'))
        # runner = unittest.TextTestResult()
        # runner.run(suite)
    

      

    # -*- coding: utf-8 -*-
    from functools import reduce
    
    import numpy as np
    import matplotlib.pyplot as plt
    from PIL import Image
    
    class MyImage(object):
    
        def __init__(self, filepath):
            self.filepath = filepath
            self.imgfile = Image.open(filepath)
            if self.imgfile.mode != 'RGB':
                self.imgfile = self.imgfile.convert("RGB")
            self.imgdata = np.array(self.imgfile)
            self.info = {
                "mode": self.imgfile.mode,
                "shape":self.imgdata.shape
            }
    
        def showImg(self, title='NO-Title'):
            plt.figure(title)
            plt.imshow(self.imgdata)
            plt.axis('off')
            plt.show()
    
        def printInfo(self):
            print(self.info)
    
    
    class MyImage2(object):
    
        def __init__(self, filepath1, filepath2):
            self.filepath1 = filepath1
            self.filepath2 = filepath2
            self.imgfile1 = Image.open(filepath1)
            self.imgfile2 = Image.open(filepath2)
            if self.imgfile1.mode != 'RGB':
                self.imgfile1 = self.imgfile1.convert("RGB")
            if self.imgfile2.mode != 'RGB':
                self.imgfile2 = self.imgfile2.convert("RGB")
            self.imgdata1 = np.array(self.imgfile1)
            self.imgdata2 = np.array(self.imgfile2)
            self.info1 = {
                "mode": self.imgfile1.mode,
                "shape":self.imgdata1.shape
            }
            self.info2 = {
                "mode": self.imgfile2.mode,
                "shape":self.imgdata2.shape
            }
    
    
        def showImg(self, title1='Image-1', title2='Image-2'):
            fig = plt.figure(figsize=(10, 5))
            ax1 = fig.add_subplot(1, 2, 1)
            ax1.imshow(self.imgdata1)
            ax1.axis('off')
            plt.title(title1)
            ax2 = fig.add_subplot(1, 2, 2)
            ax2.imshow(self.imgdata2)
            ax2.axis('off')
            plt.title(title2)
            # plt.suptitle("Image-Test")
            plt.show()
    
        def printInfo(self):
            print(self.info1)
            print(self.info2)
    
    
    
    class MyImageN(object):
    
        def __init__(self, filepaths):
            self.size = len(filepaths)
            pos = list(range(self.size))
            self.filepaths = filepaths
            self.imgfiles = []
            self.imgdatas = []
            self.imginfos = []
            for index, filepath in zip(pos, filepaths):
                info={'title':'img-'+str(index)}
                self.imgfiles.append(Image.open(filepaths[index]))
                info['mode'] = self.imgfiles[index].mode
                if self.imgfiles[index].mode != 'RGB':
                    self.imgfiles[index] = self.imgfiles[index].convert('RGB')
                self.imgdatas.append(np.array(self.imgfiles[index]))
                info['shape'] = self.imgdatas[index].shape
                self.imginfos.append(info)
    
        def showImg(self, titles = None):
            fig = plt.figure(figsize=(4*self.size, 4))
            for index in range(self.size):
                ax = fig.add_subplot(1, self.size, index + 1)
                ax.imshow(self.imgdatas[index])
                ax.axis('off')
                plt.title("Image-"+str(index))
            # plt.suptitle("Image-Test")
            plt.show()
    
        def printInfos(self):
            for index in range(self.size):
                print(index+1, '->', self.imginfos[index])
    
        def processImage(self, index, func):
            curr_data = self.imgdatas[index]
            self.imgdatas[index] = func(self.imgfiles[index])
    
    def picGray(imgfile):
        return imgfile.convert('L')
    
    
    if __name__ == '__main__':
        syz = [
            r"/Users/jacobzhao/Downloads/sunyunzhu1.jpeg",
            r"/Users/jacobzhao/Downloads/sunyunzhu2.png",
            # r"/Users/jacobzhao/Downloads/sunyunzhu3.png",
        ]
        # MyImage(syz[0]).showImg()
        # MyImage(syz[1]).showImg()
        syzImgs = MyImageN(syz)
        # syzImgs.showImg()
        syzImgs.processImage(0, picGray)
        syzImgs.processImage(1, picGray)
        # syzImgs.processImage(2, picGray)
        syzImgs.printInfos()
        syzImgs.showImg()
    

      

    # -*- coding: utf-8 -*-
    
    import matplotlib.pyplot as plt
    import numpy as np
    
    fig = plt.figure(figsize=(8, 4))
    ax1 = fig.add_subplot(2,2,1)
    ax2 = fig.add_subplot(2,2,2)
    ax3 = fig.add_subplot(2,1,2)
    ax3.plot(np.random.randn(50).cumsum(), 'k--')
    ax1.hist(np.random.randn(100), bins=10, color='b', alpha=0.3)
    ax2.scatter(np.arange(30), np.arange(30) + 3*np.random.randn(30))
    plt.show()
    
    a = [x+11 for x in range(6)]
    b = [y+101 for y in range(6)]
    print(a, b)
    
    for m,n in zip(a,b):
        print(m, n)
    
    print(list(range(8)))
    

      

  • 相关阅读:
    usaco dec 2012 first!
    uva 1449
    unity3D学习笔记之DOTween插件的学习(持续更新)
    中南林业科技大学第十一届程序设计大赛 G 0和5
    中南林业科技大学第十一届程序设计大赛 D 最大的湖
    最小生成树之克鲁斯卡尔(Kruskal)算法
    最小生成树Prim算法理解
    判断无向图是否联通模板(并查集版)(DFS),BFS等其他方法将陆续更新
    矩阵压缩学习笔记
    C/C++ assert()函数用法总结
  • 原文地址:https://www.cnblogs.com/coder211/p/9055998.html
Copyright © 2011-2022 走看看