Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display two png images simultaneously using pylab

I want to open two png image files and display them side by side for visual comparison. I have this code for opening one png file (which I got from unutbu on stackoverflow.com):

import numpy as np
import pylab
import matplotlib.cm as cm
import Image

fname='file.png'
image=Image.open(fname).convert("L")
arr=np.asarray(image)
pylab.imshow(arr,cmap=cm.Greys_r)
pylab.title('title')
pylab.show()

Is there a way to modify this code to open and display 2 png files side by side with their own titles?

like image 914
balibakbar Avatar asked Apr 29 '12 15:04

balibakbar


1 Answers

The following works for me (you can comment/uncomment the lines in the code to change the layout of the "composite" image):

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import numpy as np
import pylab
import matplotlib.cm as cm
import Image

f = pylab.figure()
for n, fname in enumerate(('1.png', '2.png')):
    image=Image.open(fname).convert("L")
    arr=np.asarray(image)
    f.add_subplot(2, 1, n)  # this line outputs images on top of each other
    # f.add_subplot(1, 2, n)  # this line outputs images side-by-side
    pylab.imshow(arr,cmap=cm.Greys_r)
pylab.title('Double image')
pylab.show()

EDIT: screenshot:

enter image description here

like image 109
mac Avatar answered Sep 24 '22 06:09

mac