def test_non_indexed_interlaced(self): img = Image.open_from_path(_get_filepath('black-white-non-indexed-interlaced-adam7.png')) print(img.pixels) self.assertEqual(img.get_color(0, 0), BLACK) self.assertEqual(img.get_color(1, 1), BLACK) self.assertEqual(img.get_color(0, 1), WHITE) self.assertEqual(img.get_color(1, 0), WHITE)
def test_decode(self): img = Image.open_from_path(_get_filepath('black-white-100.jpg')) self.assertEqual(img.get_color(0, 0), Black) # TODO: Is this correct? Is this just JPEG being JPEG or is the decoder # buggy? 1/1 SHOULD be BLACK but it's 8 8 8. self.assertEqual(img.get_color(1, 1), ALMOST_BLACK) self.assertEqual(img.get_color(0, 1), White) self.assertEqual(img.get_color(1, 0), White)
def test_decode(self): img = Image.open_from_path( get_test_file(__file__, 'black-white-100.jpg')) self.assertImage( img, [ [Black, White], [White, ALMOST_BLACK] # no clue why this is "almost" black ], False)
def index(request): if request.method == 'POST': form = PymagingForm(request.POST, request.FILES) else: form = PymagingForm() if form.is_valid(): response = HttpResponse(content_type='image/png') image = Image.open(form.cleaned_data['image']) image = image.resize(200, 200) image.save(response, 'png') return response context = RequestContext(request) context['form'] = form context['python'] = sys.version context['django'] = django.get_version() return render_to_response('index.html', context)
def lambda_handler(event, context): print("Received event: " + json.dumps(event, indent=2)) srcBucket = event.Records[0].s3.bucket.name srcKey = event.Records[0].s3.object.key dstBucket = srcBucket dstKey = 'thumbs/' + srcKey bucket = s3.Bucket(srcBucket) imageObject = bucket.Object(srcKey) metadata = image.metadata if 'width' in metadata: max_width = metadata['width'] else: max_width = DEFAULT_MAX_WIDTH if 'height' in metadata: max_height = metadata['height'] else: max_height = DEFAULT_MAX_HEIGHT scalingFactor = min(max_width / size.width, max_height / size.height) width = scalingFactor * size.width height = scalingFactor * size.height random_file_name = uuid.uuid4() + srcKey image_path = '/tmp/' + random_file_name thumbnail_path = '/tmp/resized-' + random_file_name s3.download_file(srcBucket, srcKey, image_path) resize_image(image_path, thumbnail_path) with Image.open_from_path(image_path) as image: image.img.resize(width, height) image.save(thumbnail_path) s3.upload_file(thumbnail_path, dstBucket, dstKey)
def _channels_data_to_image(channels_data, mode, size, depth): if size == (0, 0): return num_channels = mode.length assert depth == 8 assert len(channels_data) == num_channels total_size = size[0] * size[1] * num_channels image_bytes = array.array(str("B"), [0] * total_size) for index, channel in enumerate(channels_data): data = channel.data # zip and zip-with-prediction data is already decoded if channel.compression == Compression.PACK_BITS: data = packbits.decode(data) image_bytes[index::num_channels] = array.array(str("B"), data) pixels = get_pixel_array(image_bytes, size[0], size[1], mode.length) return Image(pixels, mode)
def new_image(self, **kwargs): return Image.new(RGB, self.pixel_size, self.pixel_size, White)
from pymaging import Image Image.open_from_path('testimage.png').resize(160, 240).save_to_path('resized_benchimage.png')
from timeit import default_timer as time import sys from pymaging import Image start_time = time() elapsed = 0 for i in xrange(0, 1000): Image.open_from_path('testimage.png').flip_left_right().save_to_path('benchimage.png') elapsed = time() - start_time if elapsed > 1.0: break trials = i + 1 print "{trials:d} iterations in {elapsed:0.2f} seconds ({average:0.2f}s/call)".format(trials=trials, elapsed=elapsed, average=elapsed / trials)
def test_with_transparency(self): img = Image.open_from_path(_get_filepath('black-white-with-transparency.png')) self.assertEqual(img.get_color(0, 0), BLACK) self.assertEqual(img.get_color(1, 0), WHITE) self.assertEqual(img.get_color(1, 1), BLACK.get_for_brightness(0.5)) self.assertEqual(img.get_color(0, 1), WHITE.get_for_brightness(0.5))
def test_non_indexed(self): img = Image.open_from_path(_get_filepath('black-white-non-indexed.png')) self.assertEqual(img.get_color(0, 0), BLACK) self.assertEqual(img.get_color(1, 1), BLACK) self.assertEqual(img.get_color(0, 1), WHITE) self.assertEqual(img.get_color(1, 0), WHITE)
def test_decode(self): img = Image.open_from_path(get_test_file(__file__, "black-white-100.jpg")) self.assertImage(img, [[Black, White], [White, ALMOST_BLACK]], False) # no clue why this is "almost" black