def test_should_preserve_png_transparency(self): engine = Engine(self.context) with open(join(STORAGE_PATH, "paletted-transparent.png"), "rb") as image_file: buffer = image_file.read() engine.load(buffer, "png") expect(engine.original_mode).to_equal("P") engine.resize(200, 150) img = Image.open(BytesIO(engine.read(".png"))) expect(img.mode).to_equal("P") expect(img.format.lower()).to_equal("png") transparent_pixels_count = sum( img.convert("RGBA").split()[3] # Get alpha channel .point( lambda x: 0 if x else 1) # return 1 if pixel is transparent, 0 otherwise .getdata()) # Image has total of 200x150=30000 pixels. Most of them should be transparent expect(transparent_pixels_count).to_be_greater_than(19000)
def test_load_tif_8bit_per_channel(self): engine = Engine(self.context) with open(join(STORAGE_PATH, 'gradient_8bit.tif'), 'rb') as im: buffer = im.read() expect(buffer).not_to_equal(None) engine.load(buffer, None) final_bytes = BytesIO(engine.read()) im = Image.open(final_bytes) expect(im.format).to_equal('PNG') expect(im.size).to_equal((100, 100))
def test_load_tif_8bit_per_channel(self): engine = Engine(self.context) with open(join(STORAGE_PATH, 'gradient_8bit.tif'), 'r') as im: buffer = im.read() expect(buffer).not_to_equal(None) engine.load(buffer, None) final_bytes = BytesIO(engine.read()) im = Image.open(final_bytes) expect(im.format).to_equal('PNG') expect(im.size).to_equal((100, 100))
def test_load_tif_8bit_per_channel(self): engine = Engine(self.context) with open(join(STORAGE_PATH, "gradient_8bit.tif"), "rb") as image_file: buffer = image_file.read() expect(buffer).not_to_equal(None) engine.load(buffer, None) final_bytes = BytesIO(engine.read()) image_file = Image.open(final_bytes) expect(image_file.format).to_equal("PNG") expect(image_file.size).to_equal((100, 100))
def test_convert_png_1bit_to_png(self): engine = Engine(self.context) with open(join(STORAGE_PATH, '1bit.png'), 'rb') as im: buffer = im.read() engine.load(buffer, '.png') expect(engine.original_mode).to_equal('P') # Note that this is not a true 1bit image, it's 8bit in black/white. engine.resize(10, 10) mode, _ = engine.image_data_as_rgb() expect(mode).to_equal('RGB') final_bytes = BytesIO(engine.read()) mode = Image.open(final_bytes).mode expect(mode).to_equal('P')
def test_convert_should_preserve_palette_mode(self): engine = Engine(self.context) with open(join(STORAGE_PATH, '256_color_palette.png'), 'rb') as im: buffer = im.read() engine.load(buffer, '.png') expect(engine.original_mode).to_equal('P') engine.resize(10, 10) mode, _ = engine.image_data_as_rgb() expect(mode).to_equal('RGB') final_bytes = BytesIO(engine.read()) mode = Image.open(final_bytes).mode expect(mode).to_equal('P')
def test_convert_png_1bit_to_png(self): engine = Engine(self.context) with open(join(STORAGE_PATH, '1bit.png'), 'r') as im: buffer = im.read() engine.load(buffer, '.png') expect(engine.original_mode).to_equal('P') # Note that this is not a true 1bit image, it's 8bit in black/white. engine.resize(10, 10) mode, _ = engine.image_data_as_rgb() expect(mode).to_equal('RGB') final_bytes = BytesIO(engine.read()) mode = Image.open(final_bytes).mode expect(mode).to_equal('P')
def test_convert_should_preserve_palette_mode(self): engine = Engine(self.context) with open(join(STORAGE_PATH, '256_color_palette.png'), 'r') as im: buffer = im.read() engine.load(buffer, '.png') expect(engine.original_mode).to_equal('P') engine.resize(10, 10) mode, _ = engine.image_data_as_rgb() expect(mode).to_equal('RGB') final_bytes = BytesIO(engine.read()) mode = Image.open(final_bytes).mode expect(mode).to_equal('P')
def test_convert_should_preserve_palette_mode(self): engine = Engine(self.context) with open(join(STORAGE_PATH, "256_color_palette.png"), "rb") as image_file: buffer = image_file.read() engine.load(buffer, ".png") expect(engine.original_mode).to_equal("P") engine.resize(10, 10) mode, _ = engine.image_data_as_rgb() expect(mode).to_equal("RGB") final_bytes = BytesIO(engine.read()) mode = Image.open(final_bytes).mode expect(mode).to_equal("P")
def test_should_preserve_png_transparency(self): engine = Engine(self.context) with open(join(STORAGE_PATH, 'paletted-transparent.png'), 'r') as im: buffer = im.read() engine.load(buffer, 'png') expect(engine.original_mode).to_equal('P') engine.resize(200, 150) img = Image.open(BytesIO(engine.read('.png'))) expect(img.mode).to_equal('P') expect(img.format.lower()).to_equal('png') transparent_pixels_count = sum(img.convert('RGBA') .split()[3] # Get alpha channel .point(lambda x: 0 if x else 1) # return 1 if pixel is transparent, 0 otherwise .getdata()) # Image has total of 200x150=30000 pixels. Most of them should be transparent expect(transparent_pixels_count).to_be_greater_than(19000)