We can save the data from a BytesIO object to the file system using the pathlib library.
1 2 3 4 5 6 7 8 910111213141516171819202122232425
fromioimportBytesIOfrompathlibimportPathfromPILimportImage# Path to an existing imagesource_path=Path("myfiles","images","test.jpg")# The thumbnail binary stream will get saved to this later.thumbnail_binary_stream=BytesIO()# Open imagewithImage.open(source_path)asimg:# Create thumbnailimg.thumbnail((500,500))# Save the new thumbnail to a BytesIO object.img.save(thumbnail_binary_stream,format="JPEG")# The thumbnail image will get saved to this path.thumbnail_path=Path("myfiles","images","test_thumbnail.jpg")# Save the thumbnail image using the thumbnail binary stream's viewthumbnail_path.write_bytes(thumbnail_binary_stream.getbuffer())