def test_sanity(): tar = TarIO.TarIO(tarfile, 'lena.png') im = Image.open(tar) im.load() assert_equal(im.mode, "RGB") assert_equal(im.size, (128, 128)) assert_equal(im.format, "PNG") tar = TarIO.TarIO(tarfile, 'lena.jpg') im = Image.open(tar) im.load() assert_equal(im.mode, "RGB") assert_equal(im.size, (128, 128)) assert_equal(im.format, "JPEG")
def test_sanity(self): if "zip_decoder" in codecs: tar = TarIO.TarIO(tarfile, 'lena.png') im = Image.open(tar) im.load() self.assertEqual(im.mode, "RGB") self.assertEqual(im.size, (128, 128)) self.assertEqual(im.format, "PNG") if "jpeg_decoder" in codecs: tar = TarIO.TarIO(tarfile, 'lena.jpg') im = Image.open(tar) im.load() self.assertEqual(im.mode, "RGB") self.assertEqual(im.size, (128, 128)) self.assertEqual(im.format, "JPEG")
def test_sanity(self): for codec, test_path, format in [ ["zip_decoder", "hopper.png", "PNG"], ["jpeg_decoder", "hopper.jpg", "JPEG"], ]: if codec in codecs: tar = TarIO.TarIO(TEST_TAR_FILE, test_path) im = Image.open(tar) im.load() self.assertEqual(im.mode, "RGB") self.assertEqual(im.size, (128, 128)) self.assertEqual(im.format, format)
def test_sanity(): for codec, test_path, format in [ ["zlib", "hopper.png", "PNG"], ["jpg", "hopper.jpg", "JPEG"], ]: if features.check(codec): with TarIO.TarIO(TEST_TAR_FILE, test_path) as tar: with Image.open(tar) as im: im.load() assert im.mode == "RGB" assert im.size == (128, 128) assert im.format == format
def test_sanity(self): for codec, test_path, format in [ ['zip_decoder', 'hopper.png', 'PNG'], ['jpeg_decoder', 'hopper.jpg', 'JPEG'] ]: if codec in codecs: tar = TarIO.TarIO(TEST_TAR_FILE, test_path) im = Image.open(tar) im.load() self.assertEqual(im.mode, "RGB") self.assertEqual(im.size, (128, 128)) self.assertEqual(im.format, format)
def test_close(self): tar = TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg") tar.close()
def open(): with TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg"): pass
def open(): tar = TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg") tar.close()
def open(): TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg")
# ==================================================== # more on reading images # As described earlier, the open() function of the Image module is used to open an image file. In most cases, # you simply pass it the filename as an argument. img = Image.open('sample.jpg') with open('sample.jpg', 'rb') as fp: img = Image.open(fp) # img.show() # you can use ContaineIO or TarIO module to access image file embedded in a larger file. # Reading from a tar archive. img = TarIO.TarIO('sample.tar', 'sample.jpg') img = Image.open(img) # img.show() # ==================================================== # controlling the decoder. # Some decoders allow you to manipulate the image while reading it from a file. This can often be used to speed up # decoding when creating thumbnails (when speed is usually more important thatn quality) and printing to a monochrome # laser printer. # The draft() method manipulates an opened but not yet loaded image so it as closely as possible matches the given mode and size. # Reading in draft mode. from PIL import Image img = Image.open('sample.jpg') print('original = ', img.mode, im.size)
def test_contextmanager(): with warnings.catch_warnings(): with TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg"): pass
ImageEnhance.Sharpness ''' enh = ImageEnhance.Sharpness(im) enh.enhance(30) # Image sequences ''' im = Image.open("animation.gif") im.seek(1) # skip to the second frame try: while 1: im.seek(im.tell()+1) # do something to im except EOFError: pass # end of sequence ''' # More on reading images ## Reading from a tar archive from PIL import TarIO fp = TarIO.TarIO('data.tar', 'data/test.png') im_tar = Image.open(fp) # PIL to numpy ## PIL to numpy array im_ndarray = np.array(im_tar) ## numpy array to PIL im = Image.fromarray(im_ndarray) im.show()
def test_contextmanager(): with pytest.warns(None) as record: with TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg"): pass assert not record
def test_close(): with pytest.warns(None) as record: tar = TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg") tar.close() assert not record
# open_image_from_tar.py from PIL import Image, TarIO fobj = TarIO.TarIO("flowers.tar", "flowers.jpg") image = Image.open(fobj) image.show()
def test_contextmanager(self): with TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg"): pass
for main_name in os.listdir(data_dir): # 将tar文件加入到地址中,进而遍历,获取类名,每一个类似于 ‘n01440764.tar’, # 进入训练集,里面还会有1000个压缩包,分别代表一类,其中包含很多图像 sub_dir = os.path.join(data_dir, main_name) print("The dir of sub class", sub_dir) class_name = main_name.split('.')[0] trainname_class.append(class_name) file = tarfile.open(sub_dir, "r") file_list = [] # 遍历每一个 tar 文件, 进而学习特征 for i in file.getmembers(): # 获取文件名,每一个类似于 ‘n01440764.tar’ file_name = i.name print("name of pictures ", file_name) file_list.append(file_name) fp = TarIO.TarIO(sub_dir, file_name) # 打开图片 im = Image.open(fp) # 对所有的图片进行裁剪,重新标定尺寸 img = im.resize((227, 227), Image.ANTIALIAS) #从Image类转化为 numpy array, 事实上此时的图片已经被转换成数组,227*227*3。 img = np.asarray(img) if img.size == 227 * 227: img = np.concatenate((img, img, img), axis=0) img = np.array(img) # reshape 图片,方便放入dicts img = img.reshape([227 * 227 * 3]) if traindata == [[]]: traindata = [img] else:
def test_close(): with warnings.catch_warnings(): tar = TarIO.TarIO(TEST_TAR_FILE, "hopper.jpg") tar.close()