Python入门 lab7

1 Calculating Index

B _ c o l u m n _ i n d e x = A _ c o l u m n _ i n d e x + r e g i o n _ w i d t h − 1 = 5 + 8 − 1 = 12 \begin {aligned}B\_column\_index&=A\_column\_index + region\_width - 1\\&=5+8-1\\&=12\end {aligned} B_column_index=A_column_index+region_width1=5+81=12

So the index of pixel (B) would be column index 12.
Since A and B are on the same row, they should have the same row index.

Therefore, the index of the upper right pixel (B) is img[3][12]


2 Mirror/Flip an Image

2.1

  • even case

    M _ i n d e x = A _ i n d e x + ( i m a g e _ w i d t h / 2 ) − 1 = 5 + ( 8 / 2 ) − 1 = 8 \begin {aligned}M\_index&=A\_index+(image\_width / 2) - 1\\&=5+(8 / 2) - 1\\&=8\end {aligned} M_index=A_index+(image_width/2)1=5+(8/2)1=8

    N _ i n d e x = A _ i n d e x + i m a g e _ w i d t h / 2 = 5 + 8 / 2 = 9 \begin {aligned}N\_index&=A\_index+image\_width / 2\\&=5+8 / 2\\&=9\end {aligned} N_index=A_index+image_width/2=5+8/2=9

  • odd case

    M _ i n d e x = A _ i n d e x + ( i m a g e _ w i d t h − 1 ) / 2 = 5 + ( 7 − 1 ) / 2 = 8 \begin {aligned}M\_index&=A\_index+(image\_width - 1) / 2\\&=5+(7 - 1) / 2\\&=8\end {aligned} M_index=A_index+(image_width1)/2=5+(71)/2=8

2.2

If the region width is denoted as w, and we start the loop at pixel A (img[i][j]), the index of the last pixel can be calculated as follows:

L a s t _ p i x e l _ i n d e x = A _ c o l u m n _ i n d e x + ( i m a g e _ w i d t h / / 2 − 1 ) = j + w / / 2 − 1 \begin {aligned}Last\_pixel\_index&=A\_column\_index+(image\_width // 2-1)\\&= j + w // 2-1\end {aligned} Last_pixel_index=A_column_index+(image_width//21)=j+w//21

Take above two cases as examples,

  • In the even case: w = 8 w=8 w=8, the for loop should end at the index 5 + 8 / / 2 − 1 = 8 5+8//2-1=8 5+8//21=8
  • In the odd case: w = 7 w=7 w=7, the for loop should end at the index 5 + 7 / / 2 − 1 = 7 5+7//2-1=7 5+7//21=7

Therefore, the for loop should end at the index j + (w // 2) - 1.


3 Cropping an image in half horizontally

code

from FudanImgLib import *


def crop_top_half(img):
    # TODO: calculate new height new_height =
    new_height = len(img) // 2

    # TODO: create the new image to store the cropped version cropped_image =
    cropped_image = create_img(new_height, width(img), (0, 0, 0))

    # TODO: loop through the new image and set each pixel
    for i in range(new_height):
        for j in range(width(img)):
            cropped_image[i][j] = img[i][j]

    return cropped_image

test cases

from FudanImgLib import *


img_even_height = [[(255, 0, 0), (255, 99, 33)], [(66, 120, 240), (198, 0, 240)],
                   [(16, 20, 38), (98, 170, 5)], [(16, 20, 38), (98, 170, 5)],
                   [(3, 0, 13), (5, 0, 0)], [(55, 0, 0), (255, 99, 33)]]

img_odd_height = [[(16, 20, 38), (98, 170, 5)], [(66, 120, 240), (198, 0, 240)],
                  [(255, 0, 0), (255, 99, 33)],[(3, 3, 3), (25, 9, 133)],
                  [(3, 0, 13), (5, 0, 0)]]

print(crop_top_half(img_even_height))
print(crop_top_half(img_odd_height))

img = load_img('Shanghai.png')
cropped_img=crop_top_half(img)
save_img(cropped_img, 'cropped.png')

result

running result:

  • every result matches the expectation

在这里插入图片描述

  • 11
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值