python-imaging-library

KeyError: ((1, 1, 1280), '|u1') while using PIL's Image.fromarray - PIL

為{幸葍}努か 提交于 2021-02-11 14:34:57
问题 I have this code: from PIL import Image import numpy as np img = Image.open('img.jpg') Image.fromarray(np.array([[np.mean(i, axis=1).astype(int).tolist()]*len(i) for i in np.array(img).tolist()]).astype('uint8')).show() And I am trying to modify the pixels of the image in PIL, however when I run it it gives an error as follows: KeyError: ((1, 1, 1280), '|u1') Not just that, it also outputs a second error as follows: TypeError: Cannot handle this data type Is there a way to overcome this? P.S.

TypeError: string argument expected, got 'bytes'

余生颓废 提交于 2021-02-11 13:42:10
问题 I would like to convert the below hex sequences to images, in the process of sifting through quite a number of problems that are similar to mine none have come close as to that solved in https://stackoverflow.com/a/33989302/13648455, my code is below, where could I be going wrong? data = "2a2b2c2a2b2c2a2b2c2a2b2cb1" buf = io.StringIO() for line in data.splitlines(): line = line.strip().replace(" ", "") if not line: continue bytez = binascii.unhexlify(line) buf.write(bytez) with open("image

How to resize an image using imageio?

断了今生、忘了曾经 提交于 2021-02-11 12:32:30
问题 Consider an image img of type imageio.core.util.Array . The shape of img is (256, 256, 3) . I want to resize it to (128, 128, 3) . I tried at least the following three: img.resize(img_res, pilmode="RGB") img.resize(img_res) img = cv2.resize(img, self.img_res) Here img_res = (128, 128) . None of them worked well. How can I resize my image to the desired size? 回答1: According to the documentation on imageio.core.util.Array, Array is "a subclass of np.ndarray [...]". Thus, when calling resize on

Image Erosion manual implementation not doing anything Python

我是研究僧i 提交于 2021-02-11 12:20:05
问题 Testing image I write a Python script that manually do a Erosion Morphological Operation to an image using the attached test image, but when I display both the original and altered image, this last one still looks the same even thought is it supposed to be eroded. I have 3 functions, my Main function, a RGB to Gray Conversion function and the Erosion function. import cv2 import math import numpy as np from PIL import Image, ImageFilter def main(): #Read image img = cv2.imread('pattern04.bmp')

Saving Image from URL using Python Requests - URL type error

折月煮酒 提交于 2021-02-11 06:55:02
问题 Using the following code: with open('newim','wb') as f: f.write(requests.get(repr(url))) where the url is: url = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFoAAAArCAYAAAD41p9mAAAAzUlEQVR42u3awQ4DIQhFUf7/p9tNt20nHQGl5yUuh4c36BglgoiIiIiIiGiVHq+RGfvdiGG+lxKonGiWd4vvKZNd5V/u2zXRO953c2jx3bGiMrewLt+PgbJA/xJ3RS5dvl9PEdXLduK3baeOrKrc1bcF9MnLP7WqgR4GOjtOl28L6AlHtLSqBhpooIEGGmiggQYaaKCBBhpodx3H3XW4vQN6HugILyztoL0Zhlfw9G4tfR0FfR0VnTw6lQoT0XtXmMxfdJPuALr0x5Pp+wT35KKWb6NaVgAAAABJRU5ErkJggg==' I get

When converting an image into an array and viceversa, are there extra considerations one must take into account?

∥☆過路亽.° 提交于 2021-02-11 06:22:57
问题 I wrote this code to switch the red and blue values in the RGB array from a given image: from PIL import Image import numpy as np image = Image.open("image.jpg") RGBarr = np.asarray(image) newRGB = np.full_like(RGBarr, 1) red = RGBarr[..., 0] green = RGBarr[...,1] blue = RGBarr[..., 2] newRGB[..., 0] = blue newRGB[..., 1] = green newRGB[..., 2] = red inv_image = Image.fromarray(newRGB, 'RGB') inv_image.save('inv_image.png') inv_image.show() I tried it with multiple images, and it works almost

rgb values and pixels

大憨熊 提交于 2021-02-10 14:46:46
问题 def normalize_brightness(img: Image) -> Image: """ Normalize the brightness of the given Image img by: computing the average brightness of the picture: - this can be done by calculating the average brightness of each pixel in img (the average brightness of each pixel is the sum of the values of red, blue and green of the pixel, divided by 3 as a float division) - the average brightness of the picture is then the sum of all the pixel averages, divided by the product of the width and height of

Why does openCV store a file larger(kB) than the original?

元气小坏坏 提交于 2021-02-10 14:13:13
问题 I have code that loads and saves the image in two different ways - first using openCV, the second using PIL. import cv2 from PIL import Image img = cv2.imread("/home/myname/png/image.png") cv2.imwrite("/home/myname/png/image_save.png", img) img = Image.open("/home/myname/png/image.png") img.save("/home/myname/png/image_save_pil.png") The original image is 204.6 kB in size. The result obtained with openCV is 245.0 kB, the result of PIL is 204.6 kB. Why does the image saved with openCV have a

How to map rectangle image to quadrilateral with PIL?

倾然丶 夕夏残阳落幕 提交于 2021-02-10 13:18:11
问题 Python PIL library allows me to map any quadrilateral in an image to rectangle using im.transform(size, QUAD, data) What I need is a function that does the opposite, i.e. map a rectangular image to specified quadrilateral. I figured this might be achieved with the above mentioned function like this: I.e. I would find such quad (the red one in the image) that would, using the function im.transform(size, QUAD, data) transform the image to quad I want. The problem is I don't know how to find the

what is the best way to save PIL image in json

荒凉一梦 提交于 2021-02-10 12:37:25
问题 I'm trying to send json dict that should contain Pillow image as one of his fields, to do that I have to convert the image to string. I tried to use pillow function: image.toString() but still got it as bytes, so I tried to encode it: buff = BytesIO() image.save(buff, format="JPEG") img_str = base64.b64encode(buff.getvalue()) but still got it as bytes. How can I convert Pillow images to format that can be saved in json file? 回答1: In the comments, Mark Setchell suggests calling .decode('ascii'