def resize_image(entry, resized, keyname, target_name, new_size, exif_tags, workdir, quality, filter): """ Store a resized version of an image and return its pathname. Arguments: proc_state -- the processing state for the image to resize resized -- an image from Image.open() of the original image being resized keyname -- Under what key to save in the db. target_name -- public file path for the new resized image exif_tags -- EXIF data for the original image workdir -- directory path for storing converted image files new_size -- 2-tuple size for the resized image quality -- level of compression used when resizing images filter -- One of BICUBIC, BILINEAR, NEAREST, ANTIALIAS """ resized = exif_fix_image_orientation(resized, exif_tags) # Fix orientation try: resize_filter = PIL_FILTERS[filter.upper()] except KeyError: raise Exception('Filter "{0}" not found, choose one of {1}'.format( unicode(filter), u', '.join(PIL_FILTERS.keys()))) resized.thumbnail(new_size, resize_filter) # Copy the new file to the conversion subdir, then remotely. tmp_resized_filename = os.path.join(workdir, target_name) with file(tmp_resized_filename, 'w') as resized_file: resized.save(resized_file, quality=quality) store_public(entry, keyname, tmp_resized_filename, target_name)
def resize_image(proc_state, resized, keyname, target_name, new_size, exif_tags, workdir): """ Store a resized version of an image and return its pathname. Arguments: proc_state -- the processing state for the image to resize resized -- an image from Image.open() of the original image being resized keyname -- Under what key to save in the db. target_name -- public file path for the new resized image exif_tags -- EXIF data for the original image workdir -- directory path for storing converted image files new_size -- 2-tuple size for the resized image """ config = mgg.global_config['media_type:mediagoblin.media_types.image'] resized = exif_fix_image_orientation(resized, exif_tags) # Fix orientation filter_config = config['resize_filter'] try: resize_filter = PIL_FILTERS[filter_config.upper()] except KeyError: raise Exception('Filter "{0}" not found, choose one of {1}'.format( unicode(filter_config), u', '.join(PIL_FILTERS.keys()))) resized.thumbnail(new_size, resize_filter) # Copy the new file to the conversion subdir, then remotely. tmp_resized_filename = os.path.join(workdir, target_name) with file(tmp_resized_filename, 'w') as resized_file: resized.save(resized_file, quality=config['quality']) proc_state.store_public(keyname, tmp_resized_filename, target_name)
def resize_image(entry, filename, new_path, exif_tags, workdir, new_size, size_limits=(0, 0)): """ Store a resized version of an image and return its pathname. Arguments: entry -- the entry for the image to resize filename -- the filename of the original image being resized new_path -- public file path for the new resized image exif_tags -- EXIF data for the original image workdir -- directory path for storing converted image files new_size -- 2-tuple size for the resized image """ try: resized = Image.open(filename) except IOError: raise BadMediaFail() resized = exif_fix_image_orientation(resized, exif_tags) # Fix orientation resized.thumbnail(new_size, Image.ANTIALIAS) # Copy the new file to the conversion subdir, then remotely. tmp_resized_filename = os.path.join(workdir, new_path[-1]) with file(tmp_resized_filename, 'w') as resized_file: resized.save(resized_file) mgg.public_store.copy_local_to_storage(tmp_resized_filename, new_path)
def test_exif_image_orientation(): """ Test image reorientation based on EXIF data """ result = extract_exif(GOOD_JPG) image = exif_fix_image_orientation(Image.open(GOOD_JPG), result) # Are the dimensions correct? assert image.size == (428, 640) # If this pixel looks right, the rest of the image probably will too. assert_in(image.getdata()[10000], ((41, 28, 11), (43, 27, 11)))
def test_exif_image_orientation(): ''' Test image reorientation based on EXIF data ''' result = extract_exif(GOOD_JPG) image = exif_fix_image_orientation(Image.open(GOOD_JPG), result) # Are the dimensions correct? assert image.size in ((428, 640), (640, 428)) # If this pixel looks right, the rest of the image probably will too. # It seems different values are being seen on different platforms/systems # as of ccca39f1 it seems we're adding to the list those which are seen. assert_in(image.getdata()[10000], ((37, 23, 14), (41, 28, 11), (43, 27, 11)))
def test_exif_image_orientation(): ''' Test image reorientation based on EXIF data ''' result = extract_exif(GOOD_JPG) image = exif_fix_image_orientation( Image.open(GOOD_JPG), result) # Are the dimensions correct? assert image.size in ((428, 640), (640, 428)) # If this pixel looks right, the rest of the image probably will too. assert_in(image.getdata()[10000], ((41, 28, 11), (43, 27, 11)) )
def test_exif_image_orientation(): ''' Test image reorientation based on EXIF data ''' result = extract_exif(GOOD_JPG) image = exif_fix_image_orientation( Image.open(GOOD_JPG), result) # Are the dimensions correct? assert image.size in ((428, 640), (640, 428)) # If this pixel looks right, the rest of the image probably will too. # It seems different values are being seen on different platforms/systems # as of ccca39f1 it seems we're adding to the list those which are seen. assert_in(image.getdata()[10000], ((37, 23, 14), (41, 28, 11), (43, 27, 11)) )