i want to convert a Pyglet.AbstractImage object to an PIL image for further manipulation
here are my codes
from pyglet import image
from PIL import Image
pic = image.load('pic.jpg')
data = pic.get_data('RGB', pic.pitch)
im = Image.fromstring('RGB', (pic.width, pic.height), data)
im.show()
but the image shown went wrong.
so how to convert an image from pyglet to PIL properly?
I think I find the solution
the pitch in Pyglet.AbstractImage instance is not compatible with PIL
I found in pyglet 1.1 there is a codec function to encode the Pyglet image to PIL
here is the link to the source
so the code above should be modified to this
from pyglet import image
from PIL import Image
pic = image.load('pic.jpg')
pitch = -(pic.width * len('RGB'))
data = pic.get_data('RGB', pitch) # using the new pitch
im = Image.fromstring('RGB', (pic.width, pic.height), data)
im.show()
I'm using a 461x288 image in this case and find that pic.pitch is -1384
but the new pitch is -1383
This is an open wishlist item:
AbstractImage to/from PIL image.
Related
I am working on a template kind of thing where the input are some images and text while the result being an image.
Here one of the input is a QR code generated using pyqrcode. I am using PIL library to paste images and text. Problem here is google lens is not recognizing the qr code.
What to do to solve this issue.
The code I written to generate image
from PIL import Image, ImageFont, ImageDraw
import pyqrcode
#Read the two images
image1 = Image.open('./10.jpg')
#image1.show()
image2 = Image.open('./2.jpg')
#image2.show()
#resize, first image,second image
image1 = image1.resize((320,640))
image2=image2.resize((144,144))
image1_size = image1.size
image2_size = image2.size
#pasting image2 on image1
image1.paste(image2,(100,220))
#Adding text
font1=ImageFont.truetype('./1.ttf',30)
font2=ImageFont.truetype('./1.ttf',20)
text1="My Home Jewels"
image3=ImageDraw.Draw(image1)
image3.text((30,30),text1,(237,230,211),font=font1)
#Adding QR code
link1="www.google.com"
url=pyqrcode.QRCode(link1,error='L')
url.png('test.png',scale=2)
img=Image.open('test.png')
#img.save('./qr.jpg',quality=100)
#img=img.resize((72,72))
image1.paste(img,(30,500))
#Adding link as text
image3=ImageDraw.Draw(image1)
image3.text((120,520),link1,(237,230,211),font=font2)
image1.show()
image1.save('./3.jpg',quality=100)
I cannot reproduce. This image works for me.
I'm trying to download an svg image and open it in opencv for further processing. What I am doing is to convert from svg to png format with cairosvg, open it using Pillow and finally convert to opencv. The problem here is that the image converted in opencv does not look exactly the same as the one in PIL format.
from io import BytesIO
import numpy as np
import requests
from PIL import Image
from cairosvg import svg2png
import cv2
svg_url = 'https://logincdn.msauth.net/16.000.28611.4/content/images/microsoft_logo_ee5c8d9fb6248c938fd0dc19370e90bd.svg'
# Get svg data
svg_data = requests.get(svg_url).content
# Convert from svg to png
png = svg2png(bytestring=svg_data)
# Open png in PIL
pil_img = Image.open(BytesIO(png))
pil_img.show() # This looks good
# Convert to opencv
cv_img = np.array(pil_img.convert('RGB'))[:, :, ::-1].copy() # Taken from https://stackoverflow.com/questions/14134892/convert-image-from-pil-to-opencv-format
cv2.imshow('cv_img', cv_img) # This does not look right
cv2.waitKey(0)
These are the resulting images from PIL and opencv format respectively:
The opencv image does not look right, e.g. the text does not have space between characters.
Why is that and how can I fix it?
As suggested, you have to preserve alpha channel.
import numpy as np
import requests
from io import BytesIO
from PIL import Image
from cairosvg import svg2png
import cv2
svg_url = 'https://logincdn.msauth.net/16.000.28611.4/content/images/microsoft_logo_ee5c8d9fb6248c938fd0dc19370e90bd.svg'
svg_data = requests.get(svg_url).content
png = svg2png(bytestring=svg_data)
pil_img = Image.open(BytesIO(png)).convert('RGBA')
pil_img.save('output/pil.png')
cv_img = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGBA2BGRA)
cv2.imwrite('cv.png', cv_img)
For my current task, I need to convert a 24 color bmp, to a 16 color bmp file. and Print the image in the pdf file. I tired using the PIL module,but it didn't helped me.
from PIL import Image
path = r'C:\Display_Icon_Testing\Captured_Images\Impl_Modulation_Screen.bmp'
im = Image.open(path)
print im
im1 = Image.open(path).convert('P')
print im1
please help me with this.
The code below will read an image in any format that PIL understands, convert it to 16 colors, and save it as a PDF file. By default, PIL uses Floyd-Steinberg dithering to improve the image quality when converting a 24 bit image to a palette-based image.
I'm using the Pillow fork of PIL, since the original PIL is no longer maintained, but this code should work properly on original PIL or Pillow.
from PIL import Image
iname = 'test.bmp'
oname = 'test.pdf'
img = Image.open(iname)
newimg = img.convert(mode='P', colors=16)
newimg.save(oname)
I'm trying to use this this approach to add a semi-transparent polygon to an image. The problem is the image is a JPEG. I know that JPEGs don't have an alpha channel, so I was hoping there was a way I could have PIL take in a JPEG, convert it to a form which has an alpha channel, add the semi-transparent mask, then merge the mask with the image and convert it back into a JPEG for saving. Can PIL accomplish this? If not, how else might I go about doing this? Thanks!
That's easy. Just paste the jpeg into a new rgba Image():
#!/usr/bin/env python3
from PIL import Image
from PIL import ImageDraw
im = Image.open("existing.jpg")
logo = Image.open("python-32.png")
back = Image.new('RGBA', im.size)
back.paste(im)
poly = Image.new('RGBA', (512,512))
pdraw = ImageDraw.Draw(poly)
pdraw.polygon([(128,128),(384,384),(128,384),(384,128)],
fill=(255,255,255,127),outline=(255,255,255,255))
back.paste(poly, (0,0), mask=poly)
back.paste(logo, (im.size[0]-logo.size[0], im.size[1]-logo.size[1]), mask=logo)
back.show()
This additionally adds a png (with transparency) to the image.
I writing a Python app where I need to do some image tasks.
I'm trying PIL and it's ImageOps module. But it looks that the unsharp_mask method is not working properly. It should return another image, but is returning a ImagingCore object, which I don't know what is.
Here's some code:
import Image
import ImageOps
file = '/home/phius/test.jpg'
img = Image.open(file)
img = ImageOps.unsharp_mask(img)
#This fails with AttributeError: save
img.save(file)
I'm stuck on this.
What I need: Ability to do some image tweeks like PIL's autocontrast and unsharp_mask and to re-size, rotate and export in jpg controlling the quality level.
What you want is the filter command on your image and the PIL ImageFilter module[1] so:
import Image
import ImageFilter
file = '/home/phius/test.jpg'
img = Image.open(file)
img2 = img.filter(ImageFilter.UnsharpMask) # note it returns a new image
img2.save(file)
The other filtering operations are part of the ImageFilter module[1] as well and are applied the same way. The transforms (rotation, resize) are handled by calling functions[2] on the image object itself i.e. img.resize. This question addresses JPEG quality How to adjust the quality of a resized image in Python Imaging Library?
[1] http://effbot.org/imagingbook/imagefilter.htm
[2] http://effbot.org/imagingbook/image.htm