Python PIL.ImageFile.MAXBLOCK Examples

The following are 24 code examples of PIL.ImageFile.MAXBLOCK(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module PIL.ImageFile , or try the search function .
Example #1
Source File: videos_to_tfrecords.py    From multilabel-image-classification-tensorflow with MIT License 6 votes vote down vote up
def JpegString(image, jpeg_quality=90):
  """Returns given PIL.Image instance as jpeg string.

  Args:
    image: A PIL image.
    jpeg_quality: The image quality, on a scale from 1 (worst) to 95 (best).

  Returns:
    a jpeg_string.
  """
  # This fix to PIL makes sure that we don't get an error when saving large
  # jpeg files. This is a workaround for a bug in PIL. The value should be
  # substantially larger than the size of the image being saved.
  ImageFile.MAXBLOCK = 640 * 512 * 64

  output_jpeg = StringIO()
  image.save(output_jpeg, 'jpeg', quality=jpeg_quality, optimize=True)
  return output_jpeg.getvalue() 
Example #2
Source File: test_file_jpeg.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_icc_big(self):
        # Make sure that the "extra" support handles large blocks
        def test(n):
            # The ICC APP marker can store 65519 bytes per marker, so
            # using a 4-byte test code should allow us to detect out of
            # order issues.
            icc_profile = (b"Test"*int(n/4+1))[:n]
            self.assertEqual(len(icc_profile), n)  # sanity
            im1 = self.roundtrip(hopper(), icc_profile=icc_profile)
            self.assertEqual(im1.info.get("icc_profile"), icc_profile or None)
        test(0)
        test(1)
        test(3)
        test(4)
        test(5)
        test(65533-14)  # full JPEG marker block
        test(65533-14+1)  # full block plus one byte
        test(ImageFile.MAXBLOCK)  # full buffer block
        test(ImageFile.MAXBLOCK+1)  # full buffer block plus one byte
        test(ImageFile.MAXBLOCK*4+3)  # large block 
Example #3
Source File: videos_to_tfrecords.py    From Gun-Detector with Apache License 2.0 6 votes vote down vote up
def JpegString(image, jpeg_quality=90):
  """Returns given PIL.Image instance as jpeg string.

  Args:
    image: A PIL image.
    jpeg_quality: The image quality, on a scale from 1 (worst) to 95 (best).

  Returns:
    a jpeg_string.
  """
  # This fix to PIL makes sure that we don't get an error when saving large
  # jpeg files. This is a workaround for a bug in PIL. The value should be
  # substantially larger than the size of the image being saved.
  ImageFile.MAXBLOCK = 640 * 512 * 64

  output_jpeg = StringIO()
  image.save(output_jpeg, 'jpeg', quality=jpeg_quality, optimize=True)
  return output_jpeg.getvalue() 
Example #4
Source File: videos_to_tfrecords.py    From models with Apache License 2.0 6 votes vote down vote up
def JpegString(image, jpeg_quality=90):
  """Returns given PIL.Image instance as jpeg string.

  Args:
    image: A PIL image.
    jpeg_quality: The image quality, on a scale from 1 (worst) to 95 (best).

  Returns:
    a jpeg_string.
  """
  # This fix to PIL makes sure that we don't get an error when saving large
  # jpeg files. This is a workaround for a bug in PIL. The value should be
  # substantially larger than the size of the image being saved.
  ImageFile.MAXBLOCK = 640 * 512 * 64

  output_jpeg = StringIO()
  image.save(output_jpeg, 'jpeg', quality=jpeg_quality, optimize=True)
  return output_jpeg.getvalue() 
Example #5
Source File: videos_to_tfrecords.py    From g-tensorflow-models with Apache License 2.0 6 votes vote down vote up
def JpegString(image, jpeg_quality=90):
  """Returns given PIL.Image instance as jpeg string.

  Args:
    image: A PIL image.
    jpeg_quality: The image quality, on a scale from 1 (worst) to 95 (best).

  Returns:
    a jpeg_string.
  """
  # This fix to PIL makes sure that we don't get an error when saving large
  # jpeg files. This is a workaround for a bug in PIL. The value should be
  # substantially larger than the size of the image being saved.
  ImageFile.MAXBLOCK = 640 * 512 * 64

  output_jpeg = StringIO()
  image.save(output_jpeg, 'jpeg', quality=jpeg_quality, optimize=True)
  return output_jpeg.getvalue() 
Example #6
Source File: videos_to_tfrecords.py    From object_detection_with_tensorflow with MIT License 6 votes vote down vote up
def JpegString(image, jpeg_quality=90):
  """Returns given PIL.Image instance as jpeg string.

  Args:
    image: A PIL image.
    jpeg_quality: The image quality, on a scale from 1 (worst) to 95 (best).

  Returns:
    a jpeg_string.
  """
  # This fix to PIL makes sure that we don't get an error when saving large
  # jpeg files. This is a workaround for a bug in PIL. The value should be
  # substantially larger than the size of the image being saved.
  ImageFile.MAXBLOCK = 640 * 512 * 64

  output_jpeg = StringIO()
  image.save(output_jpeg, 'jpeg', quality=jpeg_quality, optimize=True)
  return output_jpeg.getvalue() 
Example #7
Source File: image.py    From tensor2robot with Apache License 2.0 6 votes vote down vote up
def jpeg_string(image, jpeg_quality = 90):
  """Returns given PIL.Image instance as jpeg string.

  Args:
    image: A PIL image.
    jpeg_quality: The image quality, on a scale from 1 (worst) to 95 (best).

  Returns:
    a jpeg_string.
  """
  # This fix to PIL makes sure that we don't get an error when saving large
  # jpeg files. This is a workaround for a bug in PIL. The value should be
  # substantially larger than the size of the image being saved.
  ImageFile.MAXBLOCK = 640 * 512 * 64

  output_jpeg = io.BytesIO()
  image.save(output_jpeg, 'jpeg', quality=jpeg_quality, optimize=True)
  return output_jpeg.getvalue() 
Example #8
Source File: videos_to_tfrecords.py    From yolo_v2 with Apache License 2.0 6 votes vote down vote up
def JpegString(image, jpeg_quality=90):
  """Returns given PIL.Image instance as jpeg string.

  Args:
    image: A PIL image.
    jpeg_quality: The image quality, on a scale from 1 (worst) to 95 (best).

  Returns:
    a jpeg_string.
  """
  # This fix to PIL makes sure that we don't get an error when saving large
  # jpeg files. This is a workaround for a bug in PIL. The value should be
  # substantially larger than the size of the image being saved.
  ImageFile.MAXBLOCK = 640 * 512 * 64

  output_jpeg = StringIO()
  image.save(output_jpeg, 'jpeg', quality=jpeg_quality, optimize=True)
  return output_jpeg.getvalue() 
Example #9
Source File: img_info.py    From optimize-images with MIT License 5 votes vote down vote up
def is_big_png_photo(src_path: str) -> bool:
    """Try to determine if a given image if a big photo in PNG format

    Expects a path to a PNG image file. Returns True if the image is a PNG
    with an area bigger than MIN_BIG_IMG_AREA pixels that when resized to 1600
    pixels (wide or high) converts to a JPEG bigger than MIN_BIG_IMG_SIZE.
    Returns False otherwise.

    Inspired by an idea first presented by Stephen Arthur
    (https://engineeringblog.yelp.com/2017/06/making-photos-smaller.html)
    """
    img = Image.open(src_path)
    orig_format = img.format
    orig_mode = img.mode

    if orig_format != 'PNG' or orig_mode in ['P', 'L', 'LA']:
        return False

    w, h = img.size
    if (w * h) >= MIN_BIG_IMG_AREA:
        unique_colors = {img.getpixel((x, y)) for x in range(w) for y in range(h)}
        if len(unique_colors) > 2 ** 16:
            img = img.convert("RGB")
            if w > h:
                img, status = downsize_img(img, 1600, 0)
            else:
                img, status = downsize_img(img, 0, 1600)

            tempfile = BytesIO()
            try:
                img.save(tempfile, quality=80, format="JPEG")
            except IOError:
                ImageFile.MAXBLOCK = img.size[0] * img.size[1]
                img.save(tempfile, quality=80, format="JPEG")

            final_size = tempfile.getbuffer().nbytes
            return final_size > MIN_BIG_IMG_SIZE

    return False 
Example #10
Source File: spectrometer.py    From RPiSpectrometer with MIT License 5 votes vote down vote up
def save_image_with_overlay(im, name):
    output_filename = name + "_out.jpg"
    ImageFile.MAXBLOCK = 2 ** 20
    im.save(output_filename, "JPEG", quality=80, optimize=True, progressive=True) 
Example #11
Source File: screenshot.py    From backdoorme with MIT License 5 votes vote down vote up
def pil_save(filename, pixels, width, height):
	from PIL import Image, ImageFile
	buffer_len = (width * 3 + 3) & -4
	img = Image.frombuffer('RGB', (width, height), pixels, 'raw', 'BGR', buffer_len, 1)
	ImageFile.MAXBLOCK = width * height
	img=img.transpose(Image.FLIP_TOP_BOTTOM)
	img.save(filename, quality=95, optimize=True, progressive=True)
	logging.info('Screenshot saved to %s'%filename) 
Example #12
Source File: mouselogger.py    From backdoorme with MIT License 5 votes vote down vote up
def pil_save(filename, pixels, width, height):
	from PIL import Image, ImageFile
	buffer_len = (width * 3 + 3) & -4
	img = Image.frombuffer('RGB', (width, height), pixels, 'raw', 'BGR', buffer_len, 1)
	ImageFile.MAXBLOCK = width * height
	img=img.transpose(Image.FLIP_TOP_BOTTOM)
	img.save(filename, quality=95, optimize=True, progressive=True) 
Example #13
Source File: webcamsnap.py    From backdoorme with MIT License 5 votes vote down vote up
def pil_save(filename, pixels, width, height):
	from PIL import Image, ImageFile
	buffer_len = (width * 3 + 3) & -4
	img = Image.frombuffer('RGB', (width, height), pixels, 'raw', 'BGR', buffer_len, 1)
	ImageFile.MAXBLOCK = width * height
	img=img.transpose(Image.FLIP_TOP_BOTTOM)
	img.save(filename, quality=95, optimize=True, progressive=True)
	logging.info('webcam snap saved to %s'%filename) 
Example #14
Source File: drawTriangles.py    From Delaunay_Triangulation with ISC License 5 votes vote down vote up
def drawImageColoredVoronoi(polygons, filename, origIm, multiplier):
    start = time.clock()
    (sizeX, sizeY) = origIm.size
    im = Image.new('RGB', (sizeX*multiplier, sizeY*multiplier))
    draw = ImageDraw.Draw(im)
    for pol in polygons:
        if len(pol) < 2:
            continue
        (r,g,b) = getPolygonColor(pol, origIm)
        newPol = map(lambda x: (x[0] * multiplier, x[1]*multiplier), pol)
        draw.polygon(newPol, fill=(r,g,b,255))
    im = brightenImage(im, 3.0)
    ImageFile.MAXBLOCK = im.size[0] * im.size[1]
    im.save(filename, "JPEG", quality=100, optimize=True, progressive=True)
    print "Voronoi zeichnen: %.2fs" % (time.clock()-start) 
Example #15
Source File: drawTriangles.py    From Delaunay_Triangulation with ISC License 5 votes vote down vote up
def drawImageColoredTriangles(triangles, filename, origIm, multiplier):
    (sizeX, sizeY) = origIm.size
    im = Image.new('RGB', (sizeX*multiplier, sizeY*multiplier))
    draw = ImageDraw.Draw(im)
    start = time.clock()
    for t in triangles:
        (r,g,b) = getTriangleColor(t, origIm)
        p0 = tuple(map(lambda x:x*multiplier, t[0]))
        p1 = tuple(map(lambda x:x*multiplier, t[1]))
        p2 = tuple(map(lambda x:x*multiplier, t[2]))
        drawT = (p0, p1, p2)
        draw.polygon(drawT, fill=(r,g,b,255))
    im = brightenImage(im, 3.0)
    ImageFile.MAXBLOCK = im.size[0] * im.size[1]
    im.save(filename, "JPEG", quality=100, optimize=True, progressive=True) 
Example #16
Source File: mouselogger.py    From NoobSec-Toolkit with GNU General Public License v2.0 5 votes vote down vote up
def pil_save(filename, pixels, width, height):
	from PIL import Image, ImageFile
	buffer_len = (width * 3 + 3) & -4
	img = Image.frombuffer('RGB', (width, height), pixels, 'raw', 'BGR', buffer_len, 1)
	ImageFile.MAXBLOCK = width * height
	img=img.transpose(Image.FLIP_TOP_BOTTOM)
	img.save(filename, quality=95, optimize=True, progressive=True) 
Example #17
Source File: webcamsnap.py    From NoobSec-Toolkit with GNU General Public License v2.0 5 votes vote down vote up
def pil_save(filename, pixels, width, height):
	from PIL import Image, ImageFile
	buffer_len = (width * 3 + 3) & -4
	img = Image.frombuffer('RGB', (width, height), pixels, 'raw', 'BGR', buffer_len, 1)
	ImageFile.MAXBLOCK = width * height
	img=img.transpose(Image.FLIP_TOP_BOTTOM)
	img.save(filename, quality=95, optimize=True, progressive=True)
	logging.info('webcam snap saved to %s'%filename) 
Example #18
Source File: screenshot.py    From NoobSec-Toolkit with GNU General Public License v2.0 5 votes vote down vote up
def pil_save(filename, pixels, width, height):
	from PIL import Image, ImageFile
	buffer_len = (width * 3 + 3) & -4
	img = Image.frombuffer('RGB', (width, height), pixels, 'raw', 'BGR', buffer_len, 1)
	ImageFile.MAXBLOCK = width * height
	img=img.transpose(Image.FLIP_TOP_BOTTOM)
	img.save(filename, quality=95, optimize=True, progressive=True)
	logging.info('Screenshot saved to %s'%filename) 
Example #19
Source File: mouselogger.py    From NoobSec-Toolkit with GNU General Public License v2.0 5 votes vote down vote up
def pil_save(filename, pixels, width, height):
	from PIL import Image, ImageFile
	buffer_len = (width * 3 + 3) & -4
	img = Image.frombuffer('RGB', (width, height), pixels, 'raw', 'BGR', buffer_len, 1)
	ImageFile.MAXBLOCK = width * height
	img=img.transpose(Image.FLIP_TOP_BOTTOM)
	img.save(filename, quality=95, optimize=True, progressive=True) 
Example #20
Source File: webcamsnap.py    From NoobSec-Toolkit with GNU General Public License v2.0 5 votes vote down vote up
def pil_save(filename, pixels, width, height):
	from PIL import Image, ImageFile
	buffer_len = (width * 3 + 3) & -4
	img = Image.frombuffer('RGB', (width, height), pixels, 'raw', 'BGR', buffer_len, 1)
	ImageFile.MAXBLOCK = width * height
	img=img.transpose(Image.FLIP_TOP_BOTTOM)
	img.save(filename, quality=95, optimize=True, progressive=True)
	logging.info('webcam snap saved to %s'%filename) 
Example #21
Source File: test_file_jpeg.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_progressive_large_buffer(self):
        f = self.tempfile('temp.jpg')
        # this requires ~ 1.5x Image.MAXBLOCK
        im = Image.new("RGB", (4096, 4096), 0xff3333)
        im.save(f, format="JPEG", progressive=True) 
Example #22
Source File: test_file_jpeg.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_optimize_large_buffer(self):
        # https://github.com/python-pillow/Pillow/issues/148
        f = self.tempfile('temp.jpg')
        # this requires ~ 1.5x Image.MAXBLOCK
        im = Image.new("RGB", (4096, 4096), 0xff3333)
        im.save(f, format="JPEG", optimize=True) 
Example #23
Source File: test_file_jpeg.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_large_icc_meta(self):
        # https://github.com/python-pillow/Pillow/issues/148
        # Sometimes the meta data on the icc_profile block is bigger than
        # Image.MAXBLOCK or the image size.
        im = Image.open('Tests/images/icc_profile_big.jpg')
        f = self.tempfile("temp.jpg")
        icc_profile = im.info["icc_profile"]
        # Should not raise IOError for image with icc larger than image size.
        im.save(f, format='JPEG', progressive=True, quality=95,
                icc_profile=icc_profile, optimize=True) 
Example #24
Source File: test_file_pcx.py    From python3_ios with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _test_buffer_overflow(self, im, size=1024):
        _last = ImageFile.MAXBLOCK
        ImageFile.MAXBLOCK = size
        try:
            self._roundtrip(im)
        finally:
            ImageFile.MAXBLOCK = _last