Unit 2.2 Data Compression, Images HACKS
Hacks for the 2.2 Data Compression and Images lecture
from PIL import Image
filename = "images/SMILE.png"
filename2 = "images/house.png"
filename3 = "images/200px-ScratchCat-Small.png"
filename4 = "images/2014FHDCover.png"
with Image.open(filename) as img:
img.load()
with Image.open(filename2) as img2:
img2.load()
with Image.open(filename3) as img3:
img3.load()
with Image.open(filename4) as img4:
img4.load()
type(img)
isinstance(img, Image.Image)
img.show()
print(img.size)
# Cropping
cropped_img = img.crop((100, 250, 290, 300))
type(cropped_img)
print("Cropped Image")
print(cropped_img.size)
cropped_img.show()
# Transposing/Flipping
print("ORIGINAL")
img2.show()
print("180 DEGREE FLIP")
flipped_img = img2.transpose(Image.FLIP_TOP_BOTTOM)
flipped_img.show()
print("90 DEGREE FLIP")
flipped_img2 = img2.transpose(Image.FLIP_LEFT_RIGHT)
flipped_img2.show()
# Rotating
print("ORIGINAL")
img3.show()
print("ROTATED 45 DEGREES")
rotated_img = img3.rotate(45, expand = True)
rotated_img.show()
# Color Grading
print("ORIGINAL")
img4.show()
red, green, blue = img4.split()
red.mode
zeroed_band = red.point(lambda _: 0)
red_merge = Image.merge(
"RGB", (red, zeroed_band, zeroed_band)
)
green_merge = Image.merge(
"RGB", (zeroed_band, green, zeroed_band)
)
blue_merge = Image.merge(
"RGB", (zeroed_band, zeroed_band, blue)
)
print("RED TINT")
red_merge.show()
print("GREEN TINT")
green_merge.show()
print("BLUE TINT")
blue_merge.show()