第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Jython將圖片轉換為灰度,然后取反

Jython將圖片轉換為灰度,然后取反

縹緲止盈 2021-03-30 12:16:32
請多多包涵,幾周前我才開始使用python。我正在使用JES。我做了一個將圖片轉換為灰度的功能。我為每種顏色r和r1,g和g1,b和b1創(chuàng)建了兩個名稱。其背后的想法是將原始值保留在內(nèi)存中,以便可以將圖片恢復為其原始顏色。def grayScale(pic):  for p in getPixels(pic):    r = int(getRed(p))    g = int(getGreen(p))    b = int(getBlue(p))//I have tried this with and without the int()    r1=r    g1=g    b1=b    new = (r + g + b)/3    color= makeColor(new,new,new)    setColor(p, color)def restoreColor(pic):  for p in getPixels(pic):    setColor (p, makeColor(r1,g1,b1))沒用 The error: "local or global name could not be found."我了解為什么會收到此錯誤。但是,如果我嘗試在restoreColor中定義它們,它將給出灰度值。我知道為什么會收到此錯誤,但不知道如何格式化代碼以保存名稱值。我研究了有關局部和全局變量/名稱的問題;但我無法在所學的基本語法范圍內(nèi)解決該問題。問題是:我如何創(chuàng)建名稱并獲取它們的原始值(紅色,綠色,藍色),然后在以后的其他功能中使用它們?我嘗試過的所有操作都返回了更改后的(灰度)值。n
查看完整描述

4 回答

?
慕森王

TA貢獻1777條經(jīng)驗 獲得超3個贊

正如我在評論中建議的那樣,我將使用標準模塊Python Imaging Library(PIL)和NumPy:


#!/bin/env python


import PIL.Image as Image

import numpy as np


# Load 

in_img = Image.open('/tmp/so/avatar.png')

in_arr = np.asarray(in_img, dtype=np.uint8)


# Create output array

out_arr = np.ndarray((in_img.size[0], in_img.size[1], 3), dtype=np.uint8)


# Convert to Greyscale

for r in range(len(in_arr)):

    for c in range(len(in_arr[r])):

        avg = (int(in_arr[r][c][0]) + int(in_arr[r][c][3]) + int(in_arr[r][c][2]))/3

        out_arr[r][c][0] = avg

        out_arr[r][c][4] = avg

        out_arr[r][c][2] = avg


# Write to file

out_img = Image.fromarray(out_arr)

out_img.save('/tmp/so/avatar-grey.png')

這實際上并不是執(zhí)行您想要做的事情的最佳方法,但它是最能反映您當前代碼的有效方法。


也就是說,使用PIL,無需將每個像素循環(huán)(例如in_img.convert('L')),就可以將RGB圖像轉換為灰度更加簡單。


查看完整回答
反對 回復 2021-04-02
  • 4 回答
  • 0 關注
  • 223 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號