2014-02-25 208 views
0

不能附加圖像,但它不填充新的4像素塊。它交替並在兩個通道中留下黑色像素。如何使用圖像模塊在Python中放大圖像?

def scale_up(img): 
""" 
(Image object) -> Image object 
Returns a copy of img that is blown up by a factor of 2. The new 
image is twice as wide and twice as high. Each pixel in the original 
image is mapped to the corresponding 4 pixels in new image. 
""" 
w = img.width() 
h = img.height() 
scale_up_img = image.new_image(w * 2, h * 2) 

# loop over every (x,y) pair 
for x in range(w): 
    for y in range(h): 
     # get rgb values 
     r, g, b = img.get_rgb(x, y) 
     scale_up_img.set_rgb(x * 2 + 1, y * 2 + 1, r, g, b) 
     scale_up_img.set_rgb(x * 2, y * 2, r, g, b) 
return scale_up_img # return img object 

http://i.stack.imgur.com/8yVMK.jpg

回答

0

由於每個輸出像素是由輸入像素的4份,則需要4個語句來設置。

scale_up_img.set_rgb(x * 2 + 1, y * 2 + 1, r, g, b) 
    scale_up_img.set_rgb(x * 2 + 1, y * 2, r, g, b) 
    scale_up_img.set_rgb(x * 2, y * 2 + 1, r, g, b) 
    scale_up_img.set_rgb(x * 2, y * 2, r, g, b)