『壹』 python怎麼實現截圖.類似QQ一樣的
fromPILimportImageGrab
im=ImageGrab.grab()
#可以指定要截取圖片的坐標位置,不帶參數默認全屏截圖
im.save(addr,'jpeg')
#第一個參數是保存路徑,第二個參數是圖片格式
需要先下載一個PIL圖形庫
『貳』 Python程序抓圖 怎麼用程序實現截圖
由於在我的一個程序中想要添加一個截圖功能,今天看一下利用Python怎樣截圖,功能實現都挺簡單了,直接上代碼
from
PIL
import
ImageGrab
im
=
ImageGrab.grab()
im.save(addr,'jpeg')
很簡單的幾行代碼就實現了我要的功能,PIL(Python
Image
Library)是Python的一個圖形庫,需要自己下載安裝,im
=
ImageGrab.grab()這行代碼實現截圖功能,可以帶參數,指定要截取圖片的坐標位置,不帶參數默認全屏截圖,im.save(addr,'jpeg')是保存截取的圖片,第一個參數是保存路徑,第二個參數是圖片格式
『叄』 python怎麼保存生成的圖像
要使用python,必須先安裝python,一般是2.7版本以上,不管是在windows系統,還是linux系統,安裝都是非常簡單的。
要使用python進行各種開發,就必須安裝對應的庫。(推薦學習:Python視頻教程)
這和matlab非常相似,只是matlab裡面叫工具箱(toolbox),而python裡面叫庫或包。安裝這些庫,一般都是使用pip來安裝。
使用python進行數字圖片處理,還得安裝Pillow包。雖然python裡面自帶一個PIL(python images library), 但這個庫現在已經停止更新了,所以旅空使用Pillow, 它是由PIL發展而來的。
圖片的打開與顯示
雖然使用的是Pillow,但它是由PIL fork而來,因此還是要從PIL中進行import. 使用open()函數來打開圖片,使用show()函數來顯示圖片。
這種圖片顯示方式是調用操作系統自帶的圖片瀏覽器來打開圖片,有些時候這種方式不太方便,因此我們也可以使用另鄭手上一種方式,讓程序來繪制圖片。
這種方法雖然復雜了些,但推薦使用這種方法,它使用一個matplotlib的庫來繪制圖片進行顯示。matplotlib是一個專業繪圖的庫,相當於matlab中的plot,可以設置多個figure,設置figure的標題,甚至可以使用subplot在一個figure中顯示多張圖片。
python中保存圖片的方法:
1、使用io模塊的imsave(fname,arr)函數來保存生成的圖片。拆叢瞎
第一個參數表示保存的路徑和名稱,第二個參數表示需要保存的數組變數。
示例:
2、使用plt.savefig()方法保存
『肆』 linux下python excel截取圖片
xlrd讀取單元格內容,再pil畫個表存成jpg。
畫表格那裡如果用latex做,存成pdf,再用工具批量轉jpg似乎也不錯。
『伍』 python 如何將大量圖片的url保存到本地
你如果要保存圖片的url,直接把imgsrc寫入本地文件就可以了,urllib.request.urlretrieve(imgsrc)這個的意思是你要保存的不是圖片的url,而是要把圖片下載下來,這個是要批量爬取網站上的圖片,需要考慮網站的反爬蟲措施了。
『陸』 python讀取保存多幀圖片數量少了
cv2.imshow("left", img_left)
filename3=str(number)+'n3'+'.jpg' #列印第number張圖片+增值方式+保悄賣清存類型
cv2.imwrite(savedpath + filename3, img_left)
"""
# 數據增強實現
"""
import cv2
import numpy as np
import os
# 圖像平移
def img_translation(image):
# 圖像平移 下、上、右、左平移
M = np.float32([[1, 0, 0], [0, 1, 100]])
img_down = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
M = np.float32([[1, 0, 0], [0, 1, -100]])
img_up = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
M = np.float32([[1, 0, 100], [0, 1, 0]])
img_right = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
M = np.float32([[1, 0, -100], [0, 1, 0]])
img_left = cv2.warpAffine(image, M, (image.shape[1], image.shape[0]))
# 保存圖片,需要保存上述的哪一圖片,就在cv2.imwrite()中,將哪一圖片名放入。
# filename='xxx' +'.jpeg'
# cv2.imwrite(savedpath + filename, img_left)
# 顯示圖形
cv2.imshow("down", img_down)
filename0=str(number)+'n0'+'.jpg'
cv2.imwrite(savedpath + filename0, img_down)
cv2.imshow("up", img_up)
filename1=str(number)+'n1'+'.jpg'配裂
cv2.imwrite(savedpath + filename1, img_up)
cv2.imshow("right", img_right)
filename2=str(number)+'n2'+'.jpg'
cv2.imwrite(savedpath + filename2, img_right)
cv2.imshow("left", img_left)
filename3=str(number)+'n3'+'.jpg'
cv2.imwrite(savedpath + filename3, img_left)
# 圖像縮放
def img_scale(image):
result = cv2.resize(image, (224, 224))
cv2.imshow("啟前scale", result)
filename=str(number)+'n5'+'.jpg'
cv2.imwrite(savedpath + filename, result)
# 圖像翻轉
def img_flip(image):
# 0以X軸為對稱軸翻轉,>0以Y軸為對稱軸翻轉, <0X軸Y軸翻轉
horizontally = cv2.flip(image, 0) # 水平鏡像
vertically = cv2.flip(image, 1) # 垂直鏡像
hv = cv2.flip(image, -1) # 水平垂直鏡像
# 顯示圖形
cv2.imshow("Horizontally", horizontally)
filename1=str(number)+'n6'+'.jpg'
cv2.imwrite(savedpath + filename1, horizontally)
cv2.imshow("Vertically", vertically)
filename2=str(number)+'n7'+'.jpg'
cv2.imwrite(savedpath + filename2, vertically)
cv2.imshow("Horizontally & Vertically", hv)
filename3=str(number)+'n8'+'.jpg'
cv2.imwrite(savedpath + filename3, hv)
# 圖像旋轉
def img_rotation(image):
# 原圖的高、寬 以及通道數
rows, cols, channel = image.shape
# 繞圖像的中心旋轉
# 參數:旋轉中心 旋轉度數 scale
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 30, 1)
# 參數:原始圖像 旋轉參數 元素圖像寬高
rotated = cv2.warpAffine(image, M, (cols, rows))
# 顯示圖像
cv2.imshow("rotated", rotated)
filename1=str(number)+'n9'+'.jpg'
cv2.imwrite(savedpath + filename1, rotated)
#選裝60度
W = cv2.getRotationMatrix2D((cols / 2, rows / 2), 60, 1)
# 參數:原始圖像 旋轉參數 元素圖像寬高
rotated1 = cv2.warpAffine(image, W, (cols, rows))
cv2.imshow("rotated", rotated)
filename2=str(number)+'n12'+'.jpg'
cv2.imwrite(savedpath + filename2, rotated1)
#選裝145度
W = cv2.getRotationMatrix2D((cols / 2, rows / 2), 60, 1)
# 參數:原始圖像 旋轉參數 元素圖像寬高
rotated2 = cv2.warpAffine(image, W, (cols, rows))
cv2.imshow("rotated", rotated)
filename3=str(number)+'n13'+'.jpg'
cv2.imwrite(savedpath + filename3, rotated2)
# 圖像加噪
def img_noise(image, mean=0, var=0.001):
'''
添加高斯雜訊
mean : 均值
var : 方差
'''
image = np.array(image / 255, dtype=float)
noise = np.random.normal(mean, var ** 0.5, image.shape)
out = image + noise
if out.min() < 0:
low_clip = -1.
else:
low_clip = 0.
out = np.clip(out, low_clip, 1.0)
out = np.uint8(out * 255)
cv2.imshow("noise", out)
filename3=str(number)+'n10'+'.jpg'
cv2.imwrite(savedpath + filename3, out)
# 圖像亮度調節
def img_brightness(image):
contrast = 1 # 對比度
brightness = 100 # 亮度
pic_turn = cv2.addWeighted(image, contrast, image, 0, brightness)
# cv2.addWeighted(對象,對比度,對象,對比度)
'''cv2.addWeighted()實現的是圖像透明度的改變與圖像的疊加'''
cv2.imshow('bright', pic_turn) # 顯示圖片
filename3=str(number)+'n11'+'.jpg'
cv2.imwrite(savedpath + filename3, pic_turn)
if __name__ == '__main__':
i = 0
path = '../Data/'
print(path)
savedpath = './result_new/'
filelist = os.listdir(path)
total_num = len(filelist)
for item in filelist:
number = i + 1
i = number
print("######")
print("列印到第",i,"張圖片")
src = cv2.imread(path + item)
img_translation(src)
img_scale(src)
img_flip(src)
img_rotation(src)
img_noise(src)
img_brightness(src)
cv2.waitKey(0)
cv2.destroyAllWindows()
代碼較為繁瑣,有空之後進行優化
輸出結果
『柒』 基於python語言的opencv如何把圖片中指定區域截取出來
3-切割輪廓
(這是我網站找的一篇 blog, 親測有效)