From 4b57345683891de2c1e83a07a14e516caf68bd2c Mon Sep 17 00:00:00 2001 From: wiredfool Date: Sat, 3 Dec 2016 14:45:05 +0000 Subject: [PATCH] Refactor random image --- Tests/test_file_jpeg.py | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 1b34b42c6..7cbb96c2e 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -1,7 +1,6 @@ from helper import unittest, PillowTestCase, hopper, py3 from helper import djpeg_available, cjpeg_available -import random from io import BytesIO import os @@ -29,6 +28,15 @@ class TestFileJpeg(PillowTestCase): im.bytes = test_bytes # for testing only return im + def gen_random_image(self, size, mode='RGB'): + """ Generates a very hard to compress file + :param size: tuple + :param mode: optional image mode + + """ + return Image.frombytes(mode, size, + os.urandom(size[0]*size[1]*len(mode))) + def test_sanity(self): # internal version number @@ -159,12 +167,7 @@ class TestFileJpeg(PillowTestCase): def test_progressive_large_buffer_highest_quality(self): f = self.tempfile('temp.jpg') - if py3: - a = bytes(random.randint(0, 255) for _ in range(256 * 256 * 3)) - else: - a = b''.join(chr(random.randint(0, 255)) for _ in range( - 256 * 256 * 3)) - im = Image.frombuffer("RGB", (256, 256), a, "raw", "RGB", 0, 1) + im = self.gen_random_image((255,255)) # this requires more bytes than pixels in the image im.save(f, format="JPEG", progressive=True, quality=100) @@ -436,14 +439,7 @@ class TestFileJpeg(PillowTestCase): self.assertEqual(tag_ids['RelatedImageLength'], 0x1002) def test_MAXBLOCK_scaling(self): - def gen_random_image(size): - """ Generates a very hard to compress file - :param size: tuple - """ - return Image.frombytes('RGB', - size, os.urandom(size[0]*size[1] * 3)) - - im = gen_random_image((512, 512)) + im = self.gen_random_image((512, 512)) f = self.tempfile("temp.jpeg") im.save(f, quality=100, optimize=True)