python path setting code question

Asked 2 years ago, Updated 2 years ago, 21 views

# python study
import numpy as np
import cv2

#img = cv2.imread('bongdu.jpg')
img_nam = "C:\Users\jhjoo\Desktop\bongdu.jpg"
img_name = img_nam.replace("\", "/")
img = cv2.imread(img_name)
[height, width, channel] = img.shape
#img_gray = np.zeros((height,width), np.uint8)
img_gray = img.copy()

#print(height, width, channel)

for y in range(np.round(height // 4), np.round(height // 4 * 3)):
    for x in range(np.round(width // 4), np.round(width // 4 * 3)):
        r = img.item(y, x, 2)
        g = img.item(y, x, 1)
        b = img.item(y, x, 0)

        gray = (int(r) + int(g) + int(b)) / 3

        if gray > 255:
           gray = 255

        #img_gray.itemset(y, x, gray)
        img_gray[y][x] = gray

cv2.imshow('bongdu',img)
cv2.imshow('bongdu_gray',img_gray)
cv2.waitKey(0)
cv2.destroyAllWindows()

I'm practicing by writing the code like above

#img = cv2.imread('bongdu.jpg')
img_nam = "C:\Users\jhjoo\Desktop\bongdu.jpg"
img_name = img_nam.replace("\", "/")
img = cv2.imread(img_name)

In this part, we have confirmed that absolute paths cannot be executed if they are set to \, and only if they are set to /. But I'm lazy to change the path to / one by one because I usually copy it from the explorer I want to insert a code that automatically changes \ to / I don't think the replace function is applied.

I wonder what I should do.

On this site, \ appears in the shape of \, but in Python code, it looks like money.

python

2022-09-22 14:40

1 Answers

img_nam = r"C:\Users\jhjoo\Desktop\bongdu.jpg"

Use it like this.


2022-09-22 14:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.