新聞中心
隨著數(shù)字相機的普及以及智能手機的使用,每個人都擁有大量的照片。將這些照片整理并儲存到數(shù)據(jù)庫中,將方便我們更好地對照片進行管理和查找,同時保證數(shù)據(jù)的長久存儲和安全性。本文將介紹照片導入數(shù)據(jù)庫的方法和技巧。

一、選擇合適的數(shù)據(jù)庫
我們需要選擇一個合適的數(shù)據(jù)庫,可以按需選擇開源或商用數(shù)據(jù)庫軟件,如MySQL、PostgreSQL、Oracle等。MySQL是更受歡迎的開源數(shù)據(jù)庫之一,它具有高可靠性、高性能、易于管理等優(yōu)點,因此可考慮作為我們的數(shù)據(jù)庫選擇。
二、儲存圖像數(shù)據(jù)
將圖像數(shù)據(jù)儲存在數(shù)據(jù)庫中有兩種方式:二進制大對象(BLOB)和File Stream。其中,BLOB是將圖像數(shù)據(jù)保存為二進制形式的一種方式,F(xiàn)ile Stream則是直接將圖像數(shù)據(jù)保存在硬盤上,并將存儲路徑保存在數(shù)據(jù)庫中。
BLOB用于儲存小數(shù)據(jù),雖然它可以儲存大數(shù)據(jù),但容易占用過多空間,影響查詢效率。因此,我們可以選擇File Stream來儲存圖像數(shù)據(jù)。
三、建立數(shù)據(jù)庫表
為了儲存照片,我們需要在數(shù)據(jù)庫中創(chuàng)建表。表包含兩個字段:圖像ID和圖像路徑。其中,圖像ID是唯一的標識符,它用于排序和檢索圖像,而圖像路徑是指向圖像所在文件夾的路徑。
建表語句如下:
CREATE TABLE ImageData(
imageID int PRIMARY KEY,
imagePath varchar(100)
);
四、導入照片
在上傳照片之前,我們需要先確定將要儲存的照片格式,例如JPG、PNG等。將照片轉(zhuǎn)換成統(tǒng)一的格式并重命名為圖像ID,便于之后在數(shù)據(jù)庫中進行檢索。
下面是實現(xiàn)照片導入數(shù)據(jù)庫的代碼示例:
import os
import psycopg2
def imageImport(conn, cur, filePath, imgID):
try:
# Open the image file
with open(filePath, ‘rb’) as fin:
image = fin.read()
# Insert the image into the ImageData table
cur.execute(“INSERT INTO ImageData (imageID, imagePath, image) VALUES (%s, %s, %s)”, (imgID, filePath, psycopg2.Binary(image)))
conn.commit()
except Exception as e:
print(e)
if __name__ == ‘__mn__’:
# Connect to the database
conn = psycopg2.connect(“dbname=’mydb’ user=’postgres’ host=’localhost’ password=’mypassword'”)
cur = conn.cursor()
# Identify the directory in which the images are stored
imgDir = ‘images’
# Insert all images into the database
for img in os.listdir(imgDir):
imgPath = os.path.join(imgDir, img)
imgID = int(os.path.splitext(img)[0])
imageImport(conn, cur, imgPath, imgID)
# Close the database connection
cur.close()
conn.close()
五、照片檢索
在數(shù)據(jù)庫中檢索照片時,我們通常會使用圖像ID進行查詢。在Python中,我們可以使用OpenCV庫和psycopg2庫實現(xiàn)照片檢索。
下面是讀取圖像和從數(shù)據(jù)庫中查詢照片的代碼示例:
import psycopg2
import numpy as np
import cv2
# Connect to the database
conn = psycopg2.connect(“dbname=’mydb’ user=’postgres’ host=’localhost’ password=’mypassword'”)
cur = conn.cursor()
# Read the image with the given image ID
def retrieveImage(imgID):
cur.execute(“SELECT image FROM ImageData WHERE imageID=%s”, (imgID,))
row = cur.fetchone()
if row is not None:
imgData = np.frombuffer(row[0], np.uint8)
img = cv2.imdecode(imgData, cv2.IMREAD_COLOR)
return img
return None
# Close the database connection
cur.close()
conn.close()
六、
以上就是照片導入數(shù)據(jù)庫的方法和技巧。在實際操作過程中,需要根據(jù)實際情況進行不同的處理。例如,盡可能減少BLOB類型的數(shù)據(jù),使用File Stream來儲存數(shù)據(jù),在數(shù)據(jù)庫中建立索引等。通過以隨身攜帶的硬盤或云端存儲圖片的方式,再導入數(shù)據(jù)庫中,實現(xiàn)了電腦和手機平臺圖片集中保存和查詢,提高了查找效率和管理便捷性,同時也保證了數(shù)據(jù)的安全性和長久性。
相關(guān)問題拓展閱讀:
- 怎么把圖片,視頻存儲在數(shù)據(jù)庫中
- 數(shù)據(jù)庫怎樣可以將圖片放到里面
怎么把圖片,視頻存儲在數(shù)據(jù)庫中
背景
MySQL 一直以來都有 TEXT、擾灶BLOB 等類型用來存儲圖片、視頻等大對象信息。比如一張圖片,隨便一張都 5M 以上。視頻也是,隨便一部視頻就是 2G 以上。
假設用 MySQL 來存放電影視頻等信息,一部是 2G,那么存儲 1000 部就是 2TB,2TB 也就是 1000 條記錄而已,但是對數(shù)據(jù)庫性能來說,不僅僅是看記錄數(shù)量,更主要的還得看占用磁盤洞李頌空間大小??臻g大了,所有以前的經(jīng)驗啥的都失效了。
所以一般來說存放這類信息,也就是存儲他們的存放路徑,至于文件本身存放在哪里,那這就不是數(shù)據(jù)庫考慮的范疇了。數(shù)據(jù)庫只關(guān)心怎么來的快,怎么來的小。
舉例
雖然不推薦 MySQL 這樣做,但是也得知道 MySQL 該怎么做才行,做到心里有數(shù)。比如下面一張微信圖片,大概 5M 的樣子。
root@ytt:/var/lib/mysql-files# ls -sihl 微信圖片_.jpg.4M -rw-r–r– 1 root root 5.4M Jul 11 07:17 微信圖片_.jpg
拷貝 100 份這樣的圖片來測試
root@ytt:/var/lib/mysql-files# for i in `seq 1 100`; do cp 微信圖片_.jpg “$i”.jpg;done;
root@ytt:/var/lib/mysql-files# ls
100.jpg 17.jpg 25.jpg 33.jpg 41.jpg 4.jpg 58.jpg 66.jpg 74.jpg 82.jpg 90.jpg 99.jpg f8.tsv
10.jpg 18.jpg 26.jpg 34.jpg 42.jpg 50.jpg 59.jpg 67.jpg 75.jpg 83.jpg 91.jpg 9.jpg 微信圖片_.jpg
1111.jpg 19.jpg 27.jpg 35.jpg 43.jpg 51.jpg 5.jpg 68.jpg 76.jpg 84.jpg 92.jpg f1.tsv
11.jpg 1.jpg 28.jpg 36.jpg 44.jpg 52.jpg 60.jpg 69.jpg 77.jpg 85.jpg 93.jpg f2.tsv
12.jpg 20.jpg 29.jpg 37.jpg 45.jpg 53.jpg 61.jpg 6.jpg 78.jpg 86.jpg 94.jpg f3.tsv
13.jpg 21.jpg 2.jpg 38.jpg 46.jpg 54.jpg 62.jpg 70.jpg 79.jpg 87.jpg 95.jpg f4.tsv
14.jpg 22.jpg 30.jpg 39.jpg 47.jpg 55.jpg 納鄭63.jpg 71.jpg 7.jpg 88.jpg 96.jpg f5.tsv
15.jpg 23.jpg 31.jpg 3.jpg 48.jpg 56.jpg 64.jpg 72.jpg 80.jpg 89.jpg 97.jpg f6.tsv
16.jpg 24.jpg 32.jpg 40.jpg 49.jpg 57.jpg 65.jpg 73.jpg 81.jpg 8.jpg 98.jpg f7.tsv
我們建三張表,分別用 LONGBLOB、LONGTEXT 和 VARCHAR 來存儲這些圖片信息
mysql> show create table tt_image1G
*************************** 1. row ***************************
Table: tt_image1
Create Table: CREATE TABLE `tt_image1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`image_file` longblob,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
mysql> show create table tt_image2G
*************************** 1. row ***************************
Table: tt_image2
Create Table: CREATE TABLE `tt_image2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`image_file` longtext,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
mysql> show create table tt_image3G
*************************** 1. row ***************************
Table: tt_image3
Create Table: CREATE TABLE `tt_image3` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`image_file` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
我們來給三張表插入 100 張圖片(插入前,建議把 max_allowed_packet 設置到更大)
tt_image1
root@ytt:/var/lib/mysql-files# for i in `seq 1 100`;
do mysql -S /var/run/mysqld/mysqld.sock -e “insert into ytt.tt_image1(image_file)
values (load_file(‘/var/lib/mysql-files/$i.jpg’))”;done;
tt_image2
root@ytt:/var/lib/mysql-files# for i in `seq 1 100`;
do mysql -S /var/run/mysqld/mysqld.sock -e “insert into ytt.tt_image2(image_file)
values (hex(load_file(‘/var/lib/mysql-files/$i.jpg’)))”;done;
tt_image3
root@ytt:/var/lib/mysql-files# aa=’begin;’;for i in `seq 1 100`;
do aa=$aa”insert into ytt.tt_image3(image_file) values
(‘/var/lib/mysql-files/$i.jpg’);”;
done;aa=$aa’commit;’;mysql -S /var/run/mysqld/mysqld.sock -e “`echo $aa`”;
檢查下三張表記錄數(shù)
mysql> select ‘tt_image1’ as name ,count(*) from tt_image1 union allselect ‘tt_image2’,count(*) from tt_image2 union all select ‘tt_image3’, count(*) from tt_image3;+++| name | count(*) |+++| tt_image1 ||| tt_image2 ||| tt_image3 ||+++3 rows in set (0.00 sec)
看下文件大小,可以看到實際大小排名,LONGTEXT 字段存儲的更大,LONGBLOB 字段縮小到一半,最小的是存儲圖片路徑的表 tt_image3。所以這里從存儲空間來看,存放路徑最占優(yōu)勢。
root@ytt:/var/lib/mysql/ytt# ls -silhS tt_image*.1G -rw-rmysql mysql 1.1G Jul 11 07:27 tt_image2.ibdM -rw-rmysql mysql 544M Jul 11 07:26 tt_image1.ibdK -rw-rmysql mysql 112K Jul 11 07:27 tt_image3.ibd
那么怎么把圖片取出來呢?
tt_image3 肯定是最容易的
mysql> select * from tt_image3;+—-++| id | image_file |+—-++| 1 | /var/lib/mysql-files/1.jpg |+—-++…100 rows in set (0.00 sec)
tt_image1 直接導出來二進制文件即可,下面我寫了個存儲過程,導出所有圖片。
mysql> DELIMITER $$mysql> USE `ytt`$$mysql> DROP PROCEDURE IF EXISTS `sp_get_image`$$mysql> CREATE DEFINER=`ytt`@`localhost` PROCEDURE `sp_get_image`()mysql> BEGIN DECLARE i,cnt INT DEFAULT 0; SELECT COUNT(*) FROM tt_image1 WHERE 1 INTO cnt; WHILE i DELIMITER ;mysql> call sp_get_image;
tt_image2 類似,把 select 語句里 image_file 變?yōu)?unhex(image_file) 即可。
總結(jié)
這里我舉了個用 MySQL 來存放圖片的例子,總的來說有以下三點:
占用磁盤空間大(這樣會帶來各種各樣的功能與性能問題,比如備份,寫入,讀取操作等)
使用不易
還是推薦用文件路徑來代替實際的文件內(nèi)容存放
我們設計程序時一般不會將圖片、音頻和視頻資料直接存入數(shù)據(jù)庫中,這樣會讓數(shù)據(jù)庫的體積驟大,嚴重影響數(shù)據(jù)庫運行的效率。
一般會搭建一個多媒體服務器,支持流媒體格式絕和處理。程序或者網(wǎng)站中涉及到的圖片、陵宏局音頻和視頻等資料會存放在
流媒體服務器
上,然后可以記錄訪問其的路徑,然后把這個路徑存入數(shù)據(jù)庫,在你的應用系統(tǒng)調(diào)用時,可以通過路徑結(jié)合對應的多媒體播尺讓放插件就可以實現(xiàn)。
圖片、視頻一般都是存儲在磁盤中,然后把存儲在磁盤里的路徑存儲在數(shù)據(jù)庫中
數(shù)據(jù)庫怎樣可以將圖片放到里面
放在根目下,數(shù)據(jù)庫的文件路徑少了一層,容易被猜測到,而放塵戚扮到其他目錄下,因為你的這個其他目錄的目錄名是你自己定的,而且可以設置得非常復雜,所以不容易被猜測到。
一般的服務器根目錄,無外乎是:wwwroot,www,web之類的文件名名稱。而你自己設定的其他目錄,則是別人不知道的,比仔局如你可以設置你的數(shù)據(jù)庫存放目錄名為:#$這樣,一般人是猜不到的。
但如果要真正做到數(shù)據(jù)庫安全,重要的不是數(shù)據(jù)庫放在哪兒(當然除了數(shù)據(jù)庫放在空間服務商指定的文件夾,因為這個派灶文件夾不允許從外界訪問,只允許程序從服務器問),重要的是你的ASP程序?qū)懙膲虿粔虬踩??!?/p>
關(guān)于怎么把照片導入數(shù)據(jù)庫的介紹到此就結(jié)束了,不知道你從中找到你需要的信息了嗎 ?如果你還想了解更多這方面的信息,記得收藏關(guān)注本站。
成都創(chuàng)新互聯(lián)建站主營:成都網(wǎng)站建設、網(wǎng)站維護、網(wǎng)站改版的網(wǎng)站建設公司,提供成都網(wǎng)站制作、成都網(wǎng)站建設、成都網(wǎng)站推廣、成都網(wǎng)站優(yōu)化seo、響應式移動網(wǎng)站開發(fā)制作等網(wǎng)站服務。
文章標題:照片導入數(shù)據(jù)庫的方法與技巧(怎么把照片導入數(shù)據(jù)庫)
文章網(wǎng)址:http://m.fisionsoft.com.cn/article/cdojchd.html


咨詢
建站咨詢
