第1回 課題解答

3 : 画像の読み込み

from PIL import Image

def load_image(image_path):
  try:
    img = Image.open(image_path)
    print(f"Successfully loaded image: {image_path}")
    print(f"Image size: {img.size}")
    print(f"Image mode: {img.mode}")
    return img
  except Exception as e:
    print(f"Error loading image: {e}")
    return None

if __name__ == "__main__":
  image_path = "source.png"
  img = load_image(image_path)
変換用の元画像

4 : 拡大

from PIL import Image

def load_image(image_path, scale_factor=2):
  try:
    img = Image.open(image_path)

    new_width = int(img.size[0] * scale_factor)
    new_height = int(img.size[1] * scale_factor)

    img_resized = img.resize((new_width, new_height), Image.Resampling.LANCZOS)
    print(f"Successfully loaded and resized image: {image_path}")
    print(f"Original size: {img.size}")
    print(f"New size: {img_resized.size}")
    print(f"Image mode: {img_resized.mode}")

    img_resized.save("magnified_" + image_path)
    return img_resized
  except Exception as e:
    print(f"Error loading image: {e}")
    return None

if __name__ == "__main__":
  image_path = "source.png"
  img = load_image(image_path)
できる画像

5 : 縮小

from PIL import Image

def load_image(image_path, scale_factor=0.5):
  try:
    img = Image.open(image_path)

    new_width = int(img.size[0] * scale_factor)
    new_height = int(img.size[1] * scale_factor)

    img_resized = img.resize((new_width, new_height), Image.Resampling.LANCZOS)
    print(f"Successfully loaded and resized image: {image_path}")
    print(f"Original size: {img.size}")
    print(f"New size: {img_resized.size}")
    print(f"Image mode: {img_resized.mode}")

    img_resized.save("reduced_" + image_path)
    return img_resized
  except Exception as e:
    print(f"Error loading image: {e}")
    return None

if __name__ == "__main__":
  image_path = "source.png"
  img = load_image(image_path)
できる画像

6 : 最近傍補間 (Nearest neighbor)

from PIL import Image


def load_image(image_path, scale_factor=2):
  try:
    img = Image.open(image_path)

    new_width = int(img.size[0] * scale_factor)
    new_height = int(img.size[1] * scale_factor)

    img_resized = img.resize((new_width, new_height), Image.Resampling.NEAREST)
    print(f"Successfully loaded and resized image: {image_path}")
    print(f"Original size: {img.size}")
    print(f"New size: {img_resized.size}")
    print(f"Image mode: {img_resized.mode}")

    img_resized.save("magnified_nearest_" + image_path)
    return img_resized
  except Exception as e:
    print(f"Error loading image: {e}")
    return None


if __name__ == "__main__":
  image_path = "source.png"
  img = load_image(image_path)
できる画像

7 : 双一次補間 (Bilinear)

from PIL import Image


def load_image(image_path, scale_factor=2):
  try:
    img = Image.open(image_path)

    new_width = int(img.size[0] * scale_factor)
    new_height = int(img.size[1] * scale_factor)

    img_resized = img.resize((new_width, new_height),
                             Image.Resampling.BILINEAR)
    print(f"Successfully loaded and resized image: {image_path}")
    print(f"Original size: {img.size}")
    print(f"New size: {img_resized.size}")
    print(f"Image mode: {img_resized.mode}")

    img_resized.save("magnified_bilinear_" + image_path)
    return img_resized
  except Exception as e:
    print(f"Error loading image: {e}")
    return None


if __name__ == "__main__":
  image_path = "source.png"
  img = load_image(image_path)
できる画像