[ Python ] matplotlib

來源:互聯網
上載者:User

標籤:ace   index   pytho   res   result   int   param   end   print   

# -*- coding: utf-8 -*-import unittestclass 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 reduceimport numpy as npimport matplotlib.pyplot as pltfrom PIL import Imageclass 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 pltimport numpy as npfig = 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)))

  

[ Python ] matplotlib

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.