python-imaging-library

Accessing extracted zip file in Colab

怎甘沉沦 提交于 2020-12-14 11:40:17
问题 Some case study here. I am trying to play with PIL library in Google Colab, and can't get ImageFont to read my originally zipped file. The code: import requests, zipfile, io r3 = requests.get('https://sources.archlinux.org/other/community/ttf-roboto/ttf-roboto-hinted-2.138.zip') z3 = zipfile.ZipFile(io.BytesIO(r3.content)) z3.extractall() So far so good, and if I browse my directory with ls, it shows me the elements: ls Shows: LICENSE RobotoCondensed-Regular.ttf __MACOSX/ Roboto-Italic.ttf

Lanczos Interpolation in Python with 2D images

╄→гoц情女王★ 提交于 2020-12-13 05:39:12
问题 I try to rescale 2D images (greyscale). The image size is 256x256 and the desired output is 224x224. The pixel values range from 0 to 1300. I tried 2 approaches to rescale them with Lanczos Interpolation: First using PIL Image: import numpy as np from PIL import Image import cv2 array = np.random.randint(0, 1300, size=(10, 256, 256)) array[0] = Image.fromarray(array[0]).resize(size=(224, 224), resample=Image.LANCZOS) resulting in the error message: ValueError: image has wrong mode And then

Pillow ImageDraw text coordinates to center

余生颓废 提交于 2020-12-08 10:41:22
问题 The code below brings the text in the center of x, but i don't know how to calculate the center for the y coordinate... it is not (imgH-h)/2! (The right y-coordinate is -80) from PIL import Image, ImageDraw, ImageFont font= './fonts/BebasNeue-Regular.ttf' color = (255, 244, 41) text = 'S' img = Image.new('RGB', (500, 500), color=(255, 255, 255)) imgW, imgH = img.size fnt = ImageFont.truetype(font, 600) d = ImageDraw.Draw(img) w, h = d.textsize(text, fnt) nullH = (imgH-h) print(imgH, h) d.text

Combining three RGB images into a single RGB image

拈花ヽ惹草 提交于 2020-12-08 05:13:48
问题 I have three RGB images, but each one has only 1 non-zero channel (ie. one has a red channel with 0's in the blue and green channels) and I want to combine them into a single RGB image with the correct channel from each. I apologise for my phrasing, I don't know much of the terminology (which really isn't helping my search queries) Here are my images: Blue Green Red 回答1: I think you can use Image.merge here and take the appropriate channels from each image. Note that I'm using requests.get(..

How to find the rectangles in an image

穿精又带淫゛_ 提交于 2020-12-07 04:41:00
问题 This is the image generated by this code in this answer (except for the green circles, which I drew on afterwards): The shape is (707, 1028, 3), so each pixel has three channels (RGB), but is only filled with white and black. It would be better if it were converted to an 8-bit image. I need to get the position and the size of each rectangle in the image. I have some code using PIL and .load() to access each pixel, but is too slow. In the PIL version I look for start corner and end corner. The

How to find the rectangles in an image

血红的双手。 提交于 2020-12-07 04:40:35
问题 This is the image generated by this code in this answer (except for the green circles, which I drew on afterwards): The shape is (707, 1028, 3), so each pixel has three channels (RGB), but is only filled with white and black. It would be better if it were converted to an 8-bit image. I need to get the position and the size of each rectangle in the image. I have some code using PIL and .load() to access each pixel, but is too slow. In the PIL version I look for start corner and end corner. The

How to find the rectangles in an image

人走茶凉 提交于 2020-12-07 04:39:13
问题 This is the image generated by this code in this answer (except for the green circles, which I drew on afterwards): The shape is (707, 1028, 3), so each pixel has three channels (RGB), but is only filled with white and black. It would be better if it were converted to an 8-bit image. I need to get the position and the size of each rectangle in the image. I have some code using PIL and .load() to access each pixel, but is too slow. In the PIL version I look for start corner and end corner. The

Python Script to detect broken images

允我心安 提交于 2020-11-29 10:23:09
问题 I wrote a python script to detect broken images and count them, The problem in my script is it detects all the images and does not detect broken images. How to fix this. I refered : How to check if a file is a valid image file? for my code My code import os from os import listdir from PIL import Image count=0 for filename in os.listdir('/Users/ajinkyabobade/Desktop/2'): if filename.endswith('.JPG'): try: img=Image.open('/Users/ajinkyabobade/Desktop/2'+filename) img.verify() except(IOError