66
44

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Python]Base64ใงใ‚จใƒณใ‚ณใƒผใƒ‰ใ•ใ‚ŒใŸ็”ปๅƒใƒ‡ใƒผใ‚ฟใ‚’ใƒ‡ใ‚ณใƒผใƒ‰ใ™ใ‚‹ใ€‚

66
Last updated at Posted at 2019-01-22

็›ฎ็š„

Base64ใงใ‚จใƒณใ‚ณใƒผใƒ‰ใ•ใ‚ŒใŸ็”ปๅƒใƒ‡ใƒผใ‚ฟใ‚’ใƒ‡ใ‚ณใƒผใƒ‰ใ—ใพใ™ใ€‚

ๆŠ•็จฟ็†็”ฑ

Base64ใง็”ปๅƒใ‚’ใ‚จใƒณใ‚ณใƒผใƒ‰ใ™ใ‚‹ใƒ—ใƒญใ‚ฐใƒฉใƒ ใฏๆŽขใ›ใฐใ‚ˆใใ‚ใ‚Šใพใ™ใŒใ€
ใƒ‡ใ‚ณใƒผใƒ‰ใ—ใฆๅพ—ใŸ็”ปๅƒใ‚’ไฟๅญ˜ใ™ใ‚‹ใƒ—ใƒญใ‚ฐใƒฉใƒ ใฏๆŽขใ—ใŸ้™ใ‚Š่ฆ‹ใคใ‹ใ‚‰ใชใ‹ใฃใŸ็‚บๆŠ•็จฟใ—ใพใ—ใŸใ€‚

ไฝฟ็”จ่จ€่ชž

Python3.7.0

Base64ใง็”ปๅƒใ‚’ใ‚จใƒณใ‚ณใƒผใƒ‰ใ™ใ‚‹

ใ‚จใƒณใ‚ณใƒผใƒ‰ใ™ใ‚‹็”ปๅƒ

image001.jpg

ใ‚จใƒณใ‚ณใƒผใƒ‰ใ™ใ‚‹ใŸใ‚ใฎใƒ—ใƒญใ‚ฐใƒฉใƒ 

import base64

# Base64ใงใ‚จใƒณใ‚ณใƒผใƒ‰ใ™ใ‚‹็”ปๅƒใฎใƒ‘ใ‚น
target_file=r"original.jpg"
# ใ‚จใƒณใ‚ณใƒผใƒ‰ใ—ใŸ็”ปๅƒใฎไฟๅญ˜ๅ…ˆใƒ‘ใ‚น
encode_file=r"encode.txt"

with open(target_file, 'rb') as f:
    data = f.read()
# Base64ใง็”ปๅƒใ‚’ใ‚จใƒณใ‚ณใƒผใƒ‰
encode=base64.b64encode(data)
with open(encode_file,"wb") as f:
    f.write(encode)

Base64ใงใƒ‡ใ‚ณใƒผใƒ‰ใ—ใฆ็”ปๅƒใ‚’ไฟๅญ˜ใ™ใ‚‹ใ€‚

ใƒ‡ใ‚ณใƒผใƒ‰ใ™ใ‚‹ๆ–‡ๅญ—ๅˆ—(ใ•ใฃใใฎ็”ปๅƒใ‚’ใ‚จใƒณใ‚ณใƒผใƒ‰ใ—ใŸๆ–‡ๅญ—ๅˆ—)

image.png

ใƒ‡ใ‚ณใƒผใƒ‰ใ™ใ‚‹ใŸใ‚ใฎใƒ—ใƒญใ‚ฐใƒฉใƒ 

import cv2
import base64
import numpy as np
import io

# Base64ใงใ‚จใƒณใ‚ณใƒผใƒ‰ใ•ใ‚ŒใŸใƒ•ใ‚กใ‚คใƒซใฎใƒ‘ใ‚น
target_file=r"encode.txt"
# ใƒ‡ใ‚ณใƒผใƒ‰ใ•ใ‚ŒใŸ็”ปๅƒใฎไฟๅญ˜ๅ…ˆใƒ‘ใ‚น
image_file=r"decode.jpg"

with open(target_file, 'rb') as f:
    img_base64 = f.read()

# ใƒใ‚คใƒŠใƒชใƒ‡ใƒผใ‚ฟ <- base64ใงใ‚จใƒณใ‚ณใƒผใƒ‰ใ•ใ‚ŒใŸใƒ‡ใƒผใ‚ฟ  
img_binary = base64.b64decode(img_base64)
jpg=np.frombuffer(img_binary,dtype=np.uint8)

# raw image <- jpg
img = cv2.imdecode(jpg, cv2.IMREAD_COLOR)
# ็”ปๅƒใ‚’ไฟๅญ˜ใ™ใ‚‹ๅ ดๅˆ
cv2.imwrite(image_file,img)

# ่กจ็คบ็ขบ่ช
cv2.imshow('window title', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

ใƒ‡ใ‚ณใƒผใƒ‰ใ—ใฆๅพ—ใ‚‰ใ‚ŒใŸ็”ปๅƒ

image.png
ๆญฃใ—ใ็”ปๅƒใŒใƒ‡ใ‚ณใƒผใƒ‰ใ•ใ‚Œใพใ—ใŸ๏ผ

66
44
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
66
44

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?