티스토리 뷰

SWDesk

[Python] Editing Images

inhae 2021. 6. 30. 00:15
  • CombinImages(params) : 다수 이미지를 결합
  • AddPrice2Image(params) : 이미지에 가격표 부착
  • SaveWebImage(imageURL) : 웹이미지를 파일로 저장
  • MakeTextImage(textData, imageName) : 텍스트가 포함된 이미지 만들기

 

    def CombineImages(self, params):
        imageSizeX = params.get('ImageSizeX')
        imageSizeY = params.get('ImageSizeY')
        imageNumberX = params.get('ImageNumberX')
        imageNumberY = params.get('ImageNumberY')
        imagePaths = params.get('ImagePaths')
        imageFilename = params.get('ImageFilename')

        newImage = Image.new("RGB", (imageSizeX, imageSizeY), 300000)
        divSizeX = int((imageSizeX - 10 - ((imageNumberX-1)*10))/imageNumberX)
        divSizeY = int((imageSizeY - 10 - ((imageNumberY-1)*10))/imageNumberY)
        print("[divX]{}, [divY]{}", divSizeX, divSizeY)
        indexX = 0
        indexY = 0
        for imagePath in imagePaths:
            if indexX >= imageNumberX: 
                indexX = 0
                indexY += 1
            image1 = Image.open(imagePath)
            image1 = image1.resize((divSizeX, divSizeY))
            posX = (indexX*(divSizeX+10))+5
            posY = (indexY*(divSizeY+10))+5
            print("[posX]", posX, ", [posY]", indexY)
            newImage.paste(image1, (posX, posY))
            indexX += 1
        newImage.show()
        #newImage.save(newImagename)
        #print("[New Image]", newImagename)
    def SaveWebImage(self, imageURL):
        image = requests.get(imageURL).content
        fileName = os.path.basename(imageURL)
        print("[Filename]", fileName)
        with open(fileName, 'wb') as f:
            f.write(image)

        return fileName
    def MakeTextImage(self, textData=None, savedImagePathname=None):
        if not textData: textData = "Text-Data"
        if not savedImagePathname: savedImagePathname = "Image002.jpg"
        fontPath = "C:/Users/LOGINID/AppData/Local/Microsoft/Windows/Fonts"
        fontName = "NanumGothic.ttf"
        fontSize = 20
        width_TextImage = 190
        height_TextImage = 40
        textPositionX = 10
        textPositionY = 10
        
        image1 = Image.new("RGB", (width_TextImage, height_TextImage), color=(100, 100, 10))
        textFont = ImageFont.truetype(fontPath+"/"+fontName, fontSize, encoding="UTF-8")
        imageD = ImageDraw.Draw(image1)
        imageD.text((textPositionX, textPositionY), textData, font=textFont, fill=(255,255,255))
        #image1.show()
        image1.save(savedImagePathname)

        return savedImagePathname

파이썬 이미지에 글자(텍스트) 넣기,합성 다른 이미지 추가-Feat.Pillow.pdf
0.75MB
[Python] 파이썬 이미지 처리 pillow(PIL) 설치 및 사용 예제 총정리_드루와 - 정보의 공유 사회.pdf
5.64MB

 

반응형

'SWDesk' 카테고리의 다른 글

[PHP, Python] JSON Test  (0) 2021.07.10
[Android, PHP] JSON Test  (0) 2021.07.07
[Python] Download Google Images  (0) 2021.06.29
[Python] Sharing Google Spreadsheet  (0) 2021.06.29
[Python] Sentence Similarity  (0) 2021.06.29
반응형
250x250
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함