lossy image

oskar-smethurst-B1GtwanCbiw-unsplash

  • Jpeg images are very lossy, this is an example of a lossy image
  • In lossy compression, some of the original image data is permanently discarded, resulting in a reduction in quality. The amount of data that is discarded depends on the degree of compression used. Higher levels of compression lead to greater loss of data and lower image quality.

Lossless image

bow-wow-gourmet-dog-treats-are-healthy-natural-low-4

  • PNGS are lossless so if you scale the image it does not lose pixels
  • Lossless compression algorithms typically work by finding patterns in the image data and representing those patterns in a more efficient way. This can result in a significant reduction in file size without any loss of information.

Project Addition

If your project has images in it, try to implement an image change that has a purpose. (Ex. An item that has been sold out could become gray scale)
from IPython.display import HTML, display
from pathlib import Path  
from PIL import Image as pilImage 
from io import BytesIO
import base64
import numpy as np
def image_to_base64(img, format):
    with BytesIO() as buffer:
        img.save(buffer, format)
        return base64.b64encode(buffer.getvalue()).decode()

def scale_image(img):
    baseWidth = 320
    scalePercent = (baseWidth/float(img.size[0]))
    scaleHeight = int((float(img.size[1])*float(scalePercent)))
    scale = (baseWidth, scaleHeight)
    return img.resize(scale)

def image_data(path=Path("../images/"), images=None):  
    if images is None:  
        images = [
            {'source': "Internet", 'label': "Green Square", 'file': "Smile.png"},
            {'source': "Peter Carolin", 'label': "Clouds Impression", 'file': "luka.jpg"},
        ]
    for image in images:
        
        image['filename'] = path / image['file']  
    return images
def image_management(image):  # path of static images is defaulted        
    # Image open return PIL image object
    img = pilImage.open(image['filename'])
    
    # Python Image Library operations
    image['format'] = img.format
    image['mode'] = img.mode
    image['size'] = img.size
    # Scale the Image
    img = scale_image(img)
    image['pil'] = img
    image['scaled_size'] = img.size
    # Scaled HTML
    image['html'] = '<img src="data:image/png;base64,%s">' % image_to_base64(image['pil'], image['format'])
    

def image_management_add_html_grey(image):
    img = image['pil']
    format = image['format']
    
    img_data = img.getdata()  
    image['data'] = np.array(img_data)
    image['gray_data'] = [] 

    for pixel in image['data']:
        
        average = (pixel[0] + pixel[1] + pixel[2]) // 3 
        average2 = 255 - (pixel[0]) 
        if len(pixel) > 3:
            image['gray_data'].append((average2, average, average, pixel[3])) 
        else:
            image['gray_data'].append((average2, average, average))

        
    img.putdata(image['gray_data'])
    image['html_grey'] = '<img src="data:image/png;base64,%s">' % image_to_base64(img, format)



if __name__ == "__main__":

    images = image_data()
    
 
    for image in images:
        image_management(image)
        print("-- original image --")
        display(HTML(image['html'])) 
        
        print("--- grey image ----")
        image_management_add_html_grey(image)
        display(HTML(image['html_grey'])) 
    print()
-- original image --
--- grey image ----
-- original image --
--- grey image ----

from IPython.display import HTML, display
from pathlib import Path  
from PIL import Image as pilImage 
from io import BytesIO
import base64
import numpy as np
def image_to_base64(img, format):
    with BytesIO() as buffer:
        img.save(buffer, format)
        return base64.b64encode(buffer.getvalue()).decode()

def scale_image(img):
    baseWidth = 320
    scalePercent = (baseWidth/float(img.size[0]))
    scaleHeight = int((float(img.size[1])*float(scalePercent)))
    scale = (baseWidth, scaleHeight)
    return img.resize(scale)

def image_data(path=Path("../images/"), images=None):  
    if images is None:  
        images = [
            {'source': "Internet", 'label': "Green Square", 'file': "Smile.png"},
            {'source': "Peter Carolin", 'label': "Clouds Impression", 'file': "luka.jpg"},
        ]
    for image in images:
        
        image['filename'] = path / image['file']  
    return images
def image_management(image):  # path of static images is defaulted        
    # Image open return PIL image object
    img = pilImage.open(image['filename'])
    
    # Python Image Library operations
    image['format'] = img.format
    image['mode'] = img.mode
    image['size'] = img.size
    # Scale the Image
    img = scale_image(img)
    image['pil'] = img
    image['scaled_size'] = img.size
    # Scaled HTML
    image['html'] = '<img src="data:image/png;base64,%s">' % image_to_base64(image['pil'], image['format'])
    

def image_management_add_html_grey(image):
    img = image['pil']
    format = image['format']
    
    img_data = img.getdata()  
    image['data'] = np.array(img_data)
    image['gray_data'] = [] 

    for pixel in image['data']:
        
        average = (pixel[0] + pixel[1] + pixel[2]) // 3 
        average2 = 255 - (pixel[1]) 
        if len(pixel) > 3:
            image['gray_data'].append((average, average2, average, pixel[3])) 
        else:
            image['gray_data'].append((average, average2, average))

        
    img.putdata(image['gray_data'])
    image['html_grey'] = '<img src="data:image/png;base64,%s">' % image_to_base64(img, format)



if __name__ == "__main__":

    images = image_data()
    
 
    for image in images:
        image_management(image)
        print("-- original image --")
        display(HTML(image['html'])) 
        
        print("--- grey image ----")
        image_management_add_html_grey(image)
        display(HTML(image['html_grey'])) 
    print()
-- original image --
--- grey image ----
-- original image --
--- grey image ----

from IPython.display import HTML, display
from pathlib import Path  
from PIL import Image as pilImage 
from io import BytesIO
import base64
import numpy as np
def image_to_base64(img, format):
    with BytesIO() as buffer:
        img.save(buffer, format)
        return base64.b64encode(buffer.getvalue()).decode()

def scale_image(img):
    baseWidth = 320
    scalePercent = (baseWidth/float(img.size[0]))
    scaleHeight = int((float(img.size[1])*float(scalePercent)))
    scale = (baseWidth, scaleHeight)
    return img.resize(scale)

def image_data(path=Path("../images/"), images=None):  
    if images is None:  
        images = [
            {'source': "Internet", 'label': "Green Square", 'file': "Smile.png"},
            {'source': "Peter Carolin", 'label': "Clouds Impression", 'file': "luka.jpg"},
        ]
    for image in images:
        
        image['filename'] = path / image['file']  
    return images
def image_management(image):  # path of static images is defaulted        
    # Image open return PIL image object
    img = pilImage.open(image['filename'])
    
    # Python Image Library operations
    image['format'] = img.format
    image['mode'] = img.mode
    image['size'] = img.size
    # Scale the Image
    img = scale_image(img)
    image['pil'] = img
    image['scaled_size'] = img.size
    # Scaled HTML
    image['html'] = '<img src="data:image/png;base64,%s">' % image_to_base64(image['pil'], image['format'])
    

def image_management_add_html_grey(image):
    img = image['pil']
    format = image['format']
    
    img_data = img.getdata()  
    image['data'] = np.array(img_data)
    image['gray_data'] = [] 

    for pixel in image['data']:
        
        average = (pixel[0] + pixel[1] + pixel[2]) // 3 
        average2 = 254 - (pixel[2]) 
        if len(pixel) > 3:
            image['gray_data'].append((average, average, average2, pixel[3])) 
        else:
            image['gray_data'].append((average, average, average2))

        
    img.putdata(image['gray_data'])
    image['html_grey'] = '<img src="data:image/png;base64,%s">' % image_to_base64(img, format)



if __name__ == "__main__":

    images = image_data()
    
 
    for image in images:
        image_management(image)
        print("-- original image --")
        display(HTML(image['html'])) 
        
        print("--- grey image ----")
        image_management_add_html_grey(image)
        display(HTML(image['html_grey'])) 
    print()
-- original image --
--- grey image ----
-- original image --
--- grey image ----

from PIL import Image, ImageFilter

# Load image
image = Image.open("luka.jpg")

# Apply Gaussian blur filter
blurred_image = image.filter(ImageFilter.GaussianBlur(radius=5))

# Display blurred image
blurred_image.save("blurred_image.jpg")