简体   繁体   中英

Moving and writing pixels of an image -Python-

Ok, I got a newbie question in python. So I've a list with the coordinate of some pixels I'm interested for. I'd like to move those pixels into another image or saving those pixels in a new image. I'm working with cv2 and matplotlib so far, I even thought of saving the coordinates with RGB values and writing it onto another image, but I don't got any idea to start with. Sorry if its a kind of dumb question, thanks

You can do this just like in numpy array. For example, you want to take first 10x10 part of the RGB image with all three channels. Basically;

new_image = np.zeros([10, 10, 3], dtype=np.uint8)
new_image[:, :, :] = old_image[:10, :10, :]
img_src = cv2.imread('someimg.jpg')
h, w, c = img_src.shape

# an empty black image for saving pixels
img_dest = np.zeros((h, w, c), dtype=np.uint8)


for ixy in xy_list:
    x, y = xy_list[ixy]

    # copy pixels at given locations
    img_dest[x, y, :] = img_src[x, y, :]


cv2.imwrite('output.jpg', img_dest)

Could this solve your problem?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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