码迷,mamicode.com
首页 > 编程语言 > 详细

[ Python ] matplotlib

时间:2018-05-18 15:34:53      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:ace   index   pytho   res   result   int   param   end   print   

# -*- 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)))

  

[ Python ] matplotlib

标签:ace   index   pytho   res   result   int   param   end   print   

原文地址:https://www.cnblogs.com/coder211/p/9055998.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!