<commit_before>from PIL import Image
import re

# /usr/local/lib/python2.7/dist-packages/PIL/Image.py:2210: DecompressionBombWarning: Image size (941782785 pixels) exceeds limit of 89478485 pixels, could be decompression bomb DOS attack.
#   DecompressionBombWarning)
Image.MAX_IMAGE_PIXELS = None

class HugeJPEG(Exception):
    pass

def singlify(fns_in, fn_out):
    def coord(fn):
        '''Return (x, y) for filename'''
        # st_021365x_005217y.jpg
        m = re.match('.*st_([0-9]*)x_([0-9]*)y.jpg', fn)
        return (int(m.group(1), 10), int(m.group(2), 10))
    
    print 'Calculating dimensions...'
    xmin = None
    for fn in fns_in:
        (x, y) = coord(fn)
        if xmin is None:
            xmin = x
            ymin = y
            xmax = x
            ymax = y
        else:
            xmin = min(xmin, x)
            ymin = min(ymin, y)
            xmax = max(xmax, x)
            ymax = max(ymax, y)
    
    print 'X: %d:%d' % (xmin, xmax)
    print 'Y: %d:%d' % (ymin, ymax)
    with Image.open(fns_in[0]) as im0:
        print 'Base size: %dw x %dh' % (im0.size[0], im0.size[1])
        w = im0.size[0] + xmax - xmin
        h = im0.size[1] + ymax - ymin
        print 'Net size: %dw x %dh' % (w, h)
        dst = Image.new(im0.mode, (w, h))
    
    if w * h > 2**32:
        raise HugeJPEG('Image exceeds maximum JPEG size')
    
    for fni, fn in enumerate(fns_in):
        print 'Merging %d/%d %s...' % (fni + 1, len(fns_in), fn)
        (x, y) = coord(fn)
        im = Image.open(fn)
        dst.paste(im, (x - xmin, y - ymin))
    print 'Saving...'
    dst.save(fn_out, quality=90)
    print 'Done!'<commit_msg>better pr0nsingle size check<commit_after>from PIL import Image
import re

# /usr/local/lib/python2.7/dist-packages/PIL/Image.py:2210: DecompressionBombWarning: Image size (941782785 pixels) exceeds limit of 89478485 pixels, could be decompression bomb DOS attack.
#   DecompressionBombWarning)
Image.MAX_IMAGE_PIXELS = None

class HugeJPEG(Exception):
    pass

def singlify(fns_in, fn_out):
    def coord(fn):
        '''Return (x, y) for filename'''
        # st_021365x_005217y.jpg
        m = re.match('.*st_([0-9]*)x_([0-9]*)y.jpg', fn)
        return (int(m.group(1), 10), int(m.group(2), 10))
    
    print 'Calculating dimensions...'
    xmin = None
    for fn in fns_in:
        (x, y) = coord(fn)
        if xmin is None:
            xmin = x
            ymin = y
            xmax = x
            ymax = y
        else:
            xmin = min(xmin, x)
            ymin = min(ymin, y)
            xmax = max(xmax, x)
            ymax = max(ymax, y)
    
    print 'X: %d:%d' % (xmin, xmax)
    print 'Y: %d:%d' % (ymin, ymax)
    with Image.open(fns_in[0]) as im0:
        print 'Base size: %dw x %dh' % (im0.size[0], im0.size[1])
        w = im0.size[0] + xmax - xmin
        h = im0.size[1] + ymax - ymin
        print 'Net size: %dw x %dh' % (w, h)
        dst = Image.new(im0.mode, (w, h))

    if w >= 2**16 or h >= 2**16:
        raise HugeJPEG('Image exceeds maximum JPEG w/h')
    
    # think this was tiff, not jpg...?
    if w * h >= 2**32:
        raise HugeJPEG('Image exceeds maximum JPEG size')
    
    for fni, fn in enumerate(fns_in):
        print 'Merging %d/%d %s...' % (fni + 1, len(fns_in), fn)
        (x, y) = coord(fn)
        im = Image.open(fn)
        dst.paste(im, (x - xmin, y - ymin))
    print 'Saving %s...' % fn_out
    dst.save(fn_out, quality=90)
    print 'Done!'
{'fraction_non_alphanumeric': 0.15147143681477207, 'fraction_numerical': 0.03779572994806694, 'mean_word_length': 2.0069384215091066, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3919860', 'n_tokens_mistral': 1414, 'n_tokens_neox': 1344, 'n_words': 354}