简体   繁体   English

如何使用图像模块在Python中按比例放大图像?

[英]How to scale up an image in Python with an image module?

Cannot attach an image, but it's not filling in a new 4 block of pixels. 无法附加图像,但未填充新的4像素块。 It alternates and leaves black pixels in 2 of the channels. 它交替并在两个通道中留下黑色像素。

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 http://i.stack.imgur.com/8yVMK.jpg

Since each output pixel is made up of 4 copies of an input pixel, you need 4 statements to set them. 由于每个输出像素由一个输入像素的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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM